blob: 3fe9100d880fa6ee7ef96920ff7212bbfced6d66 [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 Bataev346265e2015-09-25 10:37:12 +000099 /// \brief first argument (Expr *) contains optional argument of the
100 /// 'ordered' clause, the second one is true if the regions has 'ordered'
101 /// clause, false otherwise.
102 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000103 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000104 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000105 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000106 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000107 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000108 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000109 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000110 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000111 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000112 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000114 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000115 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000116 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000117 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118 };
119
120 typedef SmallVector<SharingMapTy, 64> StackTy;
121
122 /// \brief Stack of used declaration and their data-sharing attributes.
123 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000124 /// \brief true, if check for DSA must be from parent directive, false, if
125 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000126 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000127 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000128 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129
130 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
131
132 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000133
134 /// \brief Checks if the variable is a local for OpenMP region.
135 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000136
Alexey Bataev758e55e2013-09-06 18:03:48 +0000137public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000138 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000139 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
140 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000141
Alexey Bataevaac108a2015-06-23 04:51:00 +0000142 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
143 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000145 bool isForceVarCapturing() const { return ForceCapturing; }
146 void setForceVarCapturing(bool V) { ForceCapturing = V; }
147
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000149 Scope *CurScope, SourceLocation Loc) {
150 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
151 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000152 }
153
154 void pop() {
155 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
156 Stack.pop_back();
157 }
158
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000159 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000160 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// for diagnostics.
162 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
163
Alexey Bataev9c821032015-04-30 04:23:23 +0000164 /// \brief Register specified variable as loop control variable.
165 void addLoopControlVariable(VarDecl *D);
166 /// \brief Check if the specified variable is a loop control variable for
167 /// current region.
168 bool isLoopControlVariable(VarDecl *D);
169
Alexey Bataev758e55e2013-09-06 18:03:48 +0000170 /// \brief Adds explicit data sharing attribute to the specified declaration.
171 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
172
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173 /// \brief Returns data sharing attributes from top of the stack for the
174 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000175 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000176 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000177 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000178 /// \brief Checks if the specified variables has data-sharing attributes which
179 /// match specified \a CPred predicate in any directive which matches \a DPred
180 /// predicate.
181 template <class ClausesPredicate, class DirectivesPredicate>
182 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000183 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000184 /// \brief Checks if the specified variables has data-sharing attributes which
185 /// match specified \a CPred predicate in any innermost directive which
186 /// matches \a DPred predicate.
187 template <class ClausesPredicate, class DirectivesPredicate>
188 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000189 DirectivesPredicate DPred,
190 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000191 /// \brief Checks if the specified variables has explicit data-sharing
192 /// attributes which match specified \a CPred predicate at the specified
193 /// OpenMP region.
194 bool hasExplicitDSA(VarDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000197
198 /// \brief Returns true if the directive at level \Level matches in the
199 /// specified \a DPred predicate.
200 bool hasExplicitDirective(
201 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
202 unsigned Level);
203
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000204 /// \brief Finds a directive which matches specified \a DPred predicate.
205 template <class NamedDirectivesPredicate>
206 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000207
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208 /// \brief Returns currently analyzed directive.
209 OpenMPDirectiveKind getCurrentDirective() const {
210 return Stack.back().Directive;
211 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000212 /// \brief Returns parent directive.
213 OpenMPDirectiveKind getParentDirective() const {
214 if (Stack.size() > 2)
215 return Stack[Stack.size() - 2].Directive;
216 return OMPD_unknown;
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
219 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000220 void setDefaultDSANone(SourceLocation Loc) {
221 Stack.back().DefaultAttr = DSA_none;
222 Stack.back().DefaultAttrLoc = Loc;
223 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000225 void setDefaultDSAShared(SourceLocation Loc) {
226 Stack.back().DefaultAttr = DSA_shared;
227 Stack.back().DefaultAttrLoc = Loc;
228 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000229
230 DefaultDataSharingAttributes getDefaultDSA() const {
231 return Stack.back().DefaultAttr;
232 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000233 SourceLocation getDefaultDSALocation() const {
234 return Stack.back().DefaultAttrLoc;
235 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000236
Alexey Bataevf29276e2014-06-18 04:14:57 +0000237 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000238 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000239 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000240 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000241 }
242
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000243 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000244 void setOrderedRegion(bool IsOrdered, Expr *Param) {
245 Stack.back().OrderedRegion.setInt(IsOrdered);
246 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000247 }
248 /// \brief Returns true, if parent region is ordered (has associated
249 /// 'ordered' clause), false - otherwise.
250 bool isParentOrderedRegion() const {
251 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000252 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000253 return false;
254 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000255 /// \brief Returns optional parameter for the ordered region.
256 Expr *getParentOrderedRegionParam() const {
257 if (Stack.size() > 2)
258 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
259 return nullptr;
260 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000261 /// \brief Marks current region as nowait (it has a 'nowait' clause).
262 void setNowaitRegion(bool IsNowait = true) {
263 Stack.back().NowaitRegion = IsNowait;
264 }
265 /// \brief Returns true, if parent region is nowait (has associated
266 /// 'nowait' clause), false - otherwise.
267 bool isParentNowaitRegion() const {
268 if (Stack.size() > 2)
269 return Stack[Stack.size() - 2].NowaitRegion;
270 return false;
271 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000272 /// \brief Marks parent region as cancel region.
273 void setParentCancelRegion(bool Cancel = true) {
274 if (Stack.size() > 2)
275 Stack[Stack.size() - 2].CancelRegion =
276 Stack[Stack.size() - 2].CancelRegion || Cancel;
277 }
278 /// \brief Return true if current region has inner cancel construct.
279 bool isCancelRegion() const {
280 return Stack.back().CancelRegion;
281 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000282
Alexey Bataev9c821032015-04-30 04:23:23 +0000283 /// \brief Set collapse value for the region.
284 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
285 /// \brief Return collapse value for region.
286 unsigned getCollapseNumber() const {
287 return Stack.back().CollapseNumber;
288 }
289
Alexey Bataev13314bf2014-10-09 04:18:56 +0000290 /// \brief Marks current target region as one with closely nested teams
291 /// region.
292 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
293 if (Stack.size() > 2)
294 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
295 }
296 /// \brief Returns true, if current region has closely nested teams region.
297 bool hasInnerTeamsRegion() const {
298 return getInnerTeamsRegionLoc().isValid();
299 }
300 /// \brief Returns location of the nested teams region (if any).
301 SourceLocation getInnerTeamsRegionLoc() const {
302 if (Stack.size() > 1)
303 return Stack.back().InnerTeamsRegionLoc;
304 return SourceLocation();
305 }
306
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000307 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000309 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000310};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000311bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
312 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000313 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000314}
Alexey Bataeved09d242014-05-28 05:53:51 +0000315} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316
317DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
318 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000319 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000320 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000321 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000322 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
323 // in a region but not in construct]
324 // File-scope or namespace-scope variables referenced in called routines
325 // in the region are shared unless they appear in a threadprivate
326 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000327 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000328 DVar.CKind = OMPC_shared;
329
330 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
331 // in a region but not in construct]
332 // Variables with static storage duration that are declared in called
333 // routines in the region are shared.
334 if (D->hasGlobalStorage())
335 DVar.CKind = OMPC_shared;
336
Alexey Bataev758e55e2013-09-06 18:03:48 +0000337 return DVar;
338 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000339
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000341 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
342 // in a Construct, C/C++, predetermined, p.1]
343 // Variables with automatic storage duration that are declared in a scope
344 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000345 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
346 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
347 DVar.CKind = OMPC_private;
348 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000349 }
350
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 // Explicitly specified attributes and local variables with predetermined
352 // attributes.
353 if (Iter->SharingMap.count(D)) {
354 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
355 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000356 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000357 return DVar;
358 }
359
360 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
361 // in a Construct, C/C++, implicitly determined, p.1]
362 // In a parallel or task construct, the data-sharing attributes of these
363 // variables are determined by the default clause, if present.
364 switch (Iter->DefaultAttr) {
365 case DSA_shared:
366 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000367 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000368 return DVar;
369 case DSA_none:
370 return DVar;
371 case DSA_unspecified:
372 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
373 // in a Construct, implicitly determined, p.2]
374 // In a parallel construct, if no default clause is present, these
375 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000376 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000377 if (isOpenMPParallelDirective(DVar.DKind) ||
378 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000379 DVar.CKind = OMPC_shared;
380 return DVar;
381 }
382
383 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
384 // in a Construct, implicitly determined, p.4]
385 // In a task construct, if no default clause is present, a variable that in
386 // the enclosing context is determined to be shared by all implicit tasks
387 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000388 if (DVar.DKind == OMPD_task) {
389 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000390 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000391 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000392 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
393 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000394 // in a Construct, implicitly determined, p.6]
395 // In a task construct, if no default clause is present, a variable
396 // whose data-sharing attribute is not determined by the rules above is
397 // firstprivate.
398 DVarTemp = getDSA(I, D);
399 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000400 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000402 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 return DVar;
404 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000405 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000406 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000407 }
408 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000409 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000410 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000411 return DVar;
412 }
413 }
414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a Construct, implicitly determined, p.3]
416 // For constructs other than task, if no default clause is present, these
417 // variables inherit their data-sharing attributes from the enclosing
418 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000419 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420}
421
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000422DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
423 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000424 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000425 auto It = Stack.back().AlignedMap.find(D);
426 if (It == Stack.back().AlignedMap.end()) {
427 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
428 Stack.back().AlignedMap[D] = NewDE;
429 return nullptr;
430 } else {
431 assert(It->second && "Unexpected nullptr expr in the aligned map");
432 return It->second;
433 }
434 return nullptr;
435}
436
Alexey Bataev9c821032015-04-30 04:23:23 +0000437void DSAStackTy::addLoopControlVariable(VarDecl *D) {
438 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
439 D = D->getCanonicalDecl();
440 Stack.back().LCVSet.insert(D);
441}
442
443bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
444 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
445 D = D->getCanonicalDecl();
446 return Stack.back().LCVSet.count(D) > 0;
447}
448
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000450 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451 if (A == OMPC_threadprivate) {
452 Stack[0].SharingMap[D].Attributes = A;
453 Stack[0].SharingMap[D].RefExpr = E;
454 } else {
455 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
456 Stack.back().SharingMap[D].Attributes = A;
457 Stack.back().SharingMap[D].RefExpr = E;
458 }
459}
460
Alexey Bataeved09d242014-05-28 05:53:51 +0000461bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000462 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000463 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000464 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000465 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000466 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000467 ++I;
468 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000469 if (I == E)
470 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000471 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000472 Scope *CurScope = getCurScope();
473 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000475 }
476 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000478 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000479}
480
Alexey Bataev39f915b82015-05-08 10:41:21 +0000481/// \brief Build a variable declaration for OpenMP loop iteration variable.
482static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000483 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000484 DeclContext *DC = SemaRef.CurContext;
485 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
486 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
487 VarDecl *Decl =
488 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000489 if (Attrs) {
490 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
491 I != E; ++I)
492 Decl->addAttr(*I);
493 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000494 Decl->setImplicit();
495 return Decl;
496}
497
498static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
499 SourceLocation Loc,
500 bool RefersToCapture = false) {
501 D->setReferenced();
502 D->markUsed(S.Context);
503 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
504 SourceLocation(), D, RefersToCapture, Loc, Ty,
505 VK_LValue);
506}
507
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000508DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000509 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 DSAVarData DVar;
511
512 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
513 // in a Construct, C/C++, predetermined, p.1]
514 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000515 if ((D->getTLSKind() != VarDecl::TLS_None &&
516 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
517 SemaRef.getLangOpts().OpenMPUseTLS &&
518 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000519 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
520 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000521 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
522 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000523 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000524 }
525 if (Stack[0].SharingMap.count(D)) {
526 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
527 DVar.CKind = OMPC_threadprivate;
528 return DVar;
529 }
530
531 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
532 // in a Construct, C/C++, predetermined, p.1]
533 // Variables with automatic storage duration that are declared in a scope
534 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000535 OpenMPDirectiveKind Kind =
536 FromParent ? getParentDirective() : getCurrentDirective();
537 auto StartI = std::next(Stack.rbegin());
538 auto EndI = std::prev(Stack.rend());
539 if (FromParent && StartI != EndI) {
540 StartI = std::next(StartI);
541 }
542 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000543 if (isOpenMPLocal(D, StartI) &&
544 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
545 D->getStorageClass() == SC_None)) ||
546 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000547 DVar.CKind = OMPC_private;
548 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000549 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000550
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000551 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
552 // in a Construct, C/C++, predetermined, p.4]
553 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000554 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
555 // in a Construct, C/C++, predetermined, p.7]
556 // Variables with static storage duration that are declared in a scope
557 // inside the construct are shared.
Kelvin Li4eea8c62015-09-15 18:56:58 +0000558 if (D->isStaticDataMember()) {
Alexey Bataev42971a32015-01-20 07:03:46 +0000559 DSAVarData DVarTemp =
560 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
561 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
562 return DVar;
563
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000564 DVar.CKind = OMPC_shared;
565 return DVar;
566 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 }
568
569 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000570 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
571 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
573 // in a Construct, C/C++, predetermined, p.6]
574 // Variables with const qualified type having no mutable member are
575 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000576 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000577 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000579 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000580 // Variables with const-qualified type having no mutable member may be
581 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000582 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
583 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000584 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
585 return DVar;
586
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 DVar.CKind = OMPC_shared;
588 return DVar;
589 }
590
Alexey Bataev758e55e2013-09-06 18:03:48 +0000591 // Explicitly specified attributes and local variables with predetermined
592 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000593 auto I = std::prev(StartI);
594 if (I->SharingMap.count(D)) {
595 DVar.RefExpr = I->SharingMap[D].RefExpr;
596 DVar.CKind = I->SharingMap[D].Attributes;
597 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000598 }
599
600 return DVar;
601}
602
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000603DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000604 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000605 auto StartI = Stack.rbegin();
606 auto EndI = std::prev(Stack.rend());
607 if (FromParent && StartI != EndI) {
608 StartI = std::next(StartI);
609 }
610 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611}
612
Alexey Bataevf29276e2014-06-18 04:14:57 +0000613template <class ClausesPredicate, class DirectivesPredicate>
614DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000615 DirectivesPredicate DPred,
616 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000617 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000618 auto StartI = std::next(Stack.rbegin());
619 auto EndI = std::prev(Stack.rend());
620 if (FromParent && StartI != EndI) {
621 StartI = std::next(StartI);
622 }
623 for (auto I = StartI, EE = EndI; I != EE; ++I) {
624 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000625 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000626 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000627 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000628 return DVar;
629 }
630 return DSAVarData();
631}
632
Alexey Bataevf29276e2014-06-18 04:14:57 +0000633template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000634DSAStackTy::DSAVarData
635DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
636 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000637 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000638 auto StartI = std::next(Stack.rbegin());
639 auto EndI = std::prev(Stack.rend());
640 if (FromParent && StartI != EndI) {
641 StartI = std::next(StartI);
642 }
643 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000644 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000645 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000646 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000647 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000648 return DVar;
649 return DSAVarData();
650 }
651 return DSAVarData();
652}
653
Alexey Bataevaac108a2015-06-23 04:51:00 +0000654bool DSAStackTy::hasExplicitDSA(
655 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
656 unsigned Level) {
657 if (CPred(ClauseKindMode))
658 return true;
659 if (isClauseParsingMode())
660 ++Level;
661 D = D->getCanonicalDecl();
662 auto StartI = Stack.rbegin();
663 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000664 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000665 return false;
666 std::advance(StartI, Level);
667 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
668 CPred(StartI->SharingMap[D].Attributes);
669}
670
Samuel Antao4be30e92015-10-02 17:14:03 +0000671bool DSAStackTy::hasExplicitDirective(
672 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
673 unsigned Level) {
674 if (isClauseParsingMode())
675 ++Level;
676 auto StartI = Stack.rbegin();
677 auto EndI = std::prev(Stack.rend());
678 if (std::distance(StartI, EndI) <= (int)Level)
679 return false;
680 std::advance(StartI, Level);
681 return DPred(StartI->Directive);
682}
683
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000684template <class NamedDirectivesPredicate>
685bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
686 auto StartI = std::next(Stack.rbegin());
687 auto EndI = std::prev(Stack.rend());
688 if (FromParent && StartI != EndI) {
689 StartI = std::next(StartI);
690 }
691 for (auto I = StartI, EE = EndI; I != EE; ++I) {
692 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
693 return true;
694 }
695 return false;
696}
697
Alexey Bataev758e55e2013-09-06 18:03:48 +0000698void Sema::InitDataSharingAttributesStack() {
699 VarDataSharingAttributesStack = new DSAStackTy(*this);
700}
701
702#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
703
Alexey Bataevf841bd92014-12-16 07:00:22 +0000704bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
705 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000706 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000707
708 // If we are attempting to capture a global variable in a directive with
709 // 'target' we return true so that this global is also mapped to the device.
710 //
711 // FIXME: If the declaration is enclosed in a 'declare target' directive,
712 // then it should not be captured. Therefore, an extra check has to be
713 // inserted here once support for 'declare target' is added.
714 //
715 if (!VD->hasLocalStorage()) {
716 if (DSAStack->getCurrentDirective() == OMPD_target &&
717 !DSAStack->isClauseParsingMode()) {
718 return true;
719 }
720 if (DSAStack->getCurScope() &&
721 DSAStack->hasDirective(
722 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
723 SourceLocation Loc) -> bool {
724 return isOpenMPTargetDirective(K);
725 },
726 false)) {
727 return true;
728 }
729 }
730
Alexey Bataev48977c32015-08-04 08:10:48 +0000731 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
732 (!DSAStack->isClauseParsingMode() ||
733 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000734 if (DSAStack->isLoopControlVariable(VD) ||
735 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000736 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
737 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000738 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000739 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000740 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
741 return true;
742 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000743 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000744 return DVarPrivate.CKind != OMPC_unknown;
745 }
746 return false;
747}
748
Alexey Bataevaac108a2015-06-23 04:51:00 +0000749bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
750 assert(LangOpts.OpenMP && "OpenMP is not allowed");
751 return DSAStack->hasExplicitDSA(
752 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
753}
754
Samuel Antao4be30e92015-10-02 17:14:03 +0000755bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
756 assert(LangOpts.OpenMP && "OpenMP is not allowed");
757 // Return true if the current level is no longer enclosed in a target region.
758
759 return !VD->hasLocalStorage() &&
760 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
761}
762
Alexey Bataeved09d242014-05-28 05:53:51 +0000763void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000764
765void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
766 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000767 Scope *CurScope, SourceLocation Loc) {
768 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000769 PushExpressionEvaluationContext(PotentiallyEvaluated);
770}
771
Alexey Bataevaac108a2015-06-23 04:51:00 +0000772void Sema::StartOpenMPClause(OpenMPClauseKind K) {
773 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000774}
775
Alexey Bataevaac108a2015-06-23 04:51:00 +0000776void Sema::EndOpenMPClause() {
777 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000778}
779
Alexey Bataev758e55e2013-09-06 18:03:48 +0000780void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000781 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
782 // A variable of class type (or array thereof) that appears in a lastprivate
783 // clause requires an accessible, unambiguous default constructor for the
784 // class type, unless the list item is also specified in a firstprivate
785 // clause.
786 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000787 for (auto *C : D->clauses()) {
788 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
789 SmallVector<Expr *, 8> PrivateCopies;
790 for (auto *DE : Clause->varlists()) {
791 if (DE->isValueDependent() || DE->isTypeDependent()) {
792 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000793 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000794 }
795 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000796 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000797 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000798 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000799 // Generate helper private variable and initialize it with the
800 // default value. The address of the original variable is replaced
801 // by the address of the new private variable in CodeGen. This new
802 // variable is not added to IdResolver, so the code in the OpenMP
803 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000804 auto *VDPrivate = buildVarDecl(
805 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
806 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000807 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
808 if (VDPrivate->isInvalidDecl())
809 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000810 PrivateCopies.push_back(buildDeclRefExpr(
811 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000812 } else {
813 // The variable is also a firstprivate, so initialization sequence
814 // for private copy is generated already.
815 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000816 }
817 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000818 // Set initializers to private copies if no errors were found.
819 if (PrivateCopies.size() == Clause->varlist_size()) {
820 Clause->setPrivateCopies(PrivateCopies);
821 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000822 }
823 }
824 }
825
Alexey Bataev758e55e2013-09-06 18:03:48 +0000826 DSAStack->pop();
827 DiscardCleanupsInEvaluationContext();
828 PopExpressionEvaluationContext();
829}
830
Alexander Musman3276a272015-03-21 10:12:56 +0000831static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
832 Expr *NumIterations, Sema &SemaRef,
833 Scope *S);
834
Alexey Bataeva769e072013-03-22 06:34:35 +0000835namespace {
836
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000837class VarDeclFilterCCC : public CorrectionCandidateCallback {
838private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000839 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000840
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000841public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000843 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000844 NamedDecl *ND = Candidate.getCorrectionDecl();
845 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
846 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000847 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
848 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000849 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000850 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000851 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000852};
Alexey Bataeved09d242014-05-28 05:53:51 +0000853} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000854
855ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
856 CXXScopeSpec &ScopeSpec,
857 const DeclarationNameInfo &Id) {
858 LookupResult Lookup(*this, Id, LookupOrdinaryName);
859 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
860
861 if (Lookup.isAmbiguous())
862 return ExprError();
863
864 VarDecl *VD;
865 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000866 if (TypoCorrection Corrected = CorrectTypo(
867 Id, LookupOrdinaryName, CurScope, nullptr,
868 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000869 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000870 PDiag(Lookup.empty()
871 ? diag::err_undeclared_var_use_suggest
872 : diag::err_omp_expected_var_arg_suggest)
873 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000874 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000875 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000876 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
877 : diag::err_omp_expected_var_arg)
878 << Id.getName();
879 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000880 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000881 } else {
882 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000883 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000884 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
885 return ExprError();
886 }
887 }
888 Lookup.suppressDiagnostics();
889
890 // OpenMP [2.9.2, Syntax, C/C++]
891 // Variables must be file-scope, namespace-scope, or static block-scope.
892 if (!VD->hasGlobalStorage()) {
893 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000894 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
895 bool IsDecl =
896 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000897 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
899 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000900 return ExprError();
901 }
902
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000903 VarDecl *CanonicalVD = VD->getCanonicalDecl();
904 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000905 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
906 // A threadprivate directive for file-scope variables must appear outside
907 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000908 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
909 !getCurLexicalContext()->isTranslationUnit()) {
910 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000911 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
912 bool IsDecl =
913 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
914 Diag(VD->getLocation(),
915 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
916 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000917 return ExprError();
918 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000919 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
920 // A threadprivate directive for static class member variables must appear
921 // in the class definition, in the same scope in which the member
922 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000923 if (CanonicalVD->isStaticDataMember() &&
924 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
925 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000926 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
927 bool IsDecl =
928 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
929 Diag(VD->getLocation(),
930 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
931 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000932 return ExprError();
933 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000934 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
935 // A threadprivate directive for namespace-scope variables must appear
936 // outside any definition or declaration other than the namespace
937 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000938 if (CanonicalVD->getDeclContext()->isNamespace() &&
939 (!getCurLexicalContext()->isFileContext() ||
940 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
941 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000942 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
943 bool IsDecl =
944 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
945 Diag(VD->getLocation(),
946 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
947 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000948 return ExprError();
949 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000950 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
951 // A threadprivate directive for static block-scope variables must appear
952 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000953 if (CanonicalVD->isStaticLocal() && CurScope &&
954 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000955 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000956 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
957 bool IsDecl =
958 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
959 Diag(VD->getLocation(),
960 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
961 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000962 return ExprError();
963 }
964
965 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
966 // A threadprivate directive must lexically precede all references to any
967 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000968 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000969 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000970 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000971 return ExprError();
972 }
973
974 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000975 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000976 return DE;
977}
978
Alexey Bataeved09d242014-05-28 05:53:51 +0000979Sema::DeclGroupPtrTy
980Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
981 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000982 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000983 CurContext->addDecl(D);
984 return DeclGroupPtrTy::make(DeclGroupRef(D));
985 }
986 return DeclGroupPtrTy();
987}
988
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000989namespace {
990class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
991 Sema &SemaRef;
992
993public:
994 bool VisitDeclRefExpr(const DeclRefExpr *E) {
995 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
996 if (VD->hasLocalStorage()) {
997 SemaRef.Diag(E->getLocStart(),
998 diag::err_omp_local_var_in_threadprivate_init)
999 << E->getSourceRange();
1000 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1001 << VD << VD->getSourceRange();
1002 return true;
1003 }
1004 }
1005 return false;
1006 }
1007 bool VisitStmt(const Stmt *S) {
1008 for (auto Child : S->children()) {
1009 if (Child && Visit(Child))
1010 return true;
1011 }
1012 return false;
1013 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001014 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001015};
1016} // namespace
1017
Alexey Bataeved09d242014-05-28 05:53:51 +00001018OMPThreadPrivateDecl *
1019Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001020 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001021 for (auto &RefExpr : VarList) {
1022 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001023 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1024 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001025
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001026 QualType QType = VD->getType();
1027 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1028 // It will be analyzed later.
1029 Vars.push_back(DE);
1030 continue;
1031 }
1032
Alexey Bataeva769e072013-03-22 06:34:35 +00001033 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1034 // A threadprivate variable must not have an incomplete type.
1035 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001037 continue;
1038 }
1039
1040 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1041 // A threadprivate variable must not have a reference type.
1042 if (VD->getType()->isReferenceType()) {
1043 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001044 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1045 bool IsDecl =
1046 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1047 Diag(VD->getLocation(),
1048 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1049 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001050 continue;
1051 }
1052
Samuel Antaof8b50122015-07-13 22:54:53 +00001053 // Check if this is a TLS variable. If TLS is not being supported, produce
1054 // the corresponding diagnostic.
1055 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1056 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1057 getLangOpts().OpenMPUseTLS &&
1058 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001059 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1060 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001061 Diag(ILoc, diag::err_omp_var_thread_local)
1062 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001063 bool IsDecl =
1064 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1065 Diag(VD->getLocation(),
1066 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1067 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001068 continue;
1069 }
1070
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001071 // Check if initial value of threadprivate variable reference variable with
1072 // local storage (it is not supported by runtime).
1073 if (auto Init = VD->getAnyInitializer()) {
1074 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001075 if (Checker.Visit(Init))
1076 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001077 }
1078
Alexey Bataeved09d242014-05-28 05:53:51 +00001079 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001080 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001081 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1082 Context, SourceRange(Loc, Loc)));
1083 if (auto *ML = Context.getASTMutationListener())
1084 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001085 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001086 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001087 if (!Vars.empty()) {
1088 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1089 Vars);
1090 D->setAccess(AS_public);
1091 }
1092 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001093}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001094
Alexey Bataev7ff55242014-06-19 09:13:45 +00001095static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1096 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1097 bool IsLoopIterVar = false) {
1098 if (DVar.RefExpr) {
1099 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1100 << getOpenMPClauseName(DVar.CKind);
1101 return;
1102 }
1103 enum {
1104 PDSA_StaticMemberShared,
1105 PDSA_StaticLocalVarShared,
1106 PDSA_LoopIterVarPrivate,
1107 PDSA_LoopIterVarLinear,
1108 PDSA_LoopIterVarLastprivate,
1109 PDSA_ConstVarShared,
1110 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001111 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001112 PDSA_LocalVarPrivate,
1113 PDSA_Implicit
1114 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001115 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001116 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001117 if (IsLoopIterVar) {
1118 if (DVar.CKind == OMPC_private)
1119 Reason = PDSA_LoopIterVarPrivate;
1120 else if (DVar.CKind == OMPC_lastprivate)
1121 Reason = PDSA_LoopIterVarLastprivate;
1122 else
1123 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001124 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1125 Reason = PDSA_TaskVarFirstprivate;
1126 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001127 } else if (VD->isStaticLocal())
1128 Reason = PDSA_StaticLocalVarShared;
1129 else if (VD->isStaticDataMember())
1130 Reason = PDSA_StaticMemberShared;
1131 else if (VD->isFileVarDecl())
1132 Reason = PDSA_GlobalVarShared;
1133 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1134 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001135 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001136 ReportHint = true;
1137 Reason = PDSA_LocalVarPrivate;
1138 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001139 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001140 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001141 << Reason << ReportHint
1142 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1143 } else if (DVar.ImplicitDSALoc.isValid()) {
1144 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1145 << getOpenMPClauseName(DVar.CKind);
1146 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001147}
1148
Alexey Bataev758e55e2013-09-06 18:03:48 +00001149namespace {
1150class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1151 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001152 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001153 bool ErrorFound;
1154 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001155 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001156 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001157
Alexey Bataev758e55e2013-09-06 18:03:48 +00001158public:
1159 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001160 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001161 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001162 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1163 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001164
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001165 auto DVar = Stack->getTopDSA(VD, false);
1166 // Check if the variable has explicit DSA set and stop analysis if it so.
1167 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001168
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001169 auto ELoc = E->getExprLoc();
1170 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001171 // The default(none) clause requires that each variable that is referenced
1172 // in the construct, and does not have a predetermined data-sharing
1173 // attribute, must have its data-sharing attribute explicitly determined
1174 // by being listed in a data-sharing attribute clause.
1175 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001176 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001177 VarsWithInheritedDSA.count(VD) == 0) {
1178 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001179 return;
1180 }
1181
1182 // OpenMP [2.9.3.6, Restrictions, p.2]
1183 // A list item that appears in a reduction clause of the innermost
1184 // enclosing worksharing or parallel construct may not be accessed in an
1185 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001186 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001187 [](OpenMPDirectiveKind K) -> bool {
1188 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001189 isOpenMPWorksharingDirective(K) ||
1190 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001191 },
1192 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001193 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1194 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001195 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1196 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001197 return;
1198 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001199
1200 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001201 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001202 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001203 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001204 }
1205 }
1206 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001207 for (auto *C : S->clauses()) {
1208 // Skip analysis of arguments of implicitly defined firstprivate clause
1209 // for task directives.
1210 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1211 for (auto *CC : C->children()) {
1212 if (CC)
1213 Visit(CC);
1214 }
1215 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001216 }
1217 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001218 for (auto *C : S->children()) {
1219 if (C && !isa<OMPExecutableDirective>(C))
1220 Visit(C);
1221 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001223
1224 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001225 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001226 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1227 return VarsWithInheritedDSA;
1228 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001229
Alexey Bataev7ff55242014-06-19 09:13:45 +00001230 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1231 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001232};
Alexey Bataeved09d242014-05-28 05:53:51 +00001233} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001234
Alexey Bataevbae9a792014-06-27 10:37:06 +00001235void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001236 switch (DKind) {
1237 case OMPD_parallel: {
1238 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001239 QualType KmpInt32PtrTy =
1240 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001241 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001242 std::make_pair(".global_tid.", KmpInt32PtrTy),
1243 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1244 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001245 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001246 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1247 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001248 break;
1249 }
1250 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001251 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001252 std::make_pair(StringRef(), QualType()) // __context with shared vars
1253 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001254 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1255 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001256 break;
1257 }
1258 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001259 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001260 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001261 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001262 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1263 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001264 break;
1265 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001266 case OMPD_for_simd: {
1267 Sema::CapturedParamNameType Params[] = {
1268 std::make_pair(StringRef(), QualType()) // __context with shared vars
1269 };
1270 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1271 Params);
1272 break;
1273 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001274 case OMPD_sections: {
1275 Sema::CapturedParamNameType Params[] = {
1276 std::make_pair(StringRef(), QualType()) // __context with shared vars
1277 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001278 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1279 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001280 break;
1281 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001282 case OMPD_section: {
1283 Sema::CapturedParamNameType Params[] = {
1284 std::make_pair(StringRef(), QualType()) // __context with shared vars
1285 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001286 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1287 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001288 break;
1289 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001290 case OMPD_single: {
1291 Sema::CapturedParamNameType Params[] = {
1292 std::make_pair(StringRef(), QualType()) // __context with shared vars
1293 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001294 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1295 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001296 break;
1297 }
Alexander Musman80c22892014-07-17 08:54:58 +00001298 case OMPD_master: {
1299 Sema::CapturedParamNameType Params[] = {
1300 std::make_pair(StringRef(), QualType()) // __context with shared vars
1301 };
1302 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1303 Params);
1304 break;
1305 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001306 case OMPD_critical: {
1307 Sema::CapturedParamNameType Params[] = {
1308 std::make_pair(StringRef(), QualType()) // __context with shared vars
1309 };
1310 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1311 Params);
1312 break;
1313 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001314 case OMPD_parallel_for: {
1315 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001316 QualType KmpInt32PtrTy =
1317 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001318 Sema::CapturedParamNameType Params[] = {
1319 std::make_pair(".global_tid.", KmpInt32PtrTy),
1320 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1321 std::make_pair(StringRef(), QualType()) // __context with shared vars
1322 };
1323 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1324 Params);
1325 break;
1326 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001327 case OMPD_parallel_for_simd: {
1328 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001329 QualType KmpInt32PtrTy =
1330 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001331 Sema::CapturedParamNameType Params[] = {
1332 std::make_pair(".global_tid.", KmpInt32PtrTy),
1333 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1334 std::make_pair(StringRef(), QualType()) // __context with shared vars
1335 };
1336 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1337 Params);
1338 break;
1339 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001340 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001341 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001342 QualType KmpInt32PtrTy =
1343 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001344 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001345 std::make_pair(".global_tid.", KmpInt32PtrTy),
1346 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001347 std::make_pair(StringRef(), QualType()) // __context with shared vars
1348 };
1349 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1350 Params);
1351 break;
1352 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001353 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001354 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001355 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1356 FunctionProtoType::ExtProtoInfo EPI;
1357 EPI.Variadic = true;
1358 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001360 std::make_pair(".global_tid.", KmpInt32Ty),
1361 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001362 std::make_pair(".privates.",
1363 Context.VoidPtrTy.withConst().withRestrict()),
1364 std::make_pair(
1365 ".copy_fn.",
1366 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001367 std::make_pair(StringRef(), QualType()) // __context with shared vars
1368 };
1369 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1370 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001371 // Mark this captured region as inlined, because we don't use outlined
1372 // function directly.
1373 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1374 AlwaysInlineAttr::CreateImplicit(
1375 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 break;
1377 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001378 case OMPD_ordered: {
1379 Sema::CapturedParamNameType Params[] = {
1380 std::make_pair(StringRef(), QualType()) // __context with shared vars
1381 };
1382 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1383 Params);
1384 break;
1385 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001386 case OMPD_atomic: {
1387 Sema::CapturedParamNameType Params[] = {
1388 std::make_pair(StringRef(), QualType()) // __context with shared vars
1389 };
1390 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1391 Params);
1392 break;
1393 }
Michael Wong65f367f2015-07-21 13:44:28 +00001394 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001395 case OMPD_target: {
1396 Sema::CapturedParamNameType Params[] = {
1397 std::make_pair(StringRef(), QualType()) // __context with shared vars
1398 };
1399 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1400 Params);
1401 break;
1402 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001403 case OMPD_teams: {
1404 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001405 QualType KmpInt32PtrTy =
1406 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001407 Sema::CapturedParamNameType Params[] = {
1408 std::make_pair(".global_tid.", KmpInt32PtrTy),
1409 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1410 std::make_pair(StringRef(), QualType()) // __context with shared vars
1411 };
1412 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1413 Params);
1414 break;
1415 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001416 case OMPD_taskgroup: {
1417 Sema::CapturedParamNameType Params[] = {
1418 std::make_pair(StringRef(), QualType()) // __context with shared vars
1419 };
1420 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1421 Params);
1422 break;
1423 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001424 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001425 case OMPD_taskyield:
1426 case OMPD_barrier:
1427 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001428 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001429 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001430 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001431 llvm_unreachable("OpenMP Directive is not allowed");
1432 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001433 llvm_unreachable("Unknown OpenMP directive");
1434 }
1435}
1436
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001437StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1438 ArrayRef<OMPClause *> Clauses) {
1439 if (!S.isUsable()) {
1440 ActOnCapturedRegionError();
1441 return StmtError();
1442 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001443 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001444 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001445 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001446 Clause->getClauseKind() == OMPC_copyprivate ||
1447 (getLangOpts().OpenMPUseTLS &&
1448 getASTContext().getTargetInfo().isTLSSupported() &&
1449 Clause->getClauseKind() == OMPC_copyin)) {
1450 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001451 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001452 for (auto *VarRef : Clause->children()) {
1453 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001454 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001455 }
1456 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001457 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001458 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1459 Clause->getClauseKind() == OMPC_schedule) {
1460 // Mark all variables in private list clauses as used in inner region.
1461 // Required for proper codegen of combined directives.
1462 // TODO: add processing for other clauses.
1463 if (auto *E = cast_or_null<Expr>(
1464 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1465 MarkDeclarationsReferencedInExpr(E);
1466 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001467 }
1468 }
1469 return ActOnCapturedRegionEnd(S.get());
1470}
1471
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001472static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1473 OpenMPDirectiveKind CurrentRegion,
1474 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001475 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001476 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001477 // Allowed nesting of constructs
1478 // +------------------+-----------------+------------------------------------+
1479 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1480 // +------------------+-----------------+------------------------------------+
1481 // | parallel | parallel | * |
1482 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001483 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001484 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001485 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001486 // | parallel | simd | * |
1487 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001488 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001489 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001490 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001491 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001492 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001493 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001494 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001495 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001496 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001497 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001498 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001499 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001500 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001501 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001502 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001503 // | parallel | cancellation | |
1504 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001505 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001506 // +------------------+-----------------+------------------------------------+
1507 // | for | parallel | * |
1508 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001509 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001510 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001511 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001512 // | for | simd | * |
1513 // | for | sections | + |
1514 // | for | section | + |
1515 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001516 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001517 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001518 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001519 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001520 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001521 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001522 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001523 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001524 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001525 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001526 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001527 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001528 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001529 // | for | cancellation | |
1530 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001531 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001532 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001533 // | master | parallel | * |
1534 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001535 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001536 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001537 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001538 // | master | simd | * |
1539 // | master | sections | + |
1540 // | master | section | + |
1541 // | master | single | + |
1542 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001543 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001544 // | master |parallel sections| * |
1545 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001546 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001547 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001548 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001549 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001550 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001551 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001552 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001553 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001554 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001555 // | master | cancellation | |
1556 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001557 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001558 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001559 // | critical | parallel | * |
1560 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001561 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001562 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001563 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001564 // | critical | simd | * |
1565 // | critical | sections | + |
1566 // | critical | section | + |
1567 // | critical | single | + |
1568 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001569 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001570 // | critical |parallel sections| * |
1571 // | critical | task | * |
1572 // | critical | taskyield | * |
1573 // | critical | barrier | + |
1574 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001575 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001576 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001577 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001578 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001579 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001580 // | critical | cancellation | |
1581 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001582 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001583 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001584 // | simd | parallel | |
1585 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001586 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001587 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001588 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001589 // | simd | simd | |
1590 // | simd | sections | |
1591 // | simd | section | |
1592 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001593 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001594 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001595 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001596 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001597 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001598 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001599 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001600 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001601 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001602 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001603 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001604 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001605 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001606 // | simd | cancellation | |
1607 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001608 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001609 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001610 // | for simd | parallel | |
1611 // | for simd | for | |
1612 // | for simd | for simd | |
1613 // | for simd | master | |
1614 // | for simd | critical | |
1615 // | for simd | simd | |
1616 // | for simd | sections | |
1617 // | for simd | section | |
1618 // | for simd | single | |
1619 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001620 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001621 // | for simd |parallel sections| |
1622 // | for simd | task | |
1623 // | for simd | taskyield | |
1624 // | for simd | barrier | |
1625 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001626 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001627 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001628 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001629 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001630 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001631 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001632 // | for simd | cancellation | |
1633 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001634 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001635 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001636 // | parallel for simd| parallel | |
1637 // | parallel for simd| for | |
1638 // | parallel for simd| for simd | |
1639 // | parallel for simd| master | |
1640 // | parallel for simd| critical | |
1641 // | parallel for simd| simd | |
1642 // | parallel for simd| sections | |
1643 // | parallel for simd| section | |
1644 // | parallel for simd| single | |
1645 // | parallel for simd| parallel for | |
1646 // | parallel for simd|parallel for simd| |
1647 // | parallel for simd|parallel sections| |
1648 // | parallel for simd| task | |
1649 // | parallel for simd| taskyield | |
1650 // | parallel for simd| barrier | |
1651 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001652 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001653 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001654 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001655 // | parallel for simd| atomic | |
1656 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001658 // | parallel for simd| cancellation | |
1659 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001660 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001661 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001662 // | sections | parallel | * |
1663 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001664 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001665 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001666 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001667 // | sections | simd | * |
1668 // | sections | sections | + |
1669 // | sections | section | * |
1670 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001671 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001672 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001673 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001674 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001675 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001676 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001677 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001678 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001679 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001680 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001681 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001682 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001683 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001684 // | sections | cancellation | |
1685 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001686 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001687 // +------------------+-----------------+------------------------------------+
1688 // | section | parallel | * |
1689 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001690 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001691 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001692 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001693 // | section | simd | * |
1694 // | section | sections | + |
1695 // | section | section | + |
1696 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001697 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001698 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001699 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001700 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001701 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001702 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001703 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001704 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001705 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001706 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001707 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001708 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001709 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001710 // | section | cancellation | |
1711 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001712 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001713 // +------------------+-----------------+------------------------------------+
1714 // | single | parallel | * |
1715 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001716 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001717 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001718 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001719 // | single | simd | * |
1720 // | single | sections | + |
1721 // | single | section | + |
1722 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001723 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001724 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001725 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001726 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001727 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001728 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001729 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001730 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001731 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001732 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001733 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001734 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001735 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001736 // | single | cancellation | |
1737 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001738 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001739 // +------------------+-----------------+------------------------------------+
1740 // | parallel for | parallel | * |
1741 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001742 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001743 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001744 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001745 // | parallel for | simd | * |
1746 // | parallel for | sections | + |
1747 // | parallel for | section | + |
1748 // | parallel for | single | + |
1749 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001750 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001751 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001752 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001753 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001754 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001755 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001756 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001757 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001758 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001759 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001760 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001761 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001762 // | parallel for | cancellation | |
1763 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001764 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001765 // +------------------+-----------------+------------------------------------+
1766 // | parallel sections| parallel | * |
1767 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001768 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001769 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001770 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001771 // | parallel sections| simd | * |
1772 // | parallel sections| sections | + |
1773 // | parallel sections| section | * |
1774 // | parallel sections| single | + |
1775 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001776 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001777 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001778 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001779 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001780 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001781 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001782 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001783 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001784 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001785 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001786 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001787 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001788 // | parallel sections| cancellation | |
1789 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001790 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001791 // +------------------+-----------------+------------------------------------+
1792 // | task | parallel | * |
1793 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001794 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001795 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001796 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001797 // | task | simd | * |
1798 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001799 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001800 // | task | single | + |
1801 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001802 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001803 // | task |parallel sections| * |
1804 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001805 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001806 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001807 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001808 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001809 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001810 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001811 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001812 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001813 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001814 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001815 // | | point | ! |
1816 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001817 // +------------------+-----------------+------------------------------------+
1818 // | ordered | parallel | * |
1819 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001820 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001821 // | ordered | master | * |
1822 // | ordered | critical | * |
1823 // | ordered | simd | * |
1824 // | ordered | sections | + |
1825 // | ordered | section | + |
1826 // | ordered | single | + |
1827 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001828 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001829 // | ordered |parallel sections| * |
1830 // | ordered | task | * |
1831 // | ordered | taskyield | * |
1832 // | ordered | barrier | + |
1833 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001834 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001835 // | ordered | flush | * |
1836 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001837 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001838 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001839 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001840 // | ordered | cancellation | |
1841 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001842 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001843 // +------------------+-----------------+------------------------------------+
1844 // | atomic | parallel | |
1845 // | atomic | for | |
1846 // | atomic | for simd | |
1847 // | atomic | master | |
1848 // | atomic | critical | |
1849 // | atomic | simd | |
1850 // | atomic | sections | |
1851 // | atomic | section | |
1852 // | atomic | single | |
1853 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001854 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001855 // | atomic |parallel sections| |
1856 // | atomic | task | |
1857 // | atomic | taskyield | |
1858 // | atomic | barrier | |
1859 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001860 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001861 // | atomic | flush | |
1862 // | atomic | ordered | |
1863 // | atomic | atomic | |
1864 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001865 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001866 // | atomic | cancellation | |
1867 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001868 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001869 // +------------------+-----------------+------------------------------------+
1870 // | target | parallel | * |
1871 // | target | for | * |
1872 // | target | for simd | * |
1873 // | target | master | * |
1874 // | target | critical | * |
1875 // | target | simd | * |
1876 // | target | sections | * |
1877 // | target | section | * |
1878 // | target | single | * |
1879 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001880 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001881 // | target |parallel sections| * |
1882 // | target | task | * |
1883 // | target | taskyield | * |
1884 // | target | barrier | * |
1885 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001886 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001887 // | target | flush | * |
1888 // | target | ordered | * |
1889 // | target | atomic | * |
1890 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001891 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001892 // | target | cancellation | |
1893 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001894 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001895 // +------------------+-----------------+------------------------------------+
1896 // | teams | parallel | * |
1897 // | teams | for | + |
1898 // | teams | for simd | + |
1899 // | teams | master | + |
1900 // | teams | critical | + |
1901 // | teams | simd | + |
1902 // | teams | sections | + |
1903 // | teams | section | + |
1904 // | teams | single | + |
1905 // | teams | parallel for | * |
1906 // | teams |parallel for simd| * |
1907 // | teams |parallel sections| * |
1908 // | teams | task | + |
1909 // | teams | taskyield | + |
1910 // | teams | barrier | + |
1911 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001912 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001913 // | teams | flush | + |
1914 // | teams | ordered | + |
1915 // | teams | atomic | + |
1916 // | teams | target | + |
1917 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001918 // | teams | cancellation | |
1919 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001920 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001921 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001922 if (Stack->getCurScope()) {
1923 auto ParentRegion = Stack->getParentDirective();
1924 bool NestingProhibited = false;
1925 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001926 enum {
1927 NoRecommend,
1928 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001929 ShouldBeInOrderedRegion,
1930 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001931 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001932 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001933 // OpenMP [2.16, Nesting of Regions]
1934 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001935 // OpenMP [2.8.1,simd Construct, Restrictions]
1936 // An ordered construct with the simd clause is the only OpenMP construct
1937 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00001938 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1939 return true;
1940 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001941 if (ParentRegion == OMPD_atomic) {
1942 // OpenMP [2.16, Nesting of Regions]
1943 // OpenMP constructs may not be nested inside an atomic region.
1944 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1945 return true;
1946 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001947 if (CurrentRegion == OMPD_section) {
1948 // OpenMP [2.7.2, sections Construct, Restrictions]
1949 // Orphaned section directives are prohibited. That is, the section
1950 // directives must appear within the sections construct and must not be
1951 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001952 if (ParentRegion != OMPD_sections &&
1953 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001954 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1955 << (ParentRegion != OMPD_unknown)
1956 << getOpenMPDirectiveName(ParentRegion);
1957 return true;
1958 }
1959 return false;
1960 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001961 // Allow some constructs to be orphaned (they could be used in functions,
1962 // called from OpenMP regions with the required preconditions).
1963 if (ParentRegion == OMPD_unknown)
1964 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001965 if (CurrentRegion == OMPD_cancellation_point ||
1966 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001967 // OpenMP [2.16, Nesting of Regions]
1968 // A cancellation point construct for which construct-type-clause is
1969 // taskgroup must be nested inside a task construct. A cancellation
1970 // point construct for which construct-type-clause is not taskgroup must
1971 // be closely nested inside an OpenMP construct that matches the type
1972 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001973 // A cancel construct for which construct-type-clause is taskgroup must be
1974 // nested inside a task construct. A cancel construct for which
1975 // construct-type-clause is not taskgroup must be closely nested inside an
1976 // OpenMP construct that matches the type specified in
1977 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001978 NestingProhibited =
1979 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00001980 (CancelRegion == OMPD_for &&
1981 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001982 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1983 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00001984 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
1985 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001986 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001987 // OpenMP [2.16, Nesting of Regions]
1988 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001989 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001990 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1991 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001992 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1993 // OpenMP [2.16, Nesting of Regions]
1994 // A critical region may not be nested (closely or otherwise) inside a
1995 // critical region with the same name. Note that this restriction is not
1996 // sufficient to prevent deadlock.
1997 SourceLocation PreviousCriticalLoc;
1998 bool DeadLock =
1999 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2000 OpenMPDirectiveKind K,
2001 const DeclarationNameInfo &DNI,
2002 SourceLocation Loc)
2003 ->bool {
2004 if (K == OMPD_critical &&
2005 DNI.getName() == CurrentName.getName()) {
2006 PreviousCriticalLoc = Loc;
2007 return true;
2008 } else
2009 return false;
2010 },
2011 false /* skip top directive */);
2012 if (DeadLock) {
2013 SemaRef.Diag(StartLoc,
2014 diag::err_omp_prohibited_region_critical_same_name)
2015 << CurrentName.getName();
2016 if (PreviousCriticalLoc.isValid())
2017 SemaRef.Diag(PreviousCriticalLoc,
2018 diag::note_omp_previous_critical_region);
2019 return true;
2020 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002021 } else if (CurrentRegion == OMPD_barrier) {
2022 // OpenMP [2.16, Nesting of Regions]
2023 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002024 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002025 NestingProhibited =
2026 isOpenMPWorksharingDirective(ParentRegion) ||
2027 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
2028 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002029 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002030 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002031 // OpenMP [2.16, Nesting of Regions]
2032 // A worksharing region may not be closely nested inside a worksharing,
2033 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002034 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002035 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002036 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
2037 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
2038 Recommend = ShouldBeInParallelRegion;
2039 } else if (CurrentRegion == OMPD_ordered) {
2040 // OpenMP [2.16, Nesting of Regions]
2041 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002042 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002043 // An ordered region must be closely nested inside a loop region (or
2044 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002045 // OpenMP [2.8.1,simd Construct, Restrictions]
2046 // An ordered construct with the simd clause is the only OpenMP construct
2047 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002048 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002049 ParentRegion == OMPD_task ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002050 !(isOpenMPSimdDirective(ParentRegion) ||
2051 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002052 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002053 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2054 // OpenMP [2.16, Nesting of Regions]
2055 // If specified, a teams construct must be contained within a target
2056 // construct.
2057 NestingProhibited = ParentRegion != OMPD_target;
2058 Recommend = ShouldBeInTargetRegion;
2059 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2060 }
2061 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2062 // OpenMP [2.16, Nesting of Regions]
2063 // distribute, parallel, parallel sections, parallel workshare, and the
2064 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2065 // constructs that can be closely nested in the teams region.
2066 // TODO: add distribute directive.
2067 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
2068 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002069 }
2070 if (NestingProhibited) {
2071 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002072 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2073 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002074 return true;
2075 }
2076 }
2077 return false;
2078}
2079
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002080static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2081 ArrayRef<OMPClause *> Clauses,
2082 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2083 bool ErrorFound = false;
2084 unsigned NamedModifiersNumber = 0;
2085 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2086 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002087 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002088 for (const auto *C : Clauses) {
2089 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2090 // At most one if clause without a directive-name-modifier can appear on
2091 // the directive.
2092 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2093 if (FoundNameModifiers[CurNM]) {
2094 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2095 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2096 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2097 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002098 } else if (CurNM != OMPD_unknown) {
2099 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002100 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002101 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002102 FoundNameModifiers[CurNM] = IC;
2103 if (CurNM == OMPD_unknown)
2104 continue;
2105 // Check if the specified name modifier is allowed for the current
2106 // directive.
2107 // At most one if clause with the particular directive-name-modifier can
2108 // appear on the directive.
2109 bool MatchFound = false;
2110 for (auto NM : AllowedNameModifiers) {
2111 if (CurNM == NM) {
2112 MatchFound = true;
2113 break;
2114 }
2115 }
2116 if (!MatchFound) {
2117 S.Diag(IC->getNameModifierLoc(),
2118 diag::err_omp_wrong_if_directive_name_modifier)
2119 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2120 ErrorFound = true;
2121 }
2122 }
2123 }
2124 // If any if clause on the directive includes a directive-name-modifier then
2125 // all if clauses on the directive must include a directive-name-modifier.
2126 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2127 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2128 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2129 diag::err_omp_no_more_if_clause);
2130 } else {
2131 std::string Values;
2132 std::string Sep(", ");
2133 unsigned AllowedCnt = 0;
2134 unsigned TotalAllowedNum =
2135 AllowedNameModifiers.size() - NamedModifiersNumber;
2136 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2137 ++Cnt) {
2138 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2139 if (!FoundNameModifiers[NM]) {
2140 Values += "'";
2141 Values += getOpenMPDirectiveName(NM);
2142 Values += "'";
2143 if (AllowedCnt + 2 == TotalAllowedNum)
2144 Values += " or ";
2145 else if (AllowedCnt + 1 != TotalAllowedNum)
2146 Values += Sep;
2147 ++AllowedCnt;
2148 }
2149 }
2150 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2151 diag::err_omp_unnamed_if_clause)
2152 << (TotalAllowedNum > 1) << Values;
2153 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002154 for (auto Loc : NameModifierLoc) {
2155 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2156 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002157 ErrorFound = true;
2158 }
2159 return ErrorFound;
2160}
2161
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002162StmtResult Sema::ActOnOpenMPExecutableDirective(
2163 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2164 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2165 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002166 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002167 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2168 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002169 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002170
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002171 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002172 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002173 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002174 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002175 if (AStmt) {
2176 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2177
2178 // Check default data sharing attributes for referenced variables.
2179 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2180 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2181 if (DSAChecker.isErrorFound())
2182 return StmtError();
2183 // Generate list of implicitly defined firstprivate variables.
2184 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002185
2186 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2187 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2188 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2189 SourceLocation(), SourceLocation())) {
2190 ClausesWithImplicit.push_back(Implicit);
2191 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2192 DSAChecker.getImplicitFirstprivate().size();
2193 } else
2194 ErrorFound = true;
2195 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002196 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002197
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002198 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002199 switch (Kind) {
2200 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002201 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2202 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002203 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002204 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002205 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002206 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2207 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002208 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002209 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002210 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2211 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002212 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002213 case OMPD_for_simd:
2214 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2215 EndLoc, VarsWithInheritedDSA);
2216 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002217 case OMPD_sections:
2218 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2219 EndLoc);
2220 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002221 case OMPD_section:
2222 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002223 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002224 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2225 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002226 case OMPD_single:
2227 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2228 EndLoc);
2229 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002230 case OMPD_master:
2231 assert(ClausesWithImplicit.empty() &&
2232 "No clauses are allowed for 'omp master' directive");
2233 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2234 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002235 case OMPD_critical:
2236 assert(ClausesWithImplicit.empty() &&
2237 "No clauses are allowed for 'omp critical' directive");
2238 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2239 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002240 case OMPD_parallel_for:
2241 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2242 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002243 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002244 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002245 case OMPD_parallel_for_simd:
2246 Res = ActOnOpenMPParallelForSimdDirective(
2247 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002248 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002249 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002250 case OMPD_parallel_sections:
2251 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2252 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002253 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002254 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002255 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002256 Res =
2257 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002258 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002259 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002260 case OMPD_taskyield:
2261 assert(ClausesWithImplicit.empty() &&
2262 "No clauses are allowed for 'omp taskyield' directive");
2263 assert(AStmt == nullptr &&
2264 "No associated statement allowed for 'omp taskyield' directive");
2265 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2266 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002267 case OMPD_barrier:
2268 assert(ClausesWithImplicit.empty() &&
2269 "No clauses are allowed for 'omp barrier' directive");
2270 assert(AStmt == nullptr &&
2271 "No associated statement allowed for 'omp barrier' directive");
2272 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2273 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002274 case OMPD_taskwait:
2275 assert(ClausesWithImplicit.empty() &&
2276 "No clauses are allowed for 'omp taskwait' directive");
2277 assert(AStmt == nullptr &&
2278 "No associated statement allowed for 'omp taskwait' directive");
2279 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2280 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002281 case OMPD_taskgroup:
2282 assert(ClausesWithImplicit.empty() &&
2283 "No clauses are allowed for 'omp taskgroup' directive");
2284 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2285 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002286 case OMPD_flush:
2287 assert(AStmt == nullptr &&
2288 "No associated statement allowed for 'omp flush' directive");
2289 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2290 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002291 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002292 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2293 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002294 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002295 case OMPD_atomic:
2296 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2297 EndLoc);
2298 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002299 case OMPD_teams:
2300 Res =
2301 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2302 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002303 case OMPD_target:
2304 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2305 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002306 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002307 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002308 case OMPD_cancellation_point:
2309 assert(ClausesWithImplicit.empty() &&
2310 "No clauses are allowed for 'omp cancellation point' directive");
2311 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2312 "cancellation point' directive");
2313 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2314 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002315 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002316 assert(AStmt == nullptr &&
2317 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002318 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2319 CancelRegion);
2320 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002321 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002322 case OMPD_target_data:
2323 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2324 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002325 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002326 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002327 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002328 llvm_unreachable("OpenMP Directive is not allowed");
2329 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002330 llvm_unreachable("Unknown OpenMP directive");
2331 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002332
Alexey Bataev4acb8592014-07-07 13:01:15 +00002333 for (auto P : VarsWithInheritedDSA) {
2334 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2335 << P.first << P.second->getSourceRange();
2336 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002337 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2338
2339 if (!AllowedNameModifiers.empty())
2340 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2341 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002342
Alexey Bataeved09d242014-05-28 05:53:51 +00002343 if (ErrorFound)
2344 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002345 return Res;
2346}
2347
2348StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2349 Stmt *AStmt,
2350 SourceLocation StartLoc,
2351 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002352 if (!AStmt)
2353 return StmtError();
2354
Alexey Bataev9959db52014-05-06 10:08:46 +00002355 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2356 // 1.2.2 OpenMP Language Terminology
2357 // Structured block - An executable statement with a single entry at the
2358 // top and a single exit at the bottom.
2359 // The point of exit cannot be a branch out of the structured block.
2360 // longjmp() and throw() must not violate the entry/exit criteria.
2361 CS->getCapturedDecl()->setNothrow();
2362
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002363 getCurFunction()->setHasBranchProtectedScope();
2364
Alexey Bataev25e5b442015-09-15 12:52:43 +00002365 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2366 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002367}
2368
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002369namespace {
2370/// \brief Helper class for checking canonical form of the OpenMP loops and
2371/// extracting iteration space of each loop in the loop nest, that will be used
2372/// for IR generation.
2373class OpenMPIterationSpaceChecker {
2374 /// \brief Reference to Sema.
2375 Sema &SemaRef;
2376 /// \brief A location for diagnostics (when there is no some better location).
2377 SourceLocation DefaultLoc;
2378 /// \brief A location for diagnostics (when increment is not compatible).
2379 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002380 /// \brief A source location for referring to loop init later.
2381 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002382 /// \brief A source location for referring to condition later.
2383 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002384 /// \brief A source location for referring to increment later.
2385 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002386 /// \brief Loop variable.
2387 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002388 /// \brief Reference to loop variable.
2389 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002390 /// \brief Lower bound (initializer for the var).
2391 Expr *LB;
2392 /// \brief Upper bound.
2393 Expr *UB;
2394 /// \brief Loop step (increment).
2395 Expr *Step;
2396 /// \brief This flag is true when condition is one of:
2397 /// Var < UB
2398 /// Var <= UB
2399 /// UB > Var
2400 /// UB >= Var
2401 bool TestIsLessOp;
2402 /// \brief This flag is true when condition is strict ( < or > ).
2403 bool TestIsStrictOp;
2404 /// \brief This flag is true when step is subtracted on each iteration.
2405 bool SubtractStep;
2406
2407public:
2408 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2409 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002410 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2411 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002412 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2413 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002414 /// \brief Check init-expr for canonical loop form and save loop counter
2415 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002416 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002417 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2418 /// for less/greater and for strict/non-strict comparison.
2419 bool CheckCond(Expr *S);
2420 /// \brief Check incr-expr for canonical loop form and return true if it
2421 /// does not conform, otherwise save loop step (#Step).
2422 bool CheckInc(Expr *S);
2423 /// \brief Return the loop counter variable.
2424 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002425 /// \brief Return the reference expression to loop counter variable.
2426 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002427 /// \brief Source range of the loop init.
2428 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2429 /// \brief Source range of the loop condition.
2430 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2431 /// \brief Source range of the loop increment.
2432 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2433 /// \brief True if the step should be subtracted.
2434 bool ShouldSubtractStep() const { return SubtractStep; }
2435 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002436 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002437 /// \brief Build the precondition expression for the loops.
2438 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002439 /// \brief Build reference expression to the counter be used for codegen.
2440 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002441 /// \brief Build reference expression to the private counter be used for
2442 /// codegen.
2443 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002444 /// \brief Build initization of the counter be used for codegen.
2445 Expr *BuildCounterInit() const;
2446 /// \brief Build step of the counter be used for codegen.
2447 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002448 /// \brief Return true if any expression is dependent.
2449 bool Dependent() const;
2450
2451private:
2452 /// \brief Check the right-hand side of an assignment in the increment
2453 /// expression.
2454 bool CheckIncRHS(Expr *RHS);
2455 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002456 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002457 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002458 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002459 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002460 /// \brief Helper to set loop increment.
2461 bool SetStep(Expr *NewStep, bool Subtract);
2462};
2463
2464bool OpenMPIterationSpaceChecker::Dependent() const {
2465 if (!Var) {
2466 assert(!LB && !UB && !Step);
2467 return false;
2468 }
2469 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2470 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2471}
2472
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002473template <typename T>
2474static T *getExprAsWritten(T *E) {
2475 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2476 E = ExprTemp->getSubExpr();
2477
2478 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2479 E = MTE->GetTemporaryExpr();
2480
2481 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2482 E = Binder->getSubExpr();
2483
2484 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2485 E = ICE->getSubExprAsWritten();
2486 return E->IgnoreParens();
2487}
2488
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002489bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2490 DeclRefExpr *NewVarRefExpr,
2491 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002492 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002493 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2494 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002495 if (!NewVar || !NewLB)
2496 return true;
2497 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002498 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002499 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2500 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002501 if ((Ctor->isCopyOrMoveConstructor() ||
2502 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2503 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002504 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002505 LB = NewLB;
2506 return false;
2507}
2508
2509bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002510 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002511 // State consistency checking to ensure correct usage.
2512 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2513 !TestIsLessOp && !TestIsStrictOp);
2514 if (!NewUB)
2515 return true;
2516 UB = NewUB;
2517 TestIsLessOp = LessOp;
2518 TestIsStrictOp = StrictOp;
2519 ConditionSrcRange = SR;
2520 ConditionLoc = SL;
2521 return false;
2522}
2523
2524bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2525 // State consistency checking to ensure correct usage.
2526 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2527 if (!NewStep)
2528 return true;
2529 if (!NewStep->isValueDependent()) {
2530 // Check that the step is integer expression.
2531 SourceLocation StepLoc = NewStep->getLocStart();
2532 ExprResult Val =
2533 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2534 if (Val.isInvalid())
2535 return true;
2536 NewStep = Val.get();
2537
2538 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2539 // If test-expr is of form var relational-op b and relational-op is < or
2540 // <= then incr-expr must cause var to increase on each iteration of the
2541 // loop. If test-expr is of form var relational-op b and relational-op is
2542 // > or >= then incr-expr must cause var to decrease on each iteration of
2543 // the loop.
2544 // If test-expr is of form b relational-op var and relational-op is < or
2545 // <= then incr-expr must cause var to decrease on each iteration of the
2546 // loop. If test-expr is of form b relational-op var and relational-op is
2547 // > or >= then incr-expr must cause var to increase on each iteration of
2548 // the loop.
2549 llvm::APSInt Result;
2550 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2551 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2552 bool IsConstNeg =
2553 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002554 bool IsConstPos =
2555 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002556 bool IsConstZero = IsConstant && !Result.getBoolValue();
2557 if (UB && (IsConstZero ||
2558 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002559 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002560 SemaRef.Diag(NewStep->getExprLoc(),
2561 diag::err_omp_loop_incr_not_compatible)
2562 << Var << TestIsLessOp << NewStep->getSourceRange();
2563 SemaRef.Diag(ConditionLoc,
2564 diag::note_omp_loop_cond_requres_compatible_incr)
2565 << TestIsLessOp << ConditionSrcRange;
2566 return true;
2567 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002568 if (TestIsLessOp == Subtract) {
2569 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2570 NewStep).get();
2571 Subtract = !Subtract;
2572 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002573 }
2574
2575 Step = NewStep;
2576 SubtractStep = Subtract;
2577 return false;
2578}
2579
Alexey Bataev9c821032015-04-30 04:23:23 +00002580bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002581 // Check init-expr for canonical loop form and save loop counter
2582 // variable - #Var and its initialization value - #LB.
2583 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2584 // var = lb
2585 // integer-type var = lb
2586 // random-access-iterator-type var = lb
2587 // pointer-type var = lb
2588 //
2589 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002590 if (EmitDiags) {
2591 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2592 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002593 return true;
2594 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002595 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002596 if (Expr *E = dyn_cast<Expr>(S))
2597 S = E->IgnoreParens();
2598 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2599 if (BO->getOpcode() == BO_Assign)
2600 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002601 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002602 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002603 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2604 if (DS->isSingleDecl()) {
2605 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002606 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002607 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002608 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002609 SemaRef.Diag(S->getLocStart(),
2610 diag::ext_omp_loop_not_canonical_init)
2611 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002612 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002613 }
2614 }
2615 }
2616 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2617 if (CE->getOperator() == OO_Equal)
2618 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002619 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2620 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002621
Alexey Bataev9c821032015-04-30 04:23:23 +00002622 if (EmitDiags) {
2623 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2624 << S->getSourceRange();
2625 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002626 return true;
2627}
2628
Alexey Bataev23b69422014-06-18 07:08:49 +00002629/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002630/// variable (which may be the loop variable) if possible.
2631static const VarDecl *GetInitVarDecl(const Expr *E) {
2632 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002633 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002634 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002635 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2636 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002637 if ((Ctor->isCopyOrMoveConstructor() ||
2638 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2639 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002640 E = CE->getArg(0)->IgnoreParenImpCasts();
2641 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2642 if (!DRE)
2643 return nullptr;
2644 return dyn_cast<VarDecl>(DRE->getDecl());
2645}
2646
2647bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2648 // Check test-expr for canonical form, save upper-bound UB, flags for
2649 // less/greater and for strict/non-strict comparison.
2650 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2651 // var relational-op b
2652 // b relational-op var
2653 //
2654 if (!S) {
2655 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2656 return true;
2657 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002658 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002659 SourceLocation CondLoc = S->getLocStart();
2660 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2661 if (BO->isRelationalOp()) {
2662 if (GetInitVarDecl(BO->getLHS()) == Var)
2663 return SetUB(BO->getRHS(),
2664 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2665 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2666 BO->getSourceRange(), BO->getOperatorLoc());
2667 if (GetInitVarDecl(BO->getRHS()) == Var)
2668 return SetUB(BO->getLHS(),
2669 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2670 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2671 BO->getSourceRange(), BO->getOperatorLoc());
2672 }
2673 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2674 if (CE->getNumArgs() == 2) {
2675 auto Op = CE->getOperator();
2676 switch (Op) {
2677 case OO_Greater:
2678 case OO_GreaterEqual:
2679 case OO_Less:
2680 case OO_LessEqual:
2681 if (GetInitVarDecl(CE->getArg(0)) == Var)
2682 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2683 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2684 CE->getOperatorLoc());
2685 if (GetInitVarDecl(CE->getArg(1)) == Var)
2686 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2687 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2688 CE->getOperatorLoc());
2689 break;
2690 default:
2691 break;
2692 }
2693 }
2694 }
2695 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2696 << S->getSourceRange() << Var;
2697 return true;
2698}
2699
2700bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2701 // RHS of canonical loop form increment can be:
2702 // var + incr
2703 // incr + var
2704 // var - incr
2705 //
2706 RHS = RHS->IgnoreParenImpCasts();
2707 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2708 if (BO->isAdditiveOp()) {
2709 bool IsAdd = BO->getOpcode() == BO_Add;
2710 if (GetInitVarDecl(BO->getLHS()) == Var)
2711 return SetStep(BO->getRHS(), !IsAdd);
2712 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2713 return SetStep(BO->getLHS(), false);
2714 }
2715 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2716 bool IsAdd = CE->getOperator() == OO_Plus;
2717 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2718 if (GetInitVarDecl(CE->getArg(0)) == Var)
2719 return SetStep(CE->getArg(1), !IsAdd);
2720 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2721 return SetStep(CE->getArg(0), false);
2722 }
2723 }
2724 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2725 << RHS->getSourceRange() << Var;
2726 return true;
2727}
2728
2729bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2730 // Check incr-expr for canonical loop form and return true if it
2731 // does not conform.
2732 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2733 // ++var
2734 // var++
2735 // --var
2736 // var--
2737 // var += incr
2738 // var -= incr
2739 // var = var + incr
2740 // var = incr + var
2741 // var = var - incr
2742 //
2743 if (!S) {
2744 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2745 return true;
2746 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002747 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002748 S = S->IgnoreParens();
2749 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2750 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2751 return SetStep(
2752 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2753 (UO->isDecrementOp() ? -1 : 1)).get(),
2754 false);
2755 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2756 switch (BO->getOpcode()) {
2757 case BO_AddAssign:
2758 case BO_SubAssign:
2759 if (GetInitVarDecl(BO->getLHS()) == Var)
2760 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2761 break;
2762 case BO_Assign:
2763 if (GetInitVarDecl(BO->getLHS()) == Var)
2764 return CheckIncRHS(BO->getRHS());
2765 break;
2766 default:
2767 break;
2768 }
2769 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2770 switch (CE->getOperator()) {
2771 case OO_PlusPlus:
2772 case OO_MinusMinus:
2773 if (GetInitVarDecl(CE->getArg(0)) == Var)
2774 return SetStep(
2775 SemaRef.ActOnIntegerConstant(
2776 CE->getLocStart(),
2777 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2778 false);
2779 break;
2780 case OO_PlusEqual:
2781 case OO_MinusEqual:
2782 if (GetInitVarDecl(CE->getArg(0)) == Var)
2783 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2784 break;
2785 case OO_Equal:
2786 if (GetInitVarDecl(CE->getArg(0)) == Var)
2787 return CheckIncRHS(CE->getArg(1));
2788 break;
2789 default:
2790 break;
2791 }
2792 }
2793 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2794 << S->getSourceRange() << Var;
2795 return true;
2796}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002797
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002798namespace {
2799// Transform variables declared in GNU statement expressions to new ones to
2800// avoid crash on codegen.
2801class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2802 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2803
2804public:
2805 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2806
2807 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2808 if (auto *VD = cast<VarDecl>(D))
2809 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2810 !isa<ImplicitParamDecl>(D)) {
2811 auto *NewVD = VarDecl::Create(
2812 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2813 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2814 VD->getTypeSourceInfo(), VD->getStorageClass());
2815 NewVD->setTSCSpec(VD->getTSCSpec());
2816 NewVD->setInit(VD->getInit());
2817 NewVD->setInitStyle(VD->getInitStyle());
2818 NewVD->setExceptionVariable(VD->isExceptionVariable());
2819 NewVD->setNRVOVariable(VD->isNRVOVariable());
2820 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2821 NewVD->setConstexpr(VD->isConstexpr());
2822 NewVD->setInitCapture(VD->isInitCapture());
2823 NewVD->setPreviousDeclInSameBlockScope(
2824 VD->isPreviousDeclInSameBlockScope());
2825 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00002826 if (VD->hasAttrs())
2827 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002828 transformedLocalDecl(VD, NewVD);
2829 return NewVD;
2830 }
2831 return BaseTransform::TransformDefinition(Loc, D);
2832 }
2833
2834 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2835 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2836 if (E->getDecl() != NewD) {
2837 NewD->setReferenced();
2838 NewD->markUsed(SemaRef.Context);
2839 return DeclRefExpr::Create(
2840 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2841 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2842 E->getNameInfo(), E->getType(), E->getValueKind());
2843 }
2844 return BaseTransform::TransformDeclRefExpr(E);
2845 }
2846};
2847}
2848
Alexander Musmana5f070a2014-10-01 06:03:56 +00002849/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002850Expr *
2851OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2852 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002853 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002854 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002855 auto VarType = Var->getType().getNonReferenceType();
2856 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002857 SemaRef.getLangOpts().CPlusPlus) {
2858 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002859 auto *UBExpr = TestIsLessOp ? UB : LB;
2860 auto *LBExpr = TestIsLessOp ? LB : UB;
2861 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2862 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2863 if (!Upper || !Lower)
2864 return nullptr;
2865 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2866 Sema::AA_Converting,
2867 /*AllowExplicit=*/true)
2868 .get();
2869 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2870 Sema::AA_Converting,
2871 /*AllowExplicit=*/true)
2872 .get();
2873 if (!Upper || !Lower)
2874 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002875
2876 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2877
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002878 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002879 // BuildBinOp already emitted error, this one is to point user to upper
2880 // and lower bound, and to tell what is passed to 'operator-'.
2881 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2882 << Upper->getSourceRange() << Lower->getSourceRange();
2883 return nullptr;
2884 }
2885 }
2886
2887 if (!Diff.isUsable())
2888 return nullptr;
2889
2890 // Upper - Lower [- 1]
2891 if (TestIsStrictOp)
2892 Diff = SemaRef.BuildBinOp(
2893 S, DefaultLoc, BO_Sub, Diff.get(),
2894 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2895 if (!Diff.isUsable())
2896 return nullptr;
2897
2898 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002899 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2900 if (NewStep.isInvalid())
2901 return nullptr;
2902 NewStep = SemaRef.PerformImplicitConversion(
2903 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2904 /*AllowExplicit=*/true);
2905 if (NewStep.isInvalid())
2906 return nullptr;
2907 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002908 if (!Diff.isUsable())
2909 return nullptr;
2910
2911 // Parentheses (for dumping/debugging purposes only).
2912 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2913 if (!Diff.isUsable())
2914 return nullptr;
2915
2916 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002917 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2918 if (NewStep.isInvalid())
2919 return nullptr;
2920 NewStep = SemaRef.PerformImplicitConversion(
2921 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2922 /*AllowExplicit=*/true);
2923 if (NewStep.isInvalid())
2924 return nullptr;
2925 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002926 if (!Diff.isUsable())
2927 return nullptr;
2928
Alexander Musman174b3ca2014-10-06 11:16:29 +00002929 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002930 QualType Type = Diff.get()->getType();
2931 auto &C = SemaRef.Context;
2932 bool UseVarType = VarType->hasIntegerRepresentation() &&
2933 C.getTypeSize(Type) > C.getTypeSize(VarType);
2934 if (!Type->isIntegerType() || UseVarType) {
2935 unsigned NewSize =
2936 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2937 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2938 : Type->hasSignedIntegerRepresentation();
2939 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2940 Diff = SemaRef.PerformImplicitConversion(
2941 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2942 if (!Diff.isUsable())
2943 return nullptr;
2944 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002945 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002946 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2947 if (NewSize != C.getTypeSize(Type)) {
2948 if (NewSize < C.getTypeSize(Type)) {
2949 assert(NewSize == 64 && "incorrect loop var size");
2950 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2951 << InitSrcRange << ConditionSrcRange;
2952 }
2953 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002954 NewSize, Type->hasSignedIntegerRepresentation() ||
2955 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002956 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2957 Sema::AA_Converting, true);
2958 if (!Diff.isUsable())
2959 return nullptr;
2960 }
2961 }
2962
Alexander Musmana5f070a2014-10-01 06:03:56 +00002963 return Diff.get();
2964}
2965
Alexey Bataev62dbb972015-04-22 11:59:37 +00002966Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2967 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2968 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2969 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002970 TransformToNewDefs Transform(SemaRef);
2971
2972 auto NewLB = Transform.TransformExpr(LB);
2973 auto NewUB = Transform.TransformExpr(UB);
2974 if (NewLB.isInvalid() || NewUB.isInvalid())
2975 return Cond;
2976 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2977 Sema::AA_Converting,
2978 /*AllowExplicit=*/true);
2979 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2980 Sema::AA_Converting,
2981 /*AllowExplicit=*/true);
2982 if (NewLB.isInvalid() || NewUB.isInvalid())
2983 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002984 auto CondExpr = SemaRef.BuildBinOp(
2985 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2986 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002987 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002988 if (CondExpr.isUsable()) {
2989 CondExpr = SemaRef.PerformImplicitConversion(
2990 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2991 /*AllowExplicit=*/true);
2992 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002993 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2994 // Otherwise use original loop conditon and evaluate it in runtime.
2995 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2996}
2997
Alexander Musmana5f070a2014-10-01 06:03:56 +00002998/// \brief Build reference expression to the counter be used for codegen.
2999Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003000 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3001 DefaultLoc);
3002}
3003
3004Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3005 if (Var && !Var->isInvalidDecl()) {
3006 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003007 auto *PrivateVar =
3008 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3009 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003010 if (PrivateVar->isInvalidDecl())
3011 return nullptr;
3012 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3013 }
3014 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003015}
3016
3017/// \brief Build initization of the counter be used for codegen.
3018Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3019
3020/// \brief Build step of the counter be used for codegen.
3021Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3022
3023/// \brief Iteration space of a single for loop.
3024struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003025 /// \brief Condition of the loop.
3026 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003027 /// \brief This expression calculates the number of iterations in the loop.
3028 /// It is always possible to calculate it before starting the loop.
3029 Expr *NumIterations;
3030 /// \brief The loop counter variable.
3031 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003032 /// \brief Private loop counter variable.
3033 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003034 /// \brief This is initializer for the initial value of #CounterVar.
3035 Expr *CounterInit;
3036 /// \brief This is step for the #CounterVar used to generate its update:
3037 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3038 Expr *CounterStep;
3039 /// \brief Should step be subtracted?
3040 bool Subtract;
3041 /// \brief Source range of the loop init.
3042 SourceRange InitSrcRange;
3043 /// \brief Source range of the loop condition.
3044 SourceRange CondSrcRange;
3045 /// \brief Source range of the loop increment.
3046 SourceRange IncSrcRange;
3047};
3048
Alexey Bataev23b69422014-06-18 07:08:49 +00003049} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003050
Alexey Bataev9c821032015-04-30 04:23:23 +00003051void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3052 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3053 assert(Init && "Expected loop in canonical form.");
3054 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3055 if (CollapseIteration > 0 &&
3056 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3057 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3058 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3059 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3060 }
3061 DSAStack->setCollapseNumber(CollapseIteration - 1);
3062 }
3063}
3064
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003065/// \brief Called on a for stmt to check and extract its iteration space
3066/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003067static bool CheckOpenMPIterationSpace(
3068 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3069 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003070 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003071 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3072 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003073 // OpenMP [2.6, Canonical Loop Form]
3074 // for (init-expr; test-expr; incr-expr) structured-block
3075 auto For = dyn_cast_or_null<ForStmt>(S);
3076 if (!For) {
3077 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003078 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3079 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3080 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3081 if (NestedLoopCount > 1) {
3082 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3083 SemaRef.Diag(DSA.getConstructLoc(),
3084 diag::note_omp_collapse_ordered_expr)
3085 << 2 << CollapseLoopCountExpr->getSourceRange()
3086 << OrderedLoopCountExpr->getSourceRange();
3087 else if (CollapseLoopCountExpr)
3088 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3089 diag::note_omp_collapse_ordered_expr)
3090 << 0 << CollapseLoopCountExpr->getSourceRange();
3091 else
3092 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3093 diag::note_omp_collapse_ordered_expr)
3094 << 1 << OrderedLoopCountExpr->getSourceRange();
3095 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003096 return true;
3097 }
3098 assert(For->getBody());
3099
3100 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3101
3102 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003103 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003104 if (ISC.CheckInit(Init)) {
3105 return true;
3106 }
3107
3108 bool HasErrors = false;
3109
3110 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003111 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003112
3113 // OpenMP [2.6, Canonical Loop Form]
3114 // Var is one of the following:
3115 // A variable of signed or unsigned integer type.
3116 // For C++, a variable of a random access iterator type.
3117 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003118 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003119 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3120 !VarType->isPointerType() &&
3121 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3122 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3123 << SemaRef.getLangOpts().CPlusPlus;
3124 HasErrors = true;
3125 }
3126
Alexey Bataev4acb8592014-07-07 13:01:15 +00003127 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3128 // Construct
3129 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3130 // parallel for construct is (are) private.
3131 // The loop iteration variable in the associated for-loop of a simd construct
3132 // with just one associated for-loop is linear with a constant-linear-step
3133 // that is the increment of the associated for-loop.
3134 // Exclude loop var from the list of variables with implicitly defined data
3135 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003136 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003137
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003138 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3139 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003140 // The loop iteration variable in the associated for-loop of a simd construct
3141 // with just one associated for-loop may be listed in a linear clause with a
3142 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003143 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3144 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003145 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003146 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3147 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3148 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003149 auto PredeterminedCKind =
3150 isOpenMPSimdDirective(DKind)
3151 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3152 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003153 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003154 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00003155 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
3156 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003157 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
3158 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3159 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003160 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003161 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3162 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003163 if (DVar.RefExpr == nullptr)
3164 DVar.CKind = PredeterminedCKind;
3165 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003166 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003167 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003168 // Make the loop iteration variable private (for worksharing constructs),
3169 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003170 // lastprivate (for simd directives with several collapsed or ordered
3171 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003172 if (DVar.CKind == OMPC_unknown)
3173 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3174 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003175 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003176 }
3177
Alexey Bataev7ff55242014-06-19 09:13:45 +00003178 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003179
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003180 // Check test-expr.
3181 HasErrors |= ISC.CheckCond(For->getCond());
3182
3183 // Check incr-expr.
3184 HasErrors |= ISC.CheckInc(For->getInc());
3185
Alexander Musmana5f070a2014-10-01 06:03:56 +00003186 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003187 return HasErrors;
3188
Alexander Musmana5f070a2014-10-01 06:03:56 +00003189 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003190 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003191 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3192 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003193 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003194 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003195 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3196 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3197 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3198 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3199 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3200 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3201
Alexey Bataev62dbb972015-04-22 11:59:37 +00003202 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3203 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003204 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003205 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003206 ResultIterSpace.CounterInit == nullptr ||
3207 ResultIterSpace.CounterStep == nullptr);
3208
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003209 return HasErrors;
3210}
3211
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003212/// \brief Build 'VarRef = Start.
3213static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3214 ExprResult VarRef, ExprResult Start) {
3215 TransformToNewDefs Transform(SemaRef);
3216 // Build 'VarRef = Start.
3217 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3218 if (NewStart.isInvalid())
3219 return ExprError();
3220 NewStart = SemaRef.PerformImplicitConversion(
3221 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3222 Sema::AA_Converting,
3223 /*AllowExplicit=*/true);
3224 if (NewStart.isInvalid())
3225 return ExprError();
3226 NewStart = SemaRef.PerformImplicitConversion(
3227 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3228 /*AllowExplicit=*/true);
3229 if (!NewStart.isUsable())
3230 return ExprError();
3231
3232 auto Init =
3233 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3234 return Init;
3235}
3236
Alexander Musmana5f070a2014-10-01 06:03:56 +00003237/// \brief Build 'VarRef = Start + Iter * Step'.
3238static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3239 SourceLocation Loc, ExprResult VarRef,
3240 ExprResult Start, ExprResult Iter,
3241 ExprResult Step, bool Subtract) {
3242 // Add parentheses (for debugging purposes only).
3243 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3244 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3245 !Step.isUsable())
3246 return ExprError();
3247
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003248 TransformToNewDefs Transform(SemaRef);
3249 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3250 if (NewStep.isInvalid())
3251 return ExprError();
3252 NewStep = SemaRef.PerformImplicitConversion(
3253 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3254 Sema::AA_Converting,
3255 /*AllowExplicit=*/true);
3256 if (NewStep.isInvalid())
3257 return ExprError();
3258 ExprResult Update =
3259 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003260 if (!Update.isUsable())
3261 return ExprError();
3262
3263 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003264 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3265 if (NewStart.isInvalid())
3266 return ExprError();
3267 NewStart = SemaRef.PerformImplicitConversion(
3268 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3269 Sema::AA_Converting,
3270 /*AllowExplicit=*/true);
3271 if (NewStart.isInvalid())
3272 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003273 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003274 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003275 if (!Update.isUsable())
3276 return ExprError();
3277
3278 Update = SemaRef.PerformImplicitConversion(
3279 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3280 if (!Update.isUsable())
3281 return ExprError();
3282
3283 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3284 return Update;
3285}
3286
3287/// \brief Convert integer expression \a E to make it have at least \a Bits
3288/// bits.
3289static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3290 Sema &SemaRef) {
3291 if (E == nullptr)
3292 return ExprError();
3293 auto &C = SemaRef.Context;
3294 QualType OldType = E->getType();
3295 unsigned HasBits = C.getTypeSize(OldType);
3296 if (HasBits >= Bits)
3297 return ExprResult(E);
3298 // OK to convert to signed, because new type has more bits than old.
3299 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3300 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3301 true);
3302}
3303
3304/// \brief Check if the given expression \a E is a constant integer that fits
3305/// into \a Bits bits.
3306static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3307 if (E == nullptr)
3308 return false;
3309 llvm::APSInt Result;
3310 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3311 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3312 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003313}
3314
3315/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003316/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3317/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003318static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003319CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3320 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3321 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003322 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003323 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003324 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003325 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003326 // Found 'collapse' clause - calculate collapse number.
3327 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003328 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3329 NestedLoopCount += Result.getLimitedValue() - 1;
3330 }
3331 if (OrderedLoopCountExpr) {
3332 // Found 'ordered' clause - calculate collapse number.
3333 llvm::APSInt Result;
3334 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3335 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003336 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003337 // This is helper routine for loop directives (e.g., 'for', 'simd',
3338 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003339 SmallVector<LoopIterationSpace, 4> IterSpaces;
3340 IterSpaces.resize(NestedLoopCount);
3341 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003342 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003343 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003344 NestedLoopCount, CollapseLoopCountExpr,
3345 OrderedLoopCountExpr, VarsWithImplicitDSA,
3346 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003347 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003348 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003349 // OpenMP [2.8.1, simd construct, Restrictions]
3350 // All loops associated with the construct must be perfectly nested; that
3351 // is, there must be no intervening code nor any OpenMP directive between
3352 // any two loops.
3353 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003354 }
3355
Alexander Musmana5f070a2014-10-01 06:03:56 +00003356 Built.clear(/* size */ NestedLoopCount);
3357
3358 if (SemaRef.CurContext->isDependentContext())
3359 return NestedLoopCount;
3360
3361 // An example of what is generated for the following code:
3362 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003363 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003364 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003365 // for (k = 0; k < NK; ++k)
3366 // for (j = J0; j < NJ; j+=2) {
3367 // <loop body>
3368 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003369 //
3370 // We generate the code below.
3371 // Note: the loop body may be outlined in CodeGen.
3372 // Note: some counters may be C++ classes, operator- is used to find number of
3373 // iterations and operator+= to calculate counter value.
3374 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3375 // or i64 is currently supported).
3376 //
3377 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3378 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3379 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3380 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3381 // // similar updates for vars in clauses (e.g. 'linear')
3382 // <loop body (using local i and j)>
3383 // }
3384 // i = NI; // assign final values of counters
3385 // j = NJ;
3386 //
3387
3388 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3389 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003390 // Precondition tests if there is at least one iteration (all conditions are
3391 // true).
3392 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003393 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003394 ExprResult LastIteration32 = WidenIterationCount(
3395 32 /* Bits */, SemaRef.PerformImplicitConversion(
3396 N0->IgnoreImpCasts(), N0->getType(),
3397 Sema::AA_Converting, /*AllowExplicit=*/true)
3398 .get(),
3399 SemaRef);
3400 ExprResult LastIteration64 = WidenIterationCount(
3401 64 /* Bits */, SemaRef.PerformImplicitConversion(
3402 N0->IgnoreImpCasts(), N0->getType(),
3403 Sema::AA_Converting, /*AllowExplicit=*/true)
3404 .get(),
3405 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003406
3407 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3408 return NestedLoopCount;
3409
3410 auto &C = SemaRef.Context;
3411 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3412
3413 Scope *CurScope = DSA.getCurScope();
3414 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003415 if (PreCond.isUsable()) {
3416 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3417 PreCond.get(), IterSpaces[Cnt].PreCond);
3418 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003419 auto N = IterSpaces[Cnt].NumIterations;
3420 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3421 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003422 LastIteration32 = SemaRef.BuildBinOp(
3423 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3424 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3425 Sema::AA_Converting,
3426 /*AllowExplicit=*/true)
3427 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003428 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003429 LastIteration64 = SemaRef.BuildBinOp(
3430 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3431 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3432 Sema::AA_Converting,
3433 /*AllowExplicit=*/true)
3434 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003435 }
3436
3437 // Choose either the 32-bit or 64-bit version.
3438 ExprResult LastIteration = LastIteration64;
3439 if (LastIteration32.isUsable() &&
3440 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3441 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3442 FitsInto(
3443 32 /* Bits */,
3444 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3445 LastIteration64.get(), SemaRef)))
3446 LastIteration = LastIteration32;
3447
3448 if (!LastIteration.isUsable())
3449 return 0;
3450
3451 // Save the number of iterations.
3452 ExprResult NumIterations = LastIteration;
3453 {
3454 LastIteration = SemaRef.BuildBinOp(
3455 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3456 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3457 if (!LastIteration.isUsable())
3458 return 0;
3459 }
3460
3461 // Calculate the last iteration number beforehand instead of doing this on
3462 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3463 llvm::APSInt Result;
3464 bool IsConstant =
3465 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3466 ExprResult CalcLastIteration;
3467 if (!IsConstant) {
3468 SourceLocation SaveLoc;
3469 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003470 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003471 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003472 ExprResult SaveRef = buildDeclRefExpr(
3473 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003474 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3475 SaveRef.get(), LastIteration.get());
3476 LastIteration = SaveRef;
3477
3478 // Prepare SaveRef + 1.
3479 NumIterations = SemaRef.BuildBinOp(
3480 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3481 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3482 if (!NumIterations.isUsable())
3483 return 0;
3484 }
3485
3486 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3487
Alexander Musmanc6388682014-12-15 07:07:06 +00003488 QualType VType = LastIteration.get()->getType();
3489 // Build variables passed into runtime, nesessary for worksharing directives.
3490 ExprResult LB, UB, IL, ST, EUB;
3491 if (isOpenMPWorksharingDirective(DKind)) {
3492 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003493 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3494 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003495 SemaRef.AddInitializerToDecl(
3496 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3497 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3498
3499 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003500 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3501 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003502 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3503 /*DirectInit*/ false,
3504 /*TypeMayContainAuto*/ false);
3505
3506 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3507 // This will be used to implement clause 'lastprivate'.
3508 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003509 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3510 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003511 SemaRef.AddInitializerToDecl(
3512 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3513 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3514
3515 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003516 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3517 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003518 SemaRef.AddInitializerToDecl(
3519 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3520 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3521
3522 // Build expression: UB = min(UB, LastIteration)
3523 // It is nesessary for CodeGen of directives with static scheduling.
3524 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3525 UB.get(), LastIteration.get());
3526 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3527 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3528 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3529 CondOp.get());
3530 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3531 }
3532
3533 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003534 ExprResult IV;
3535 ExprResult Init;
3536 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003537 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3538 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003539 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3540 ? LB.get()
3541 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3542 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3543 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003544 }
3545
Alexander Musmanc6388682014-12-15 07:07:06 +00003546 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003547 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003548 ExprResult Cond =
3549 isOpenMPWorksharingDirective(DKind)
3550 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3551 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3552 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003553
3554 // Loop increment (IV = IV + 1)
3555 SourceLocation IncLoc;
3556 ExprResult Inc =
3557 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3558 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3559 if (!Inc.isUsable())
3560 return 0;
3561 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003562 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3563 if (!Inc.isUsable())
3564 return 0;
3565
3566 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3567 // Used for directives with static scheduling.
3568 ExprResult NextLB, NextUB;
3569 if (isOpenMPWorksharingDirective(DKind)) {
3570 // LB + ST
3571 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3572 if (!NextLB.isUsable())
3573 return 0;
3574 // LB = LB + ST
3575 NextLB =
3576 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3577 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3578 if (!NextLB.isUsable())
3579 return 0;
3580 // UB + ST
3581 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3582 if (!NextUB.isUsable())
3583 return 0;
3584 // UB = UB + ST
3585 NextUB =
3586 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3587 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3588 if (!NextUB.isUsable())
3589 return 0;
3590 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003591
3592 // Build updates and final values of the loop counters.
3593 bool HasErrors = false;
3594 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003595 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003596 Built.Updates.resize(NestedLoopCount);
3597 Built.Finals.resize(NestedLoopCount);
3598 {
3599 ExprResult Div;
3600 // Go from inner nested loop to outer.
3601 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3602 LoopIterationSpace &IS = IterSpaces[Cnt];
3603 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3604 // Build: Iter = (IV / Div) % IS.NumIters
3605 // where Div is product of previous iterations' IS.NumIters.
3606 ExprResult Iter;
3607 if (Div.isUsable()) {
3608 Iter =
3609 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3610 } else {
3611 Iter = IV;
3612 assert((Cnt == (int)NestedLoopCount - 1) &&
3613 "unusable div expected on first iteration only");
3614 }
3615
3616 if (Cnt != 0 && Iter.isUsable())
3617 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3618 IS.NumIterations);
3619 if (!Iter.isUsable()) {
3620 HasErrors = true;
3621 break;
3622 }
3623
Alexey Bataev39f915b82015-05-08 10:41:21 +00003624 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3625 auto *CounterVar = buildDeclRefExpr(
3626 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3627 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3628 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003629 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3630 IS.CounterInit);
3631 if (!Init.isUsable()) {
3632 HasErrors = true;
3633 break;
3634 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003635 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003636 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003637 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3638 if (!Update.isUsable()) {
3639 HasErrors = true;
3640 break;
3641 }
3642
3643 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3644 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003645 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003646 IS.NumIterations, IS.CounterStep, IS.Subtract);
3647 if (!Final.isUsable()) {
3648 HasErrors = true;
3649 break;
3650 }
3651
3652 // Build Div for the next iteration: Div <- Div * IS.NumIters
3653 if (Cnt != 0) {
3654 if (Div.isUnset())
3655 Div = IS.NumIterations;
3656 else
3657 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3658 IS.NumIterations);
3659
3660 // Add parentheses (for debugging purposes only).
3661 if (Div.isUsable())
3662 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3663 if (!Div.isUsable()) {
3664 HasErrors = true;
3665 break;
3666 }
3667 }
3668 if (!Update.isUsable() || !Final.isUsable()) {
3669 HasErrors = true;
3670 break;
3671 }
3672 // Save results
3673 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003674 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003675 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003676 Built.Updates[Cnt] = Update.get();
3677 Built.Finals[Cnt] = Final.get();
3678 }
3679 }
3680
3681 if (HasErrors)
3682 return 0;
3683
3684 // Save results
3685 Built.IterationVarRef = IV.get();
3686 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003687 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003688 Built.CalcLastIteration =
3689 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003690 Built.PreCond = PreCond.get();
3691 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003692 Built.Init = Init.get();
3693 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003694 Built.LB = LB.get();
3695 Built.UB = UB.get();
3696 Built.IL = IL.get();
3697 Built.ST = ST.get();
3698 Built.EUB = EUB.get();
3699 Built.NLB = NextLB.get();
3700 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003701
Alexey Bataevabfc0692014-06-25 06:52:00 +00003702 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003703}
3704
Alexey Bataev10e775f2015-07-30 11:36:16 +00003705static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003706 auto CollapseClauses =
3707 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3708 if (CollapseClauses.begin() != CollapseClauses.end())
3709 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003710 return nullptr;
3711}
3712
Alexey Bataev10e775f2015-07-30 11:36:16 +00003713static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003714 auto OrderedClauses =
3715 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3716 if (OrderedClauses.begin() != OrderedClauses.end())
3717 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003718 return nullptr;
3719}
3720
Alexey Bataev66b15b52015-08-21 11:14:16 +00003721static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3722 const Expr *Safelen) {
3723 llvm::APSInt SimdlenRes, SafelenRes;
3724 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3725 Simdlen->isInstantiationDependent() ||
3726 Simdlen->containsUnexpandedParameterPack())
3727 return false;
3728 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3729 Safelen->isInstantiationDependent() ||
3730 Safelen->containsUnexpandedParameterPack())
3731 return false;
3732 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3733 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3734 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3735 // If both simdlen and safelen clauses are specified, the value of the simdlen
3736 // parameter must be less than or equal to the value of the safelen parameter.
3737 if (SimdlenRes > SafelenRes) {
3738 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3739 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3740 return true;
3741 }
3742 return false;
3743}
3744
Alexey Bataev4acb8592014-07-07 13:01:15 +00003745StmtResult Sema::ActOnOpenMPSimdDirective(
3746 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3747 SourceLocation EndLoc,
3748 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003749 if (!AStmt)
3750 return StmtError();
3751
3752 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003753 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003754 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3755 // define the nested loops number.
3756 unsigned NestedLoopCount = CheckOpenMPLoop(
3757 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3758 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003759 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003760 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003761
Alexander Musmana5f070a2014-10-01 06:03:56 +00003762 assert((CurContext->isDependentContext() || B.builtAll()) &&
3763 "omp simd loop exprs were not built");
3764
Alexander Musman3276a272015-03-21 10:12:56 +00003765 if (!CurContext->isDependentContext()) {
3766 // Finalize the clauses that need pre-built expressions for CodeGen.
3767 for (auto C : Clauses) {
3768 if (auto LC = dyn_cast<OMPLinearClause>(C))
3769 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3770 B.NumIterations, *this, CurScope))
3771 return StmtError();
3772 }
3773 }
3774
Alexey Bataev66b15b52015-08-21 11:14:16 +00003775 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3776 // If both simdlen and safelen clauses are specified, the value of the simdlen
3777 // parameter must be less than or equal to the value of the safelen parameter.
3778 OMPSafelenClause *Safelen = nullptr;
3779 OMPSimdlenClause *Simdlen = nullptr;
3780 for (auto *Clause : Clauses) {
3781 if (Clause->getClauseKind() == OMPC_safelen)
3782 Safelen = cast<OMPSafelenClause>(Clause);
3783 else if (Clause->getClauseKind() == OMPC_simdlen)
3784 Simdlen = cast<OMPSimdlenClause>(Clause);
3785 if (Safelen && Simdlen)
3786 break;
3787 }
3788 if (Simdlen && Safelen &&
3789 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3790 Safelen->getSafelen()))
3791 return StmtError();
3792
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003793 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003794 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3795 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003796}
3797
Alexey Bataev4acb8592014-07-07 13:01:15 +00003798StmtResult Sema::ActOnOpenMPForDirective(
3799 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3800 SourceLocation EndLoc,
3801 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003802 if (!AStmt)
3803 return StmtError();
3804
3805 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003806 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003807 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3808 // define the nested loops number.
3809 unsigned NestedLoopCount = CheckOpenMPLoop(
3810 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3811 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003812 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003813 return StmtError();
3814
Alexander Musmana5f070a2014-10-01 06:03:56 +00003815 assert((CurContext->isDependentContext() || B.builtAll()) &&
3816 "omp for loop exprs were not built");
3817
Alexey Bataev54acd402015-08-04 11:18:19 +00003818 if (!CurContext->isDependentContext()) {
3819 // Finalize the clauses that need pre-built expressions for CodeGen.
3820 for (auto C : Clauses) {
3821 if (auto LC = dyn_cast<OMPLinearClause>(C))
3822 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3823 B.NumIterations, *this, CurScope))
3824 return StmtError();
3825 }
3826 }
3827
Alexey Bataevf29276e2014-06-18 04:14:57 +00003828 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003829 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00003830 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003831}
3832
Alexander Musmanf82886e2014-09-18 05:12:34 +00003833StmtResult Sema::ActOnOpenMPForSimdDirective(
3834 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3835 SourceLocation EndLoc,
3836 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003837 if (!AStmt)
3838 return StmtError();
3839
3840 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003841 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003842 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3843 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003844 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003845 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3846 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3847 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003848 if (NestedLoopCount == 0)
3849 return StmtError();
3850
Alexander Musmanc6388682014-12-15 07:07:06 +00003851 assert((CurContext->isDependentContext() || B.builtAll()) &&
3852 "omp for simd loop exprs were not built");
3853
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003854 if (!CurContext->isDependentContext()) {
3855 // Finalize the clauses that need pre-built expressions for CodeGen.
3856 for (auto C : Clauses) {
3857 if (auto LC = dyn_cast<OMPLinearClause>(C))
3858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3859 B.NumIterations, *this, CurScope))
3860 return StmtError();
3861 }
3862 }
3863
Alexey Bataev66b15b52015-08-21 11:14:16 +00003864 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3865 // If both simdlen and safelen clauses are specified, the value of the simdlen
3866 // parameter must be less than or equal to the value of the safelen parameter.
3867 OMPSafelenClause *Safelen = nullptr;
3868 OMPSimdlenClause *Simdlen = nullptr;
3869 for (auto *Clause : Clauses) {
3870 if (Clause->getClauseKind() == OMPC_safelen)
3871 Safelen = cast<OMPSafelenClause>(Clause);
3872 else if (Clause->getClauseKind() == OMPC_simdlen)
3873 Simdlen = cast<OMPSimdlenClause>(Clause);
3874 if (Safelen && Simdlen)
3875 break;
3876 }
3877 if (Simdlen && Safelen &&
3878 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3879 Safelen->getSafelen()))
3880 return StmtError();
3881
Alexander Musmanf82886e2014-09-18 05:12:34 +00003882 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003883 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3884 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003885}
3886
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003887StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3888 Stmt *AStmt,
3889 SourceLocation StartLoc,
3890 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003891 if (!AStmt)
3892 return StmtError();
3893
3894 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003895 auto BaseStmt = AStmt;
3896 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3897 BaseStmt = CS->getCapturedStmt();
3898 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3899 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003900 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003901 return StmtError();
3902 // All associated statements must be '#pragma omp section' except for
3903 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003904 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003905 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3906 if (SectionStmt)
3907 Diag(SectionStmt->getLocStart(),
3908 diag::err_omp_sections_substmt_not_section);
3909 return StmtError();
3910 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00003911 cast<OMPSectionDirective>(SectionStmt)
3912 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003913 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003914 } else {
3915 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3916 return StmtError();
3917 }
3918
3919 getCurFunction()->setHasBranchProtectedScope();
3920
Alexey Bataev25e5b442015-09-15 12:52:43 +00003921 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3922 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003923}
3924
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003925StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3926 SourceLocation StartLoc,
3927 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003928 if (!AStmt)
3929 return StmtError();
3930
3931 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003932
3933 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00003934 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003935
Alexey Bataev25e5b442015-09-15 12:52:43 +00003936 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
3937 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003938}
3939
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003940StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3941 Stmt *AStmt,
3942 SourceLocation StartLoc,
3943 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003944 if (!AStmt)
3945 return StmtError();
3946
3947 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00003948
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003949 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003950
Alexey Bataev3255bf32015-01-19 05:20:46 +00003951 // OpenMP [2.7.3, single Construct, Restrictions]
3952 // The copyprivate clause must not be used with the nowait clause.
3953 OMPClause *Nowait = nullptr;
3954 OMPClause *Copyprivate = nullptr;
3955 for (auto *Clause : Clauses) {
3956 if (Clause->getClauseKind() == OMPC_nowait)
3957 Nowait = Clause;
3958 else if (Clause->getClauseKind() == OMPC_copyprivate)
3959 Copyprivate = Clause;
3960 if (Copyprivate && Nowait) {
3961 Diag(Copyprivate->getLocStart(),
3962 diag::err_omp_single_copyprivate_with_nowait);
3963 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3964 return StmtError();
3965 }
3966 }
3967
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003968 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3969}
3970
Alexander Musman80c22892014-07-17 08:54:58 +00003971StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3972 SourceLocation StartLoc,
3973 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003974 if (!AStmt)
3975 return StmtError();
3976
3977 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00003978
3979 getCurFunction()->setHasBranchProtectedScope();
3980
3981 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3982}
3983
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003984StmtResult
3985Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3986 Stmt *AStmt, SourceLocation StartLoc,
3987 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003988 if (!AStmt)
3989 return StmtError();
3990
3991 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003992
3993 getCurFunction()->setHasBranchProtectedScope();
3994
3995 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3996 AStmt);
3997}
3998
Alexey Bataev4acb8592014-07-07 13:01:15 +00003999StmtResult Sema::ActOnOpenMPParallelForDirective(
4000 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4001 SourceLocation EndLoc,
4002 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004003 if (!AStmt)
4004 return StmtError();
4005
Alexey Bataev4acb8592014-07-07 13:01:15 +00004006 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4007 // 1.2.2 OpenMP Language Terminology
4008 // Structured block - An executable statement with a single entry at the
4009 // top and a single exit at the bottom.
4010 // The point of exit cannot be a branch out of the structured block.
4011 // longjmp() and throw() must not violate the entry/exit criteria.
4012 CS->getCapturedDecl()->setNothrow();
4013
Alexander Musmanc6388682014-12-15 07:07:06 +00004014 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004015 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4016 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004017 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004018 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4019 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4020 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004021 if (NestedLoopCount == 0)
4022 return StmtError();
4023
Alexander Musmana5f070a2014-10-01 06:03:56 +00004024 assert((CurContext->isDependentContext() || B.builtAll()) &&
4025 "omp parallel for loop exprs were not built");
4026
Alexey Bataev54acd402015-08-04 11:18:19 +00004027 if (!CurContext->isDependentContext()) {
4028 // Finalize the clauses that need pre-built expressions for CodeGen.
4029 for (auto C : Clauses) {
4030 if (auto LC = dyn_cast<OMPLinearClause>(C))
4031 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4032 B.NumIterations, *this, CurScope))
4033 return StmtError();
4034 }
4035 }
4036
Alexey Bataev4acb8592014-07-07 13:01:15 +00004037 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004038 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004039 NestedLoopCount, Clauses, AStmt, B,
4040 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004041}
4042
Alexander Musmane4e893b2014-09-23 09:33:00 +00004043StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4044 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4045 SourceLocation EndLoc,
4046 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004047 if (!AStmt)
4048 return StmtError();
4049
Alexander Musmane4e893b2014-09-23 09:33:00 +00004050 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4051 // 1.2.2 OpenMP Language Terminology
4052 // Structured block - An executable statement with a single entry at the
4053 // top and a single exit at the bottom.
4054 // The point of exit cannot be a branch out of the structured block.
4055 // longjmp() and throw() must not violate the entry/exit criteria.
4056 CS->getCapturedDecl()->setNothrow();
4057
Alexander Musmanc6388682014-12-15 07:07:06 +00004058 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004059 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4060 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004061 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004062 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4063 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4064 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004065 if (NestedLoopCount == 0)
4066 return StmtError();
4067
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004068 if (!CurContext->isDependentContext()) {
4069 // Finalize the clauses that need pre-built expressions for CodeGen.
4070 for (auto C : Clauses) {
4071 if (auto LC = dyn_cast<OMPLinearClause>(C))
4072 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4073 B.NumIterations, *this, CurScope))
4074 return StmtError();
4075 }
4076 }
4077
Alexey Bataev66b15b52015-08-21 11:14:16 +00004078 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4079 // If both simdlen and safelen clauses are specified, the value of the simdlen
4080 // parameter must be less than or equal to the value of the safelen parameter.
4081 OMPSafelenClause *Safelen = nullptr;
4082 OMPSimdlenClause *Simdlen = nullptr;
4083 for (auto *Clause : Clauses) {
4084 if (Clause->getClauseKind() == OMPC_safelen)
4085 Safelen = cast<OMPSafelenClause>(Clause);
4086 else if (Clause->getClauseKind() == OMPC_simdlen)
4087 Simdlen = cast<OMPSimdlenClause>(Clause);
4088 if (Safelen && Simdlen)
4089 break;
4090 }
4091 if (Simdlen && Safelen &&
4092 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4093 Safelen->getSafelen()))
4094 return StmtError();
4095
Alexander Musmane4e893b2014-09-23 09:33:00 +00004096 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004097 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004098 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004099}
4100
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004101StmtResult
4102Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4103 Stmt *AStmt, SourceLocation StartLoc,
4104 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004105 if (!AStmt)
4106 return StmtError();
4107
4108 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004109 auto BaseStmt = AStmt;
4110 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4111 BaseStmt = CS->getCapturedStmt();
4112 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4113 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004114 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004115 return StmtError();
4116 // All associated statements must be '#pragma omp section' except for
4117 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004118 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004119 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4120 if (SectionStmt)
4121 Diag(SectionStmt->getLocStart(),
4122 diag::err_omp_parallel_sections_substmt_not_section);
4123 return StmtError();
4124 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004125 cast<OMPSectionDirective>(SectionStmt)
4126 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004127 }
4128 } else {
4129 Diag(AStmt->getLocStart(),
4130 diag::err_omp_parallel_sections_not_compound_stmt);
4131 return StmtError();
4132 }
4133
4134 getCurFunction()->setHasBranchProtectedScope();
4135
Alexey Bataev25e5b442015-09-15 12:52:43 +00004136 return OMPParallelSectionsDirective::Create(
4137 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004138}
4139
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004140StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4141 Stmt *AStmt, SourceLocation StartLoc,
4142 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004143 if (!AStmt)
4144 return StmtError();
4145
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004146 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4147 // 1.2.2 OpenMP Language Terminology
4148 // Structured block - An executable statement with a single entry at the
4149 // top and a single exit at the bottom.
4150 // The point of exit cannot be a branch out of the structured block.
4151 // longjmp() and throw() must not violate the entry/exit criteria.
4152 CS->getCapturedDecl()->setNothrow();
4153
4154 getCurFunction()->setHasBranchProtectedScope();
4155
Alexey Bataev25e5b442015-09-15 12:52:43 +00004156 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4157 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004158}
4159
Alexey Bataev68446b72014-07-18 07:47:19 +00004160StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4161 SourceLocation EndLoc) {
4162 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4163}
4164
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004165StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4166 SourceLocation EndLoc) {
4167 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4168}
4169
Alexey Bataev2df347a2014-07-18 10:17:07 +00004170StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4171 SourceLocation EndLoc) {
4172 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4173}
4174
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004175StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4176 SourceLocation StartLoc,
4177 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004178 if (!AStmt)
4179 return StmtError();
4180
4181 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004182
4183 getCurFunction()->setHasBranchProtectedScope();
4184
4185 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4186}
4187
Alexey Bataev6125da92014-07-21 11:26:11 +00004188StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4189 SourceLocation StartLoc,
4190 SourceLocation EndLoc) {
4191 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4192 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4193}
4194
Alexey Bataev346265e2015-09-25 10:37:12 +00004195StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4196 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004197 SourceLocation StartLoc,
4198 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004199 if (!AStmt)
4200 return StmtError();
4201
4202 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004203
4204 getCurFunction()->setHasBranchProtectedScope();
4205
Alexey Bataev346265e2015-09-25 10:37:12 +00004206 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004207 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004208 for (auto *C: Clauses) {
4209 if (C->getClauseKind() == OMPC_threads)
4210 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004211 else if (C->getClauseKind() == OMPC_simd)
4212 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004213 }
4214
4215 // TODO: this must happen only if 'threads' clause specified or if no clauses
4216 // is specified.
4217 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4218 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4219 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4220 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4221 return StmtError();
4222 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004223 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4224 // OpenMP [2.8.1,simd Construct, Restrictions]
4225 // An ordered construct with the simd clause is the only OpenMP construct
4226 // that can appear in the simd region.
4227 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4228 return StmtError();
4229 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004230
4231 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004232}
4233
Alexey Bataev1d160b12015-03-13 12:27:31 +00004234namespace {
4235/// \brief Helper class for checking expression in 'omp atomic [update]'
4236/// construct.
4237class OpenMPAtomicUpdateChecker {
4238 /// \brief Error results for atomic update expressions.
4239 enum ExprAnalysisErrorCode {
4240 /// \brief A statement is not an expression statement.
4241 NotAnExpression,
4242 /// \brief Expression is not builtin binary or unary operation.
4243 NotABinaryOrUnaryExpression,
4244 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4245 NotAnUnaryIncDecExpression,
4246 /// \brief An expression is not of scalar type.
4247 NotAScalarType,
4248 /// \brief A binary operation is not an assignment operation.
4249 NotAnAssignmentOp,
4250 /// \brief RHS part of the binary operation is not a binary expression.
4251 NotABinaryExpression,
4252 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4253 /// expression.
4254 NotABinaryOperator,
4255 /// \brief RHS binary operation does not have reference to the updated LHS
4256 /// part.
4257 NotAnUpdateExpression,
4258 /// \brief No errors is found.
4259 NoError
4260 };
4261 /// \brief Reference to Sema.
4262 Sema &SemaRef;
4263 /// \brief A location for note diagnostics (when error is found).
4264 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004265 /// \brief 'x' lvalue part of the source atomic expression.
4266 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004267 /// \brief 'expr' rvalue part of the source atomic expression.
4268 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004269 /// \brief Helper expression of the form
4270 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4271 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4272 Expr *UpdateExpr;
4273 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4274 /// important for non-associative operations.
4275 bool IsXLHSInRHSPart;
4276 BinaryOperatorKind Op;
4277 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004278 /// \brief true if the source expression is a postfix unary operation, false
4279 /// if it is a prefix unary operation.
4280 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004281
4282public:
4283 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004284 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004285 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004286 /// \brief Check specified statement that it is suitable for 'atomic update'
4287 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004288 /// expression. If DiagId and NoteId == 0, then only check is performed
4289 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004290 /// \param DiagId Diagnostic which should be emitted if error is found.
4291 /// \param NoteId Diagnostic note for the main error message.
4292 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004293 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004294 /// \brief Return the 'x' lvalue part of the source atomic expression.
4295 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004296 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4297 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004298 /// \brief Return the update expression used in calculation of the updated
4299 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4300 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4301 Expr *getUpdateExpr() const { return UpdateExpr; }
4302 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4303 /// false otherwise.
4304 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4305
Alexey Bataevb78ca832015-04-01 03:33:17 +00004306 /// \brief true if the source expression is a postfix unary operation, false
4307 /// if it is a prefix unary operation.
4308 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4309
Alexey Bataev1d160b12015-03-13 12:27:31 +00004310private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004311 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4312 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004313};
4314} // namespace
4315
4316bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4317 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4318 ExprAnalysisErrorCode ErrorFound = NoError;
4319 SourceLocation ErrorLoc, NoteLoc;
4320 SourceRange ErrorRange, NoteRange;
4321 // Allowed constructs are:
4322 // x = x binop expr;
4323 // x = expr binop x;
4324 if (AtomicBinOp->getOpcode() == BO_Assign) {
4325 X = AtomicBinOp->getLHS();
4326 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4327 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4328 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4329 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4330 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004331 Op = AtomicInnerBinOp->getOpcode();
4332 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004333 auto *LHS = AtomicInnerBinOp->getLHS();
4334 auto *RHS = AtomicInnerBinOp->getRHS();
4335 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4336 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4337 /*Canonical=*/true);
4338 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4339 /*Canonical=*/true);
4340 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4341 /*Canonical=*/true);
4342 if (XId == LHSId) {
4343 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004344 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004345 } else if (XId == RHSId) {
4346 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004347 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004348 } else {
4349 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4350 ErrorRange = AtomicInnerBinOp->getSourceRange();
4351 NoteLoc = X->getExprLoc();
4352 NoteRange = X->getSourceRange();
4353 ErrorFound = NotAnUpdateExpression;
4354 }
4355 } else {
4356 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4357 ErrorRange = AtomicInnerBinOp->getSourceRange();
4358 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4359 NoteRange = SourceRange(NoteLoc, NoteLoc);
4360 ErrorFound = NotABinaryOperator;
4361 }
4362 } else {
4363 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4364 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4365 ErrorFound = NotABinaryExpression;
4366 }
4367 } else {
4368 ErrorLoc = AtomicBinOp->getExprLoc();
4369 ErrorRange = AtomicBinOp->getSourceRange();
4370 NoteLoc = AtomicBinOp->getOperatorLoc();
4371 NoteRange = SourceRange(NoteLoc, NoteLoc);
4372 ErrorFound = NotAnAssignmentOp;
4373 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004374 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004375 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4376 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4377 return true;
4378 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004379 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004380 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004381}
4382
4383bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4384 unsigned NoteId) {
4385 ExprAnalysisErrorCode ErrorFound = NoError;
4386 SourceLocation ErrorLoc, NoteLoc;
4387 SourceRange ErrorRange, NoteRange;
4388 // Allowed constructs are:
4389 // x++;
4390 // x--;
4391 // ++x;
4392 // --x;
4393 // x binop= expr;
4394 // x = x binop expr;
4395 // x = expr binop x;
4396 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4397 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4398 if (AtomicBody->getType()->isScalarType() ||
4399 AtomicBody->isInstantiationDependent()) {
4400 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4401 AtomicBody->IgnoreParenImpCasts())) {
4402 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004403 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004404 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004405 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004406 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004407 X = AtomicCompAssignOp->getLHS();
4408 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004409 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4410 AtomicBody->IgnoreParenImpCasts())) {
4411 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004412 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4413 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004414 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004415 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4416 // Check for Unary Operation
4417 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004418 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004419 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4420 OpLoc = AtomicUnaryOp->getOperatorLoc();
4421 X = AtomicUnaryOp->getSubExpr();
4422 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4423 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004424 } else {
4425 ErrorFound = NotAnUnaryIncDecExpression;
4426 ErrorLoc = AtomicUnaryOp->getExprLoc();
4427 ErrorRange = AtomicUnaryOp->getSourceRange();
4428 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4429 NoteRange = SourceRange(NoteLoc, NoteLoc);
4430 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004431 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004432 ErrorFound = NotABinaryOrUnaryExpression;
4433 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4434 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4435 }
4436 } else {
4437 ErrorFound = NotAScalarType;
4438 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4439 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4440 }
4441 } else {
4442 ErrorFound = NotAnExpression;
4443 NoteLoc = ErrorLoc = S->getLocStart();
4444 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4445 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004446 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004447 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4448 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4449 return true;
4450 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004451 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004452 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004453 // Build an update expression of form 'OpaqueValueExpr(x) binop
4454 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4455 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4456 auto *OVEX = new (SemaRef.getASTContext())
4457 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4458 auto *OVEExpr = new (SemaRef.getASTContext())
4459 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4460 auto Update =
4461 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4462 IsXLHSInRHSPart ? OVEExpr : OVEX);
4463 if (Update.isInvalid())
4464 return true;
4465 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4466 Sema::AA_Casting);
4467 if (Update.isInvalid())
4468 return true;
4469 UpdateExpr = Update.get();
4470 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004471 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004472}
4473
Alexey Bataev0162e452014-07-22 10:10:35 +00004474StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4475 Stmt *AStmt,
4476 SourceLocation StartLoc,
4477 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004478 if (!AStmt)
4479 return StmtError();
4480
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004481 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004482 // 1.2.2 OpenMP Language Terminology
4483 // Structured block - An executable statement with a single entry at the
4484 // top and a single exit at the bottom.
4485 // The point of exit cannot be a branch out of the structured block.
4486 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004487 OpenMPClauseKind AtomicKind = OMPC_unknown;
4488 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004489 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004490 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004491 C->getClauseKind() == OMPC_update ||
4492 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004493 if (AtomicKind != OMPC_unknown) {
4494 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4495 << SourceRange(C->getLocStart(), C->getLocEnd());
4496 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4497 << getOpenMPClauseName(AtomicKind);
4498 } else {
4499 AtomicKind = C->getClauseKind();
4500 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004501 }
4502 }
4503 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004504
Alexey Bataev459dec02014-07-24 06:46:57 +00004505 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004506 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4507 Body = EWC->getSubExpr();
4508
Alexey Bataev62cec442014-11-18 10:14:22 +00004509 Expr *X = nullptr;
4510 Expr *V = nullptr;
4511 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004512 Expr *UE = nullptr;
4513 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004514 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004515 // OpenMP [2.12.6, atomic Construct]
4516 // In the next expressions:
4517 // * x and v (as applicable) are both l-value expressions with scalar type.
4518 // * During the execution of an atomic region, multiple syntactic
4519 // occurrences of x must designate the same storage location.
4520 // * Neither of v and expr (as applicable) may access the storage location
4521 // designated by x.
4522 // * Neither of x and expr (as applicable) may access the storage location
4523 // designated by v.
4524 // * expr is an expression with scalar type.
4525 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4526 // * binop, binop=, ++, and -- are not overloaded operators.
4527 // * The expression x binop expr must be numerically equivalent to x binop
4528 // (expr). This requirement is satisfied if the operators in expr have
4529 // precedence greater than binop, or by using parentheses around expr or
4530 // subexpressions of expr.
4531 // * The expression expr binop x must be numerically equivalent to (expr)
4532 // binop x. This requirement is satisfied if the operators in expr have
4533 // precedence equal to or greater than binop, or by using parentheses around
4534 // expr or subexpressions of expr.
4535 // * For forms that allow multiple occurrences of x, the number of times
4536 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004537 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004538 enum {
4539 NotAnExpression,
4540 NotAnAssignmentOp,
4541 NotAScalarType,
4542 NotAnLValue,
4543 NoError
4544 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004545 SourceLocation ErrorLoc, NoteLoc;
4546 SourceRange ErrorRange, NoteRange;
4547 // If clause is read:
4548 // v = x;
4549 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4550 auto AtomicBinOp =
4551 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4552 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4553 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4554 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4555 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4556 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4557 if (!X->isLValue() || !V->isLValue()) {
4558 auto NotLValueExpr = X->isLValue() ? V : X;
4559 ErrorFound = NotAnLValue;
4560 ErrorLoc = AtomicBinOp->getExprLoc();
4561 ErrorRange = AtomicBinOp->getSourceRange();
4562 NoteLoc = NotLValueExpr->getExprLoc();
4563 NoteRange = NotLValueExpr->getSourceRange();
4564 }
4565 } else if (!X->isInstantiationDependent() ||
4566 !V->isInstantiationDependent()) {
4567 auto NotScalarExpr =
4568 (X->isInstantiationDependent() || X->getType()->isScalarType())
4569 ? V
4570 : X;
4571 ErrorFound = NotAScalarType;
4572 ErrorLoc = AtomicBinOp->getExprLoc();
4573 ErrorRange = AtomicBinOp->getSourceRange();
4574 NoteLoc = NotScalarExpr->getExprLoc();
4575 NoteRange = NotScalarExpr->getSourceRange();
4576 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004577 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004578 ErrorFound = NotAnAssignmentOp;
4579 ErrorLoc = AtomicBody->getExprLoc();
4580 ErrorRange = AtomicBody->getSourceRange();
4581 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4582 : AtomicBody->getExprLoc();
4583 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4584 : AtomicBody->getSourceRange();
4585 }
4586 } else {
4587 ErrorFound = NotAnExpression;
4588 NoteLoc = ErrorLoc = Body->getLocStart();
4589 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004590 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004591 if (ErrorFound != NoError) {
4592 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4593 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004594 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4595 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004596 return StmtError();
4597 } else if (CurContext->isDependentContext())
4598 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004599 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004600 enum {
4601 NotAnExpression,
4602 NotAnAssignmentOp,
4603 NotAScalarType,
4604 NotAnLValue,
4605 NoError
4606 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004607 SourceLocation ErrorLoc, NoteLoc;
4608 SourceRange ErrorRange, NoteRange;
4609 // If clause is write:
4610 // x = expr;
4611 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4612 auto AtomicBinOp =
4613 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4614 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004615 X = AtomicBinOp->getLHS();
4616 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004617 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4618 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4619 if (!X->isLValue()) {
4620 ErrorFound = NotAnLValue;
4621 ErrorLoc = AtomicBinOp->getExprLoc();
4622 ErrorRange = AtomicBinOp->getSourceRange();
4623 NoteLoc = X->getExprLoc();
4624 NoteRange = X->getSourceRange();
4625 }
4626 } else if (!X->isInstantiationDependent() ||
4627 !E->isInstantiationDependent()) {
4628 auto NotScalarExpr =
4629 (X->isInstantiationDependent() || X->getType()->isScalarType())
4630 ? E
4631 : X;
4632 ErrorFound = NotAScalarType;
4633 ErrorLoc = AtomicBinOp->getExprLoc();
4634 ErrorRange = AtomicBinOp->getSourceRange();
4635 NoteLoc = NotScalarExpr->getExprLoc();
4636 NoteRange = NotScalarExpr->getSourceRange();
4637 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004638 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004639 ErrorFound = NotAnAssignmentOp;
4640 ErrorLoc = AtomicBody->getExprLoc();
4641 ErrorRange = AtomicBody->getSourceRange();
4642 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4643 : AtomicBody->getExprLoc();
4644 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4645 : AtomicBody->getSourceRange();
4646 }
4647 } else {
4648 ErrorFound = NotAnExpression;
4649 NoteLoc = ErrorLoc = Body->getLocStart();
4650 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004651 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004652 if (ErrorFound != NoError) {
4653 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4654 << ErrorRange;
4655 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4656 << NoteRange;
4657 return StmtError();
4658 } else if (CurContext->isDependentContext())
4659 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004660 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004661 // If clause is update:
4662 // x++;
4663 // x--;
4664 // ++x;
4665 // --x;
4666 // x binop= expr;
4667 // x = x binop expr;
4668 // x = expr binop x;
4669 OpenMPAtomicUpdateChecker Checker(*this);
4670 if (Checker.checkStatement(
4671 Body, (AtomicKind == OMPC_update)
4672 ? diag::err_omp_atomic_update_not_expression_statement
4673 : diag::err_omp_atomic_not_expression_statement,
4674 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004675 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004676 if (!CurContext->isDependentContext()) {
4677 E = Checker.getExpr();
4678 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004679 UE = Checker.getUpdateExpr();
4680 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004681 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004682 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004683 enum {
4684 NotAnAssignmentOp,
4685 NotACompoundStatement,
4686 NotTwoSubstatements,
4687 NotASpecificExpression,
4688 NoError
4689 } ErrorFound = NoError;
4690 SourceLocation ErrorLoc, NoteLoc;
4691 SourceRange ErrorRange, NoteRange;
4692 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4693 // If clause is a capture:
4694 // v = x++;
4695 // v = x--;
4696 // v = ++x;
4697 // v = --x;
4698 // v = x binop= expr;
4699 // v = x = x binop expr;
4700 // v = x = expr binop x;
4701 auto *AtomicBinOp =
4702 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4703 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4704 V = AtomicBinOp->getLHS();
4705 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4706 OpenMPAtomicUpdateChecker Checker(*this);
4707 if (Checker.checkStatement(
4708 Body, diag::err_omp_atomic_capture_not_expression_statement,
4709 diag::note_omp_atomic_update))
4710 return StmtError();
4711 E = Checker.getExpr();
4712 X = Checker.getX();
4713 UE = Checker.getUpdateExpr();
4714 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4715 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004716 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004717 ErrorLoc = AtomicBody->getExprLoc();
4718 ErrorRange = AtomicBody->getSourceRange();
4719 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4720 : AtomicBody->getExprLoc();
4721 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4722 : AtomicBody->getSourceRange();
4723 ErrorFound = NotAnAssignmentOp;
4724 }
4725 if (ErrorFound != NoError) {
4726 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4727 << ErrorRange;
4728 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4729 return StmtError();
4730 } else if (CurContext->isDependentContext()) {
4731 UE = V = E = X = nullptr;
4732 }
4733 } else {
4734 // If clause is a capture:
4735 // { v = x; x = expr; }
4736 // { v = x; x++; }
4737 // { v = x; x--; }
4738 // { v = x; ++x; }
4739 // { v = x; --x; }
4740 // { v = x; x binop= expr; }
4741 // { v = x; x = x binop expr; }
4742 // { v = x; x = expr binop x; }
4743 // { x++; v = x; }
4744 // { x--; v = x; }
4745 // { ++x; v = x; }
4746 // { --x; v = x; }
4747 // { x binop= expr; v = x; }
4748 // { x = x binop expr; v = x; }
4749 // { x = expr binop x; v = x; }
4750 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4751 // Check that this is { expr1; expr2; }
4752 if (CS->size() == 2) {
4753 auto *First = CS->body_front();
4754 auto *Second = CS->body_back();
4755 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4756 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4757 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4758 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4759 // Need to find what subexpression is 'v' and what is 'x'.
4760 OpenMPAtomicUpdateChecker Checker(*this);
4761 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4762 BinaryOperator *BinOp = nullptr;
4763 if (IsUpdateExprFound) {
4764 BinOp = dyn_cast<BinaryOperator>(First);
4765 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4766 }
4767 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4768 // { v = x; x++; }
4769 // { v = x; x--; }
4770 // { v = x; ++x; }
4771 // { v = x; --x; }
4772 // { v = x; x binop= expr; }
4773 // { v = x; x = x binop expr; }
4774 // { v = x; x = expr binop x; }
4775 // Check that the first expression has form v = x.
4776 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4777 llvm::FoldingSetNodeID XId, PossibleXId;
4778 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4779 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4780 IsUpdateExprFound = XId == PossibleXId;
4781 if (IsUpdateExprFound) {
4782 V = BinOp->getLHS();
4783 X = Checker.getX();
4784 E = Checker.getExpr();
4785 UE = Checker.getUpdateExpr();
4786 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004787 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004788 }
4789 }
4790 if (!IsUpdateExprFound) {
4791 IsUpdateExprFound = !Checker.checkStatement(First);
4792 BinOp = nullptr;
4793 if (IsUpdateExprFound) {
4794 BinOp = dyn_cast<BinaryOperator>(Second);
4795 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4796 }
4797 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4798 // { x++; v = x; }
4799 // { x--; v = x; }
4800 // { ++x; v = x; }
4801 // { --x; v = x; }
4802 // { x binop= expr; v = x; }
4803 // { x = x binop expr; v = x; }
4804 // { x = expr binop x; v = x; }
4805 // Check that the second expression has form v = x.
4806 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4807 llvm::FoldingSetNodeID XId, PossibleXId;
4808 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4809 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4810 IsUpdateExprFound = XId == PossibleXId;
4811 if (IsUpdateExprFound) {
4812 V = BinOp->getLHS();
4813 X = Checker.getX();
4814 E = Checker.getExpr();
4815 UE = Checker.getUpdateExpr();
4816 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004817 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004818 }
4819 }
4820 }
4821 if (!IsUpdateExprFound) {
4822 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00004823 auto *FirstExpr = dyn_cast<Expr>(First);
4824 auto *SecondExpr = dyn_cast<Expr>(Second);
4825 if (!FirstExpr || !SecondExpr ||
4826 !(FirstExpr->isInstantiationDependent() ||
4827 SecondExpr->isInstantiationDependent())) {
4828 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4829 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004830 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00004831 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4832 : First->getLocStart();
4833 NoteRange = ErrorRange = FirstBinOp
4834 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00004835 : SourceRange(ErrorLoc, ErrorLoc);
4836 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004837 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4838 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4839 ErrorFound = NotAnAssignmentOp;
4840 NoteLoc = ErrorLoc = SecondBinOp
4841 ? SecondBinOp->getOperatorLoc()
4842 : Second->getLocStart();
4843 NoteRange = ErrorRange =
4844 SecondBinOp ? SecondBinOp->getSourceRange()
4845 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00004846 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004847 auto *PossibleXRHSInFirst =
4848 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4849 auto *PossibleXLHSInSecond =
4850 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4851 llvm::FoldingSetNodeID X1Id, X2Id;
4852 PossibleXRHSInFirst->Profile(X1Id, Context,
4853 /*Canonical=*/true);
4854 PossibleXLHSInSecond->Profile(X2Id, Context,
4855 /*Canonical=*/true);
4856 IsUpdateExprFound = X1Id == X2Id;
4857 if (IsUpdateExprFound) {
4858 V = FirstBinOp->getLHS();
4859 X = SecondBinOp->getLHS();
4860 E = SecondBinOp->getRHS();
4861 UE = nullptr;
4862 IsXLHSInRHSPart = false;
4863 IsPostfixUpdate = true;
4864 } else {
4865 ErrorFound = NotASpecificExpression;
4866 ErrorLoc = FirstBinOp->getExprLoc();
4867 ErrorRange = FirstBinOp->getSourceRange();
4868 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4869 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4870 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004871 }
4872 }
4873 }
4874 }
4875 } else {
4876 NoteLoc = ErrorLoc = Body->getLocStart();
4877 NoteRange = ErrorRange =
4878 SourceRange(Body->getLocStart(), Body->getLocStart());
4879 ErrorFound = NotTwoSubstatements;
4880 }
4881 } else {
4882 NoteLoc = ErrorLoc = Body->getLocStart();
4883 NoteRange = ErrorRange =
4884 SourceRange(Body->getLocStart(), Body->getLocStart());
4885 ErrorFound = NotACompoundStatement;
4886 }
4887 if (ErrorFound != NoError) {
4888 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4889 << ErrorRange;
4890 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4891 return StmtError();
4892 } else if (CurContext->isDependentContext()) {
4893 UE = V = E = X = nullptr;
4894 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004895 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004896 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004897
4898 getCurFunction()->setHasBranchProtectedScope();
4899
Alexey Bataev62cec442014-11-18 10:14:22 +00004900 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004901 X, V, E, UE, IsXLHSInRHSPart,
4902 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004903}
4904
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004905StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4906 Stmt *AStmt,
4907 SourceLocation StartLoc,
4908 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004909 if (!AStmt)
4910 return StmtError();
4911
4912 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004913
Alexey Bataev13314bf2014-10-09 04:18:56 +00004914 // OpenMP [2.16, Nesting of Regions]
4915 // If specified, a teams construct must be contained within a target
4916 // construct. That target construct must contain no statements or directives
4917 // outside of the teams construct.
4918 if (DSAStack->hasInnerTeamsRegion()) {
4919 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4920 bool OMPTeamsFound = true;
4921 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4922 auto I = CS->body_begin();
4923 while (I != CS->body_end()) {
4924 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4925 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4926 OMPTeamsFound = false;
4927 break;
4928 }
4929 ++I;
4930 }
4931 assert(I != CS->body_end() && "Not found statement");
4932 S = *I;
4933 }
4934 if (!OMPTeamsFound) {
4935 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4936 Diag(DSAStack->getInnerTeamsRegionLoc(),
4937 diag::note_omp_nested_teams_construct_here);
4938 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4939 << isa<OMPExecutableDirective>(S);
4940 return StmtError();
4941 }
4942 }
4943
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004944 getCurFunction()->setHasBranchProtectedScope();
4945
4946 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4947}
4948
Michael Wong65f367f2015-07-21 13:44:28 +00004949StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4950 Stmt *AStmt,
4951 SourceLocation StartLoc,
4952 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004953 if (!AStmt)
4954 return StmtError();
4955
4956 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4957
Michael Wong65f367f2015-07-21 13:44:28 +00004958 getCurFunction()->setHasBranchProtectedScope();
4959
4960 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4961 AStmt);
4962}
4963
Alexey Bataev13314bf2014-10-09 04:18:56 +00004964StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4965 Stmt *AStmt, SourceLocation StartLoc,
4966 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004967 if (!AStmt)
4968 return StmtError();
4969
Alexey Bataev13314bf2014-10-09 04:18:56 +00004970 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4971 // 1.2.2 OpenMP Language Terminology
4972 // Structured block - An executable statement with a single entry at the
4973 // top and a single exit at the bottom.
4974 // The point of exit cannot be a branch out of the structured block.
4975 // longjmp() and throw() must not violate the entry/exit criteria.
4976 CS->getCapturedDecl()->setNothrow();
4977
4978 getCurFunction()->setHasBranchProtectedScope();
4979
4980 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4981}
4982
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004983StmtResult
4984Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4985 SourceLocation EndLoc,
4986 OpenMPDirectiveKind CancelRegion) {
4987 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4988 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4989 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4990 << getOpenMPDirectiveName(CancelRegion);
4991 return StmtError();
4992 }
4993 if (DSAStack->isParentNowaitRegion()) {
4994 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4995 return StmtError();
4996 }
4997 if (DSAStack->isParentOrderedRegion()) {
4998 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4999 return StmtError();
5000 }
5001 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5002 CancelRegion);
5003}
5004
Alexey Bataev87933c72015-09-18 08:07:34 +00005005StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5006 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005007 SourceLocation EndLoc,
5008 OpenMPDirectiveKind CancelRegion) {
5009 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5010 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5011 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5012 << getOpenMPDirectiveName(CancelRegion);
5013 return StmtError();
5014 }
5015 if (DSAStack->isParentNowaitRegion()) {
5016 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5017 return StmtError();
5018 }
5019 if (DSAStack->isParentOrderedRegion()) {
5020 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5021 return StmtError();
5022 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005023 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005024 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5025 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005026}
5027
Alexey Bataeved09d242014-05-28 05:53:51 +00005028OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005029 SourceLocation StartLoc,
5030 SourceLocation LParenLoc,
5031 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005032 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005033 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005034 case OMPC_final:
5035 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5036 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005037 case OMPC_num_threads:
5038 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5039 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005040 case OMPC_safelen:
5041 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5042 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005043 case OMPC_simdlen:
5044 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5045 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005046 case OMPC_collapse:
5047 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5048 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005049 case OMPC_ordered:
5050 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5051 break;
Michael Wonge710d542015-08-07 16:16:36 +00005052 case OMPC_device:
5053 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5054 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005055 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005056 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005057 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005058 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005059 case OMPC_private:
5060 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005061 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005062 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005063 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005064 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005065 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005066 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005067 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005068 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005069 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005070 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005071 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005072 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005073 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005074 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005075 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005076 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005077 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005078 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005079 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005080 case OMPC_simd:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005081 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005082 llvm_unreachable("Clause is not allowed.");
5083 }
5084 return Res;
5085}
5086
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005087OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5088 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005089 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005090 SourceLocation NameModifierLoc,
5091 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005092 SourceLocation EndLoc) {
5093 Expr *ValExpr = Condition;
5094 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5095 !Condition->isInstantiationDependent() &&
5096 !Condition->containsUnexpandedParameterPack()) {
5097 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005098 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005099 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005100 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005101
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005102 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005103 }
5104
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005105 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5106 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005107}
5108
Alexey Bataev3778b602014-07-17 07:32:53 +00005109OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5110 SourceLocation StartLoc,
5111 SourceLocation LParenLoc,
5112 SourceLocation EndLoc) {
5113 Expr *ValExpr = Condition;
5114 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5115 !Condition->isInstantiationDependent() &&
5116 !Condition->containsUnexpandedParameterPack()) {
5117 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5118 Condition->getExprLoc(), Condition);
5119 if (Val.isInvalid())
5120 return nullptr;
5121
5122 ValExpr = Val.get();
5123 }
5124
5125 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5126}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005127ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5128 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005129 if (!Op)
5130 return ExprError();
5131
5132 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5133 public:
5134 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005135 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005136 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5137 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005138 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5139 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005140 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5141 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005142 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5143 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005144 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5145 QualType T,
5146 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005147 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5148 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005149 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5150 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005151 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005152 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005153 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005154 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5155 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005156 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5157 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005158 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5159 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005160 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005161 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005162 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005163 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5164 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005165 llvm_unreachable("conversion functions are permitted");
5166 }
5167 } ConvertDiagnoser;
5168 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5169}
5170
5171OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5172 SourceLocation StartLoc,
5173 SourceLocation LParenLoc,
5174 SourceLocation EndLoc) {
5175 Expr *ValExpr = NumThreads;
5176 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00005177 !NumThreads->containsUnexpandedParameterPack()) {
5178 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
5179 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005180 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00005181 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005182 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005183
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005184 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00005185
5186 // OpenMP [2.5, Restrictions]
5187 // The num_threads expression must evaluate to a positive integer value.
5188 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00005189 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
5190 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005191 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
5192 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005193 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005194 }
5195 }
5196
Alexey Bataeved09d242014-05-28 05:53:51 +00005197 return new (Context)
5198 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005199}
5200
Alexey Bataev62c87d22014-03-21 04:51:18 +00005201ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5202 OpenMPClauseKind CKind) {
5203 if (!E)
5204 return ExprError();
5205 if (E->isValueDependent() || E->isTypeDependent() ||
5206 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005207 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005208 llvm::APSInt Result;
5209 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5210 if (ICE.isInvalid())
5211 return ExprError();
5212 if (!Result.isStrictlyPositive()) {
5213 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
5214 << getOpenMPClauseName(CKind) << E->getSourceRange();
5215 return ExprError();
5216 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005217 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5218 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5219 << E->getSourceRange();
5220 return ExprError();
5221 }
Alexey Bataev9c821032015-04-30 04:23:23 +00005222 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005223 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5224 Result.getExtValue());
5225 } else if (CKind == OMPC_ordered) {
5226 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5227 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00005228 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00005229 return ICE;
5230}
5231
5232OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5233 SourceLocation LParenLoc,
5234 SourceLocation EndLoc) {
5235 // OpenMP [2.8.1, simd construct, Description]
5236 // The parameter of the safelen clause must be a constant
5237 // positive integer expression.
5238 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5239 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005240 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005241 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005242 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005243}
5244
Alexey Bataev66b15b52015-08-21 11:14:16 +00005245OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5246 SourceLocation LParenLoc,
5247 SourceLocation EndLoc) {
5248 // OpenMP [2.8.1, simd construct, Description]
5249 // The parameter of the simdlen clause must be a constant
5250 // positive integer expression.
5251 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5252 if (Simdlen.isInvalid())
5253 return nullptr;
5254 return new (Context)
5255 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5256}
5257
Alexander Musman64d33f12014-06-04 07:53:32 +00005258OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5259 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005260 SourceLocation LParenLoc,
5261 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005262 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005263 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005264 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005265 // The parameter of the collapse clause must be a constant
5266 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005267 ExprResult NumForLoopsResult =
5268 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5269 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005270 return nullptr;
5271 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005272 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005273}
5274
Alexey Bataev10e775f2015-07-30 11:36:16 +00005275OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5276 SourceLocation EndLoc,
5277 SourceLocation LParenLoc,
5278 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005279 // OpenMP [2.7.1, loop construct, Description]
5280 // OpenMP [2.8.1, simd construct, Description]
5281 // OpenMP [2.9.6, distribute construct, Description]
5282 // The parameter of the ordered clause must be a constant
5283 // positive integer expression if any.
5284 if (NumForLoops && LParenLoc.isValid()) {
5285 ExprResult NumForLoopsResult =
5286 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5287 if (NumForLoopsResult.isInvalid())
5288 return nullptr;
5289 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005290 } else
5291 NumForLoops = nullptr;
5292 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005293 return new (Context)
5294 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5295}
5296
Alexey Bataeved09d242014-05-28 05:53:51 +00005297OMPClause *Sema::ActOnOpenMPSimpleClause(
5298 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5299 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005300 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005301 switch (Kind) {
5302 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005303 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005304 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5305 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005306 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005307 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005308 Res = ActOnOpenMPProcBindClause(
5309 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5310 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005311 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005312 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005313 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005314 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005315 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005316 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005317 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005318 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005319 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005320 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005321 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005322 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005323 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005324 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005325 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005326 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005327 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005328 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005329 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005330 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005331 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005332 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005333 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005334 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005335 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005336 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005337 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005338 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005339 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005340 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005341 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005342 case OMPC_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005343 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005344 llvm_unreachable("Clause is not allowed.");
5345 }
5346 return Res;
5347}
5348
5349OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5350 SourceLocation KindKwLoc,
5351 SourceLocation StartLoc,
5352 SourceLocation LParenLoc,
5353 SourceLocation EndLoc) {
5354 if (Kind == OMPC_DEFAULT_unknown) {
5355 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005356 static_assert(OMPC_DEFAULT_unknown > 0,
5357 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005358 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005359 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005360 Values += "'";
5361 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5362 Values += "'";
5363 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005364 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005365 Values += " or ";
5366 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005367 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005368 break;
5369 default:
5370 Values += Sep;
5371 break;
5372 }
5373 }
5374 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005375 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005376 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005377 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005378 switch (Kind) {
5379 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005380 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005381 break;
5382 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005383 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005384 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005385 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005386 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005387 break;
5388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005389 return new (Context)
5390 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005391}
5392
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005393OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5394 SourceLocation KindKwLoc,
5395 SourceLocation StartLoc,
5396 SourceLocation LParenLoc,
5397 SourceLocation EndLoc) {
5398 if (Kind == OMPC_PROC_BIND_unknown) {
5399 std::string Values;
5400 std::string Sep(", ");
5401 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5402 Values += "'";
5403 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5404 Values += "'";
5405 switch (i) {
5406 case OMPC_PROC_BIND_unknown - 2:
5407 Values += " or ";
5408 break;
5409 case OMPC_PROC_BIND_unknown - 1:
5410 break;
5411 default:
5412 Values += Sep;
5413 break;
5414 }
5415 }
5416 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005417 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005418 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005419 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005420 return new (Context)
5421 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005422}
5423
Alexey Bataev56dafe82014-06-20 07:16:17 +00005424OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5425 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5426 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005427 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005428 SourceLocation EndLoc) {
5429 OMPClause *Res = nullptr;
5430 switch (Kind) {
5431 case OMPC_schedule:
5432 Res = ActOnOpenMPScheduleClause(
5433 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005434 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005435 break;
5436 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005437 Res =
5438 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5439 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5440 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005441 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005442 case OMPC_num_threads:
5443 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005444 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005445 case OMPC_collapse:
5446 case OMPC_default:
5447 case OMPC_proc_bind:
5448 case OMPC_private:
5449 case OMPC_firstprivate:
5450 case OMPC_lastprivate:
5451 case OMPC_shared:
5452 case OMPC_reduction:
5453 case OMPC_linear:
5454 case OMPC_aligned:
5455 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005456 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005457 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005458 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005459 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005460 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005461 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005462 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005463 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005464 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005465 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005466 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005467 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005468 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005469 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005470 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005471 case OMPC_simd:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005472 case OMPC_unknown:
5473 llvm_unreachable("Clause is not allowed.");
5474 }
5475 return Res;
5476}
5477
5478OMPClause *Sema::ActOnOpenMPScheduleClause(
5479 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5480 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5481 SourceLocation EndLoc) {
5482 if (Kind == OMPC_SCHEDULE_unknown) {
5483 std::string Values;
5484 std::string Sep(", ");
5485 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5486 Values += "'";
5487 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5488 Values += "'";
5489 switch (i) {
5490 case OMPC_SCHEDULE_unknown - 2:
5491 Values += " or ";
5492 break;
5493 case OMPC_SCHEDULE_unknown - 1:
5494 break;
5495 default:
5496 Values += Sep;
5497 break;
5498 }
5499 }
5500 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5501 << Values << getOpenMPClauseName(OMPC_schedule);
5502 return nullptr;
5503 }
5504 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005505 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005506 if (ChunkSize) {
5507 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5508 !ChunkSize->isInstantiationDependent() &&
5509 !ChunkSize->containsUnexpandedParameterPack()) {
5510 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5511 ExprResult Val =
5512 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5513 if (Val.isInvalid())
5514 return nullptr;
5515
5516 ValExpr = Val.get();
5517
5518 // OpenMP [2.7.1, Restrictions]
5519 // chunk_size must be a loop invariant integer expression with a positive
5520 // value.
5521 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005522 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5523 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5524 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5525 << "schedule" << ChunkSize->getSourceRange();
5526 return nullptr;
5527 }
5528 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5529 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5530 ChunkSize->getType(), ".chunk.");
5531 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5532 ChunkSize->getExprLoc(),
5533 /*RefersToCapture=*/true);
5534 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005535 }
5536 }
5537 }
5538
5539 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005540 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005541}
5542
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005543OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5544 SourceLocation StartLoc,
5545 SourceLocation EndLoc) {
5546 OMPClause *Res = nullptr;
5547 switch (Kind) {
5548 case OMPC_ordered:
5549 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5550 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005551 case OMPC_nowait:
5552 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5553 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005554 case OMPC_untied:
5555 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5556 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005557 case OMPC_mergeable:
5558 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5559 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005560 case OMPC_read:
5561 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5562 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005563 case OMPC_write:
5564 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5565 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005566 case OMPC_update:
5567 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5568 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005569 case OMPC_capture:
5570 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5571 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005572 case OMPC_seq_cst:
5573 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5574 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00005575 case OMPC_threads:
5576 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
5577 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005578 case OMPC_simd:
5579 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
5580 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005581 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005582 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005583 case OMPC_num_threads:
5584 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005585 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005586 case OMPC_collapse:
5587 case OMPC_schedule:
5588 case OMPC_private:
5589 case OMPC_firstprivate:
5590 case OMPC_lastprivate:
5591 case OMPC_shared:
5592 case OMPC_reduction:
5593 case OMPC_linear:
5594 case OMPC_aligned:
5595 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005596 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005597 case OMPC_default:
5598 case OMPC_proc_bind:
5599 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005600 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005601 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005602 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005603 case OMPC_unknown:
5604 llvm_unreachable("Clause is not allowed.");
5605 }
5606 return Res;
5607}
5608
Alexey Bataev236070f2014-06-20 11:19:47 +00005609OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5610 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005611 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005612 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5613}
5614
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005615OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5616 SourceLocation EndLoc) {
5617 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5618}
5619
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005620OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5621 SourceLocation EndLoc) {
5622 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5623}
5624
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005625OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5626 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005627 return new (Context) OMPReadClause(StartLoc, EndLoc);
5628}
5629
Alexey Bataevdea47612014-07-23 07:46:59 +00005630OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5631 SourceLocation EndLoc) {
5632 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5633}
5634
Alexey Bataev67a4f222014-07-23 10:25:33 +00005635OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5636 SourceLocation EndLoc) {
5637 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5638}
5639
Alexey Bataev459dec02014-07-24 06:46:57 +00005640OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5641 SourceLocation EndLoc) {
5642 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5643}
5644
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005645OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5646 SourceLocation EndLoc) {
5647 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5648}
5649
Alexey Bataev346265e2015-09-25 10:37:12 +00005650OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
5651 SourceLocation EndLoc) {
5652 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
5653}
5654
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005655OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
5656 SourceLocation EndLoc) {
5657 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
5658}
5659
Alexey Bataevc5e02582014-06-16 07:08:35 +00005660OMPClause *Sema::ActOnOpenMPVarListClause(
5661 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5662 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5663 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005664 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005665 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005666 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005667 switch (Kind) {
5668 case OMPC_private:
5669 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5670 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005671 case OMPC_firstprivate:
5672 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5673 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005674 case OMPC_lastprivate:
5675 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5676 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005677 case OMPC_shared:
5678 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5679 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005680 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005681 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5682 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005683 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005684 case OMPC_linear:
5685 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005686 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005687 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005688 case OMPC_aligned:
5689 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5690 ColonLoc, EndLoc);
5691 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005692 case OMPC_copyin:
5693 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5694 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005695 case OMPC_copyprivate:
5696 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5697 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005698 case OMPC_flush:
5699 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5700 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005701 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005702 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005703 LParenLoc, EndLoc);
5704 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005705 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005706 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005707 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005708 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005709 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005710 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005711 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005712 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005713 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005714 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005715 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005716 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005717 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005718 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005719 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005720 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005721 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005722 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005723 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005724 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005725 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005726 case OMPC_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005727 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005728 llvm_unreachable("Clause is not allowed.");
5729 }
5730 return Res;
5731}
5732
5733OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5734 SourceLocation StartLoc,
5735 SourceLocation LParenLoc,
5736 SourceLocation EndLoc) {
5737 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005738 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005739 for (auto &RefExpr : VarList) {
5740 assert(RefExpr && "NULL expr in OpenMP private clause.");
5741 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005742 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005743 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005744 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005745 continue;
5746 }
5747
Alexey Bataeved09d242014-05-28 05:53:51 +00005748 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005749 // OpenMP [2.1, C/C++]
5750 // A list item is a variable name.
5751 // OpenMP [2.9.3.3, Restrictions, p.1]
5752 // A variable that is part of another variable (as an array or
5753 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005754 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005755 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005756 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005757 continue;
5758 }
5759 Decl *D = DE->getDecl();
5760 VarDecl *VD = cast<VarDecl>(D);
5761
5762 QualType Type = VD->getType();
5763 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5764 // It will be analyzed later.
5765 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005766 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005767 continue;
5768 }
5769
5770 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5771 // A variable that appears in a private clause must not have an incomplete
5772 // type or a reference type.
5773 if (RequireCompleteType(ELoc, Type,
5774 diag::err_omp_private_incomplete_type)) {
5775 continue;
5776 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005777 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005778
Alexey Bataev758e55e2013-09-06 18:03:48 +00005779 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5780 // in a Construct]
5781 // Variables with the predetermined data-sharing attributes may not be
5782 // listed in data-sharing attributes clauses, except for the cases
5783 // listed below. For these exceptions only, listing a predetermined
5784 // variable in a data-sharing attribute clause is allowed and overrides
5785 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005786 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005787 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005788 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5789 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005790 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005791 continue;
5792 }
5793
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005794 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005795 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005796 DSAStack->getCurrentDirective() == OMPD_task) {
5797 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5798 << getOpenMPClauseName(OMPC_private) << Type
5799 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5800 bool IsDecl =
5801 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5802 Diag(VD->getLocation(),
5803 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5804 << VD;
5805 continue;
5806 }
5807
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005808 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5809 // A variable of class type (or array thereof) that appears in a private
5810 // clause requires an accessible, unambiguous default constructor for the
5811 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005812 // Generate helper private variable and initialize it with the default
5813 // value. The address of the original variable is replaced by the address of
5814 // the new private variable in CodeGen. This new variable is not added to
5815 // IdResolver, so the code in the OpenMP region uses original variable for
5816 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005817 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00005818 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
5819 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005820 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005821 if (VDPrivate->isInvalidDecl())
5822 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005823 auto VDPrivateRefExpr = buildDeclRefExpr(
5824 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005825
Alexey Bataev758e55e2013-09-06 18:03:48 +00005826 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005827 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005828 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005829 }
5830
Alexey Bataeved09d242014-05-28 05:53:51 +00005831 if (Vars.empty())
5832 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005833
Alexey Bataev03b340a2014-10-21 03:16:40 +00005834 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5835 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005836}
5837
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005838namespace {
5839class DiagsUninitializedSeveretyRAII {
5840private:
5841 DiagnosticsEngine &Diags;
5842 SourceLocation SavedLoc;
5843 bool IsIgnored;
5844
5845public:
5846 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5847 bool IsIgnored)
5848 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5849 if (!IsIgnored) {
5850 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5851 /*Map*/ diag::Severity::Ignored, Loc);
5852 }
5853 }
5854 ~DiagsUninitializedSeveretyRAII() {
5855 if (!IsIgnored)
5856 Diags.popMappings(SavedLoc);
5857 }
5858};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005859}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005860
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005861OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5862 SourceLocation StartLoc,
5863 SourceLocation LParenLoc,
5864 SourceLocation EndLoc) {
5865 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005866 SmallVector<Expr *, 8> PrivateCopies;
5867 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005868 bool IsImplicitClause =
5869 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5870 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5871
Alexey Bataeved09d242014-05-28 05:53:51 +00005872 for (auto &RefExpr : VarList) {
5873 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5874 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005875 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005876 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005877 PrivateCopies.push_back(nullptr);
5878 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005879 continue;
5880 }
5881
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005882 SourceLocation ELoc =
5883 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005884 // OpenMP [2.1, C/C++]
5885 // A list item is a variable name.
5886 // OpenMP [2.9.3.3, Restrictions, p.1]
5887 // A variable that is part of another variable (as an array or
5888 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005889 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005890 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005891 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005892 continue;
5893 }
5894 Decl *D = DE->getDecl();
5895 VarDecl *VD = cast<VarDecl>(D);
5896
5897 QualType Type = VD->getType();
5898 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5899 // It will be analyzed later.
5900 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005901 PrivateCopies.push_back(nullptr);
5902 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005903 continue;
5904 }
5905
5906 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5907 // A variable that appears in a private clause must not have an incomplete
5908 // type or a reference type.
5909 if (RequireCompleteType(ELoc, Type,
5910 diag::err_omp_firstprivate_incomplete_type)) {
5911 continue;
5912 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005913 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005914
5915 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5916 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005917 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005918 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005919 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005920
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005921 // If an implicit firstprivate variable found it was checked already.
5922 if (!IsImplicitClause) {
5923 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005924 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005925 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5926 // A list item that specifies a given variable may not appear in more
5927 // than one clause on the same directive, except that a variable may be
5928 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005929 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005930 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005931 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005932 << getOpenMPClauseName(DVar.CKind)
5933 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005934 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005935 continue;
5936 }
5937
5938 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5939 // in a Construct]
5940 // Variables with the predetermined data-sharing attributes may not be
5941 // listed in data-sharing attributes clauses, except for the cases
5942 // listed below. For these exceptions only, listing a predetermined
5943 // variable in a data-sharing attribute clause is allowed and overrides
5944 // the variable's predetermined data-sharing attributes.
5945 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5946 // in a Construct, C/C++, p.2]
5947 // Variables with const-qualified type having no mutable member may be
5948 // listed in a firstprivate clause, even if they are static data members.
5949 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5950 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5951 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005952 << getOpenMPClauseName(DVar.CKind)
5953 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005954 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005955 continue;
5956 }
5957
Alexey Bataevf29276e2014-06-18 04:14:57 +00005958 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005959 // OpenMP [2.9.3.4, Restrictions, p.2]
5960 // A list item that is private within a parallel region must not appear
5961 // in a firstprivate clause on a worksharing construct if any of the
5962 // worksharing regions arising from the worksharing construct ever bind
5963 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005964 if (isOpenMPWorksharingDirective(CurrDir) &&
5965 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005966 DVar = DSAStack->getImplicitDSA(VD, true);
5967 if (DVar.CKind != OMPC_shared &&
5968 (isOpenMPParallelDirective(DVar.DKind) ||
5969 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005970 Diag(ELoc, diag::err_omp_required_access)
5971 << getOpenMPClauseName(OMPC_firstprivate)
5972 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005973 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005974 continue;
5975 }
5976 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005977 // OpenMP [2.9.3.4, Restrictions, p.3]
5978 // A list item that appears in a reduction clause of a parallel construct
5979 // must not appear in a firstprivate clause on a worksharing or task
5980 // construct if any of the worksharing or task regions arising from the
5981 // worksharing or task construct ever bind to any of the parallel regions
5982 // arising from the parallel construct.
5983 // OpenMP [2.9.3.4, Restrictions, p.4]
5984 // A list item that appears in a reduction clause in worksharing
5985 // construct must not appear in a firstprivate clause in a task construct
5986 // encountered during execution of any of the worksharing regions arising
5987 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005988 if (CurrDir == OMPD_task) {
5989 DVar =
5990 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5991 [](OpenMPDirectiveKind K) -> bool {
5992 return isOpenMPParallelDirective(K) ||
5993 isOpenMPWorksharingDirective(K);
5994 },
5995 false);
5996 if (DVar.CKind == OMPC_reduction &&
5997 (isOpenMPParallelDirective(DVar.DKind) ||
5998 isOpenMPWorksharingDirective(DVar.DKind))) {
5999 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6000 << getOpenMPDirectiveName(DVar.DKind);
6001 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6002 continue;
6003 }
6004 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006005 }
6006
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006007 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006008 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006009 DSAStack->getCurrentDirective() == OMPD_task) {
6010 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6011 << getOpenMPClauseName(OMPC_firstprivate) << Type
6012 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6013 bool IsDecl =
6014 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6015 Diag(VD->getLocation(),
6016 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6017 << VD;
6018 continue;
6019 }
6020
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006021 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006022 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6023 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006024 // Generate helper private variable and initialize it with the value of the
6025 // original variable. The address of the original variable is replaced by
6026 // the address of the new private variable in the CodeGen. This new variable
6027 // is not added to IdResolver, so the code in the OpenMP region uses
6028 // original variable for proper diagnostics and variable capturing.
6029 Expr *VDInitRefExpr = nullptr;
6030 // For arrays generate initializer for single element and replace it by the
6031 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006032 if (Type->isArrayType()) {
6033 auto VDInit =
6034 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6035 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006036 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006037 ElemType = ElemType.getUnqualifiedType();
6038 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6039 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006040 InitializedEntity Entity =
6041 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006042 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6043
6044 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6045 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6046 if (Result.isInvalid())
6047 VDPrivate->setInvalidDecl();
6048 else
6049 VDPrivate->setInit(Result.getAs<Expr>());
6050 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006051 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006052 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006053 VDInitRefExpr =
6054 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006055 AddInitializerToDecl(VDPrivate,
6056 DefaultLvalueConversion(VDInitRefExpr).get(),
6057 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006058 }
6059 if (VDPrivate->isInvalidDecl()) {
6060 if (IsImplicitClause) {
6061 Diag(DE->getExprLoc(),
6062 diag::note_omp_task_predetermined_firstprivate_here);
6063 }
6064 continue;
6065 }
6066 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006067 auto VDPrivateRefExpr = buildDeclRefExpr(
6068 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006069 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6070 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006071 PrivateCopies.push_back(VDPrivateRefExpr);
6072 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006073 }
6074
Alexey Bataeved09d242014-05-28 05:53:51 +00006075 if (Vars.empty())
6076 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006077
6078 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006079 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006080}
6081
Alexander Musman1bb328c2014-06-04 13:06:39 +00006082OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6083 SourceLocation StartLoc,
6084 SourceLocation LParenLoc,
6085 SourceLocation EndLoc) {
6086 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006087 SmallVector<Expr *, 8> SrcExprs;
6088 SmallVector<Expr *, 8> DstExprs;
6089 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006090 for (auto &RefExpr : VarList) {
6091 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6092 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6093 // It will be analyzed later.
6094 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006095 SrcExprs.push_back(nullptr);
6096 DstExprs.push_back(nullptr);
6097 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006098 continue;
6099 }
6100
6101 SourceLocation ELoc = RefExpr->getExprLoc();
6102 // OpenMP [2.1, C/C++]
6103 // A list item is a variable name.
6104 // OpenMP [2.14.3.5, Restrictions, p.1]
6105 // A variable that is part of another variable (as an array or structure
6106 // element) cannot appear in a lastprivate clause.
6107 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6108 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6109 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6110 continue;
6111 }
6112 Decl *D = DE->getDecl();
6113 VarDecl *VD = cast<VarDecl>(D);
6114
6115 QualType Type = VD->getType();
6116 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6117 // It will be analyzed later.
6118 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006119 SrcExprs.push_back(nullptr);
6120 DstExprs.push_back(nullptr);
6121 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006122 continue;
6123 }
6124
6125 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6126 // A variable that appears in a lastprivate clause must not have an
6127 // incomplete type or a reference type.
6128 if (RequireCompleteType(ELoc, Type,
6129 diag::err_omp_lastprivate_incomplete_type)) {
6130 continue;
6131 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006132 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006133
6134 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6135 // in a Construct]
6136 // Variables with the predetermined data-sharing attributes may not be
6137 // listed in data-sharing attributes clauses, except for the cases
6138 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006139 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006140 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6141 DVar.CKind != OMPC_firstprivate &&
6142 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6143 Diag(ELoc, diag::err_omp_wrong_dsa)
6144 << getOpenMPClauseName(DVar.CKind)
6145 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006146 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006147 continue;
6148 }
6149
Alexey Bataevf29276e2014-06-18 04:14:57 +00006150 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6151 // OpenMP [2.14.3.5, Restrictions, p.2]
6152 // A list item that is private within a parallel region, or that appears in
6153 // the reduction clause of a parallel construct, must not appear in a
6154 // lastprivate clause on a worksharing construct if any of the corresponding
6155 // worksharing regions ever binds to any of the corresponding parallel
6156 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006157 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006158 if (isOpenMPWorksharingDirective(CurrDir) &&
6159 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006160 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006161 if (DVar.CKind != OMPC_shared) {
6162 Diag(ELoc, diag::err_omp_required_access)
6163 << getOpenMPClauseName(OMPC_lastprivate)
6164 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006165 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006166 continue;
6167 }
6168 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006169 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006170 // A variable of class type (or array thereof) that appears in a
6171 // lastprivate clause requires an accessible, unambiguous default
6172 // constructor for the class type, unless the list item is also specified
6173 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006174 // A variable of class type (or array thereof) that appears in a
6175 // lastprivate clause requires an accessible, unambiguous copy assignment
6176 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006177 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006178 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006179 Type.getUnqualifiedType(), ".lastprivate.src",
6180 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006181 auto *PseudoSrcExpr = buildDeclRefExpr(
6182 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006183 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006184 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6185 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006186 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006187 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006188 // For arrays generate assignment operation for single element and replace
6189 // it by the original array element in CodeGen.
6190 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6191 PseudoDstExpr, PseudoSrcExpr);
6192 if (AssignmentOp.isInvalid())
6193 continue;
6194 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6195 /*DiscardedValue=*/true);
6196 if (AssignmentOp.isInvalid())
6197 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006198
Alexey Bataev39f915b82015-05-08 10:41:21 +00006199 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006200 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006201 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006202 SrcExprs.push_back(PseudoSrcExpr);
6203 DstExprs.push_back(PseudoDstExpr);
6204 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006205 }
6206
6207 if (Vars.empty())
6208 return nullptr;
6209
6210 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006211 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006212}
6213
Alexey Bataev758e55e2013-09-06 18:03:48 +00006214OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6215 SourceLocation StartLoc,
6216 SourceLocation LParenLoc,
6217 SourceLocation EndLoc) {
6218 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006219 for (auto &RefExpr : VarList) {
6220 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6221 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006222 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006223 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006224 continue;
6225 }
6226
Alexey Bataeved09d242014-05-28 05:53:51 +00006227 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006228 // OpenMP [2.1, C/C++]
6229 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006230 // OpenMP [2.14.3.2, Restrictions, p.1]
6231 // A variable that is part of another variable (as an array or structure
6232 // element) cannot appear in a shared unless it is a static data member
6233 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006234 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006235 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006236 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006237 continue;
6238 }
6239 Decl *D = DE->getDecl();
6240 VarDecl *VD = cast<VarDecl>(D);
6241
6242 QualType Type = VD->getType();
6243 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6244 // It will be analyzed later.
6245 Vars.push_back(DE);
6246 continue;
6247 }
6248
6249 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6250 // in a Construct]
6251 // Variables with the predetermined data-sharing attributes may not be
6252 // listed in data-sharing attributes clauses, except for the cases
6253 // listed below. For these exceptions only, listing a predetermined
6254 // variable in a data-sharing attribute clause is allowed and overrides
6255 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006256 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006257 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6258 DVar.RefExpr) {
6259 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6260 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006261 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006262 continue;
6263 }
6264
6265 DSAStack->addDSA(VD, DE, OMPC_shared);
6266 Vars.push_back(DE);
6267 }
6268
Alexey Bataeved09d242014-05-28 05:53:51 +00006269 if (Vars.empty())
6270 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006271
6272 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6273}
6274
Alexey Bataevc5e02582014-06-16 07:08:35 +00006275namespace {
6276class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6277 DSAStackTy *Stack;
6278
6279public:
6280 bool VisitDeclRefExpr(DeclRefExpr *E) {
6281 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006282 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006283 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6284 return false;
6285 if (DVar.CKind != OMPC_unknown)
6286 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006287 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006288 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006289 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006290 return true;
6291 return false;
6292 }
6293 return false;
6294 }
6295 bool VisitStmt(Stmt *S) {
6296 for (auto Child : S->children()) {
6297 if (Child && Visit(Child))
6298 return true;
6299 }
6300 return false;
6301 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006302 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006303};
Alexey Bataev23b69422014-06-18 07:08:49 +00006304} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006305
6306OMPClause *Sema::ActOnOpenMPReductionClause(
6307 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6308 SourceLocation ColonLoc, SourceLocation EndLoc,
6309 CXXScopeSpec &ReductionIdScopeSpec,
6310 const DeclarationNameInfo &ReductionId) {
6311 // TODO: Allow scope specification search when 'declare reduction' is
6312 // supported.
6313 assert(ReductionIdScopeSpec.isEmpty() &&
6314 "No support for scoped reduction identifiers yet.");
6315
6316 auto DN = ReductionId.getName();
6317 auto OOK = DN.getCXXOverloadedOperator();
6318 BinaryOperatorKind BOK = BO_Comma;
6319
6320 // OpenMP [2.14.3.6, reduction clause]
6321 // C
6322 // reduction-identifier is either an identifier or one of the following
6323 // operators: +, -, *, &, |, ^, && and ||
6324 // C++
6325 // reduction-identifier is either an id-expression or one of the following
6326 // operators: +, -, *, &, |, ^, && and ||
6327 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6328 switch (OOK) {
6329 case OO_Plus:
6330 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006331 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006332 break;
6333 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006334 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006335 break;
6336 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006337 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006338 break;
6339 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006340 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006341 break;
6342 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006343 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006344 break;
6345 case OO_AmpAmp:
6346 BOK = BO_LAnd;
6347 break;
6348 case OO_PipePipe:
6349 BOK = BO_LOr;
6350 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006351 case OO_New:
6352 case OO_Delete:
6353 case OO_Array_New:
6354 case OO_Array_Delete:
6355 case OO_Slash:
6356 case OO_Percent:
6357 case OO_Tilde:
6358 case OO_Exclaim:
6359 case OO_Equal:
6360 case OO_Less:
6361 case OO_Greater:
6362 case OO_LessEqual:
6363 case OO_GreaterEqual:
6364 case OO_PlusEqual:
6365 case OO_MinusEqual:
6366 case OO_StarEqual:
6367 case OO_SlashEqual:
6368 case OO_PercentEqual:
6369 case OO_CaretEqual:
6370 case OO_AmpEqual:
6371 case OO_PipeEqual:
6372 case OO_LessLess:
6373 case OO_GreaterGreater:
6374 case OO_LessLessEqual:
6375 case OO_GreaterGreaterEqual:
6376 case OO_EqualEqual:
6377 case OO_ExclaimEqual:
6378 case OO_PlusPlus:
6379 case OO_MinusMinus:
6380 case OO_Comma:
6381 case OO_ArrowStar:
6382 case OO_Arrow:
6383 case OO_Call:
6384 case OO_Subscript:
6385 case OO_Conditional:
6386 case NUM_OVERLOADED_OPERATORS:
6387 llvm_unreachable("Unexpected reduction identifier");
6388 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006389 if (auto II = DN.getAsIdentifierInfo()) {
6390 if (II->isStr("max"))
6391 BOK = BO_GT;
6392 else if (II->isStr("min"))
6393 BOK = BO_LT;
6394 }
6395 break;
6396 }
6397 SourceRange ReductionIdRange;
6398 if (ReductionIdScopeSpec.isValid()) {
6399 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6400 }
6401 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6402 if (BOK == BO_Comma) {
6403 // Not allowed reduction identifier is found.
6404 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6405 << ReductionIdRange;
6406 return nullptr;
6407 }
6408
6409 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006410 SmallVector<Expr *, 8> LHSs;
6411 SmallVector<Expr *, 8> RHSs;
6412 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006413 for (auto RefExpr : VarList) {
6414 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6415 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6416 // It will be analyzed later.
6417 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006418 LHSs.push_back(nullptr);
6419 RHSs.push_back(nullptr);
6420 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006421 continue;
6422 }
6423
6424 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6425 RefExpr->isInstantiationDependent() ||
6426 RefExpr->containsUnexpandedParameterPack()) {
6427 // It will be analyzed later.
6428 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006429 LHSs.push_back(nullptr);
6430 RHSs.push_back(nullptr);
6431 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006432 continue;
6433 }
6434
6435 auto ELoc = RefExpr->getExprLoc();
6436 auto ERange = RefExpr->getSourceRange();
6437 // OpenMP [2.1, C/C++]
6438 // A list item is a variable or array section, subject to the restrictions
6439 // specified in Section 2.4 on page 42 and in each of the sections
6440 // describing clauses and directives for which a list appears.
6441 // OpenMP [2.14.3.3, Restrictions, p.1]
6442 // A variable that is part of another variable (as an array or
6443 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00006444 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
6445 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
6446 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
6447 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
6448 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006449 continue;
6450 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006451 QualType Type;
6452 VarDecl *VD = nullptr;
6453 if (DE) {
6454 auto D = DE->getDecl();
6455 VD = cast<VarDecl>(D);
6456 Type = VD->getType();
6457 } else if (ASE)
6458 Type = ASE->getType();
6459 else if (OASE) {
6460 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
6461 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
6462 Type = ATy->getElementType();
6463 else
6464 Type = BaseType->getPointeeType();
6465 }
6466 // OpenMP [2.15.3.6, reduction Clause]
6467 // If a list item is an array section, its lower-bound must be zero.
6468 llvm::APSInt Result;
6469 if (OASE && OASE->getLowerBound() &&
6470 OASE->getLowerBound()->EvaluateAsInt(Result, Context) && Result != 0) {
6471 Diag(OASE->getLowerBound()->getExprLoc(),
6472 diag::err_omp_expected_array_sect_reduction_lb_not_zero)
6473 << OASE->getLowerBound()->getSourceRange();
6474 continue;
6475 }
6476
Alexey Bataevc5e02582014-06-16 07:08:35 +00006477 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6478 // A variable that appears in a private clause must not have an incomplete
6479 // type or a reference type.
6480 if (RequireCompleteType(ELoc, Type,
6481 diag::err_omp_reduction_incomplete_type))
6482 continue;
6483 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6484 // Arrays may not appear in a reduction clause.
6485 if (Type.getNonReferenceType()->isArrayType()) {
6486 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataeva1764212015-09-30 09:22:36 +00006487 if (VD) {
6488 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6489 VarDecl::DeclarationOnly;
6490 Diag(VD->getLocation(),
6491 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6492 << VD;
6493 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006494 continue;
6495 }
6496 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6497 // A list item that appears in a reduction clause must not be
6498 // const-qualified.
6499 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006500 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006501 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataeva1764212015-09-30 09:22:36 +00006502 if (VD) {
6503 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6504 VarDecl::DeclarationOnly;
6505 Diag(VD->getLocation(),
6506 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6507 << VD;
6508 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006509 continue;
6510 }
6511 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6512 // If a list-item is a reference type then it must bind to the same object
6513 // for all threads of the team.
Alexey Bataeva1764212015-09-30 09:22:36 +00006514 if (VD) {
6515 VarDecl *VDDef = VD->getDefinition();
6516 if (Type->isReferenceType() && VDDef) {
6517 DSARefChecker Check(DSAStack);
6518 if (Check.Visit(VDDef->getInit())) {
6519 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6520 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6521 continue;
6522 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006523 }
6524 }
6525 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6526 // The type of a list item that appears in a reduction clause must be valid
6527 // for the reduction-identifier. For a max or min reduction in C, the type
6528 // of the list item must be an allowed arithmetic data type: char, int,
6529 // float, double, or _Bool, possibly modified with long, short, signed, or
6530 // unsigned. For a max or min reduction in C++, the type of the list item
6531 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6532 // double, or bool, possibly modified with long, short, signed, or unsigned.
6533 if ((BOK == BO_GT || BOK == BO_LT) &&
6534 !(Type->isScalarType() ||
6535 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6536 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6537 << getLangOpts().CPlusPlus;
Alexey Bataeva1764212015-09-30 09:22:36 +00006538 if (VD) {
6539 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6540 VarDecl::DeclarationOnly;
6541 Diag(VD->getLocation(),
6542 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6543 << VD;
6544 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006545 continue;
6546 }
6547 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6548 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6549 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataeva1764212015-09-30 09:22:36 +00006550 if (VD) {
6551 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6552 VarDecl::DeclarationOnly;
6553 Diag(VD->getLocation(),
6554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6555 << VD;
6556 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006557 continue;
6558 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006559 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6560 // in a Construct]
6561 // Variables with the predetermined data-sharing attributes may not be
6562 // listed in data-sharing attributes clauses, except for the cases
6563 // listed below. For these exceptions only, listing a predetermined
6564 // variable in a data-sharing attribute clause is allowed and overrides
6565 // the variable's predetermined data-sharing attributes.
6566 // OpenMP [2.14.3.6, Restrictions, p.3]
6567 // Any number of reduction clauses can be specified on the directive,
6568 // but a list item can appear only once in the reduction clauses for that
6569 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00006570 DSAStackTy::DSAVarData DVar;
6571 if (VD) {
6572 DVar = DSAStack->getTopDSA(VD, false);
6573 if (DVar.CKind == OMPC_reduction) {
6574 Diag(ELoc, diag::err_omp_once_referenced)
6575 << getOpenMPClauseName(OMPC_reduction);
6576 if (DVar.RefExpr) {
6577 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6578 }
6579 } else if (DVar.CKind != OMPC_unknown) {
6580 Diag(ELoc, diag::err_omp_wrong_dsa)
6581 << getOpenMPClauseName(DVar.CKind)
6582 << getOpenMPClauseName(OMPC_reduction);
6583 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6584 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006585 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006586 }
6587
6588 // OpenMP [2.14.3.6, Restrictions, p.1]
6589 // A list item that appears in a reduction clause of a worksharing
6590 // construct must be shared in the parallel regions to which any of the
6591 // worksharing regions arising from the worksharing construct bind.
Alexey Bataeva1764212015-09-30 09:22:36 +00006592 if (VD) {
6593 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6594 if (isOpenMPWorksharingDirective(CurrDir) &&
6595 !isOpenMPParallelDirective(CurrDir)) {
6596 DVar = DSAStack->getImplicitDSA(VD, true);
6597 if (DVar.CKind != OMPC_shared) {
6598 Diag(ELoc, diag::err_omp_required_access)
6599 << getOpenMPClauseName(OMPC_reduction)
6600 << getOpenMPClauseName(OMPC_shared);
6601 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6602 continue;
6603 }
Alexey Bataevf29276e2014-06-18 04:14:57 +00006604 }
6605 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006606 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeva1764212015-09-30 09:22:36 +00006607 auto *LHSVD =
6608 buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
6609 VD && VD->hasAttrs() ? &VD->getAttrs() : nullptr);
6610 auto *RHSVD =
6611 buildVarDecl(*this, ELoc, Type, VD ? VD->getName() : ".item.",
6612 VD && VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006613 // Add initializer for private variable.
6614 Expr *Init = nullptr;
6615 switch (BOK) {
6616 case BO_Add:
6617 case BO_Xor:
6618 case BO_Or:
6619 case BO_LOr:
6620 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6621 if (Type->isScalarType() || Type->isAnyComplexType()) {
6622 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006623 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006624 break;
6625 case BO_Mul:
6626 case BO_LAnd:
6627 if (Type->isScalarType() || Type->isAnyComplexType()) {
6628 // '*' and '&&' reduction ops - initializer is '1'.
6629 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6630 }
6631 break;
6632 case BO_And: {
6633 // '&' reduction op - initializer is '~0'.
6634 QualType OrigType = Type;
6635 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6636 Type = ComplexTy->getElementType();
6637 }
6638 if (Type->isRealFloatingType()) {
6639 llvm::APFloat InitValue =
6640 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6641 /*isIEEE=*/true);
6642 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6643 Type, ELoc);
6644 } else if (Type->isScalarType()) {
6645 auto Size = Context.getTypeSize(Type);
6646 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6647 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6648 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6649 }
6650 if (Init && OrigType->isAnyComplexType()) {
6651 // Init = 0xFFFF + 0xFFFFi;
6652 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6653 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6654 }
6655 Type = OrigType;
6656 break;
6657 }
6658 case BO_LT:
6659 case BO_GT: {
6660 // 'min' reduction op - initializer is 'Largest representable number in
6661 // the reduction list item type'.
6662 // 'max' reduction op - initializer is 'Least representable number in
6663 // the reduction list item type'.
6664 if (Type->isIntegerType() || Type->isPointerType()) {
6665 bool IsSigned = Type->hasSignedIntegerRepresentation();
6666 auto Size = Context.getTypeSize(Type);
6667 QualType IntTy =
6668 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6669 llvm::APInt InitValue =
6670 (BOK != BO_LT)
6671 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6672 : llvm::APInt::getMinValue(Size)
6673 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6674 : llvm::APInt::getMaxValue(Size);
6675 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6676 if (Type->isPointerType()) {
6677 // Cast to pointer type.
6678 auto CastExpr = BuildCStyleCastExpr(
6679 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6680 SourceLocation(), Init);
6681 if (CastExpr.isInvalid())
6682 continue;
6683 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006684 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006685 } else if (Type->isRealFloatingType()) {
6686 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6687 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6688 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6689 Type, ELoc);
6690 }
6691 break;
6692 }
6693 case BO_PtrMemD:
6694 case BO_PtrMemI:
6695 case BO_MulAssign:
6696 case BO_Div:
6697 case BO_Rem:
6698 case BO_Sub:
6699 case BO_Shl:
6700 case BO_Shr:
6701 case BO_LE:
6702 case BO_GE:
6703 case BO_EQ:
6704 case BO_NE:
6705 case BO_AndAssign:
6706 case BO_XorAssign:
6707 case BO_OrAssign:
6708 case BO_Assign:
6709 case BO_AddAssign:
6710 case BO_SubAssign:
6711 case BO_DivAssign:
6712 case BO_RemAssign:
6713 case BO_ShlAssign:
6714 case BO_ShrAssign:
6715 case BO_Comma:
6716 llvm_unreachable("Unexpected reduction operation");
6717 }
6718 if (Init) {
6719 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6720 /*TypeMayContainAuto=*/false);
6721 } else {
6722 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6723 }
6724 if (!RHSVD->hasInit()) {
6725 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6726 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00006727 if (VD) {
6728 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6729 VarDecl::DeclarationOnly;
6730 Diag(VD->getLocation(),
6731 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6732 << VD;
6733 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006734 continue;
6735 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006736 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6737 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006738 ExprResult ReductionOp =
6739 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6740 LHSDRE, RHSDRE);
6741 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006742 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006743 ReductionOp =
6744 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6745 BO_Assign, LHSDRE, ReductionOp.get());
6746 } else {
6747 auto *ConditionalOp = new (Context) ConditionalOperator(
6748 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6749 RHSDRE, Type, VK_LValue, OK_Ordinary);
6750 ReductionOp =
6751 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6752 BO_Assign, LHSDRE, ConditionalOp);
6753 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006754 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006755 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006756 if (ReductionOp.isInvalid())
6757 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006758
Alexey Bataeva1764212015-09-30 09:22:36 +00006759 if (VD)
6760 DSAStack->addDSA(VD, DE, OMPC_reduction);
6761 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006762 LHSs.push_back(LHSDRE);
6763 RHSs.push_back(RHSDRE);
6764 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006765 }
6766
6767 if (Vars.empty())
6768 return nullptr;
6769
6770 return OMPReductionClause::Create(
6771 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006772 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6773 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006774}
6775
Alexey Bataev182227b2015-08-20 10:54:39 +00006776OMPClause *Sema::ActOnOpenMPLinearClause(
6777 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6778 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6779 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006780 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006781 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006782 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006783 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6784 LinKind == OMPC_LINEAR_unknown) {
6785 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6786 LinKind = OMPC_LINEAR_val;
6787 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006788 for (auto &RefExpr : VarList) {
6789 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6790 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006791 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006792 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006793 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006794 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006795 continue;
6796 }
6797
6798 // OpenMP [2.14.3.7, linear clause]
6799 // A list item that appears in a linear clause is subject to the private
6800 // clause semantics described in Section 2.14.3.3 on page 159 except as
6801 // noted. In addition, the value of the new list item on each iteration
6802 // of the associated loop(s) corresponds to the value of the original
6803 // list item before entering the construct plus the logical number of
6804 // the iteration times linear-step.
6805
Alexey Bataeved09d242014-05-28 05:53:51 +00006806 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006807 // OpenMP [2.1, C/C++]
6808 // A list item is a variable name.
6809 // OpenMP [2.14.3.3, Restrictions, p.1]
6810 // A variable that is part of another variable (as an array or
6811 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006812 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006813 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006814 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006815 continue;
6816 }
6817
6818 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6819
6820 // OpenMP [2.14.3.7, linear clause]
6821 // A list-item cannot appear in more than one linear clause.
6822 // A list-item that appears in a linear clause cannot appear in any
6823 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006824 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006825 if (DVar.RefExpr) {
6826 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6827 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006828 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006829 continue;
6830 }
6831
6832 QualType QType = VD->getType();
6833 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6834 // It will be analyzed later.
6835 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006836 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006837 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006838 continue;
6839 }
6840
6841 // A variable must not have an incomplete type or a reference type.
6842 if (RequireCompleteType(ELoc, QType,
6843 diag::err_omp_linear_incomplete_type)) {
6844 continue;
6845 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006846 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6847 !QType->isReferenceType()) {
6848 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6849 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6850 continue;
6851 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006852 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006853
6854 // A list item must not be const-qualified.
6855 if (QType.isConstant(Context)) {
6856 Diag(ELoc, diag::err_omp_const_variable)
6857 << getOpenMPClauseName(OMPC_linear);
6858 bool IsDecl =
6859 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6860 Diag(VD->getLocation(),
6861 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6862 << VD;
6863 continue;
6864 }
6865
6866 // A list item must be of integral or pointer type.
6867 QType = QType.getUnqualifiedType().getCanonicalType();
6868 const Type *Ty = QType.getTypePtrOrNull();
6869 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6870 !Ty->isPointerType())) {
6871 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6872 bool IsDecl =
6873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6874 Diag(VD->getLocation(),
6875 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6876 << VD;
6877 continue;
6878 }
6879
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006880 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006881 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
6882 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006883 auto *PrivateRef = buildDeclRefExpr(
6884 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006885 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006886 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006887 Expr *InitExpr;
6888 if (LinKind == OMPC_LINEAR_uval)
6889 InitExpr = VD->getInit();
6890 else
6891 InitExpr = DE;
6892 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006893 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006894 auto InitRef = buildDeclRefExpr(
6895 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006896 DSAStack->addDSA(VD, DE, OMPC_linear);
6897 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006898 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006899 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006900 }
6901
6902 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006903 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006904
6905 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006906 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006907 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6908 !Step->isInstantiationDependent() &&
6909 !Step->containsUnexpandedParameterPack()) {
6910 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006911 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006912 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006913 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006914 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006915
Alexander Musman3276a272015-03-21 10:12:56 +00006916 // Build var to save the step value.
6917 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006918 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006919 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006920 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006921 ExprResult CalcStep =
6922 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006923 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00006924
Alexander Musman8dba6642014-04-22 13:09:42 +00006925 // Warn about zero linear step (it would be probably better specified as
6926 // making corresponding variables 'const').
6927 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006928 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6929 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006930 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6931 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006932 if (!IsConstant && CalcStep.isUsable()) {
6933 // Calculate the step beforehand instead of doing this on each iteration.
6934 // (This is not used if the number of iterations may be kfold-ed).
6935 CalcStepExpr = CalcStep.get();
6936 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006937 }
6938
Alexey Bataev182227b2015-08-20 10:54:39 +00006939 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6940 ColonLoc, EndLoc, Vars, Privates, Inits,
6941 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006942}
6943
6944static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6945 Expr *NumIterations, Sema &SemaRef,
6946 Scope *S) {
6947 // Walk the vars and build update/final expressions for the CodeGen.
6948 SmallVector<Expr *, 8> Updates;
6949 SmallVector<Expr *, 8> Finals;
6950 Expr *Step = Clause.getStep();
6951 Expr *CalcStep = Clause.getCalcStep();
6952 // OpenMP [2.14.3.7, linear clause]
6953 // If linear-step is not specified it is assumed to be 1.
6954 if (Step == nullptr)
6955 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6956 else if (CalcStep)
6957 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6958 bool HasErrors = false;
6959 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006960 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006961 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006962 for (auto &RefExpr : Clause.varlists()) {
6963 Expr *InitExpr = *CurInit;
6964
6965 // Build privatized reference to the current linear var.
6966 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006967 Expr *CapturedRef;
6968 if (LinKind == OMPC_LINEAR_uval)
6969 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6970 else
6971 CapturedRef =
6972 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6973 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6974 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006975
6976 // Build update: Var = InitExpr + IV * Step
6977 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006978 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006979 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006980 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
6981 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006982
6983 // Build final: Var = InitExpr + NumIterations * Step
6984 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006985 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006986 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006987 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
6988 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006989 if (!Update.isUsable() || !Final.isUsable()) {
6990 Updates.push_back(nullptr);
6991 Finals.push_back(nullptr);
6992 HasErrors = true;
6993 } else {
6994 Updates.push_back(Update.get());
6995 Finals.push_back(Final.get());
6996 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006997 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006998 }
6999 Clause.setUpdates(Updates);
7000 Clause.setFinals(Finals);
7001 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007002}
7003
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007004OMPClause *Sema::ActOnOpenMPAlignedClause(
7005 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7006 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7007
7008 SmallVector<Expr *, 8> Vars;
7009 for (auto &RefExpr : VarList) {
7010 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7011 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7012 // It will be analyzed later.
7013 Vars.push_back(RefExpr);
7014 continue;
7015 }
7016
7017 SourceLocation ELoc = RefExpr->getExprLoc();
7018 // OpenMP [2.1, C/C++]
7019 // A list item is a variable name.
7020 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7021 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7022 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7023 continue;
7024 }
7025
7026 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7027
7028 // OpenMP [2.8.1, simd construct, Restrictions]
7029 // The type of list items appearing in the aligned clause must be
7030 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007031 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007032 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007033 const Type *Ty = QType.getTypePtrOrNull();
7034 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7035 !Ty->isPointerType())) {
7036 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7037 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7038 bool IsDecl =
7039 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7040 Diag(VD->getLocation(),
7041 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7042 << VD;
7043 continue;
7044 }
7045
7046 // OpenMP [2.8.1, simd construct, Restrictions]
7047 // A list-item cannot appear in more than one aligned clause.
7048 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7049 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7050 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7051 << getOpenMPClauseName(OMPC_aligned);
7052 continue;
7053 }
7054
7055 Vars.push_back(DE);
7056 }
7057
7058 // OpenMP [2.8.1, simd construct, Description]
7059 // The parameter of the aligned clause, alignment, must be a constant
7060 // positive integer expression.
7061 // If no optional parameter is specified, implementation-defined default
7062 // alignments for SIMD instructions on the target platforms are assumed.
7063 if (Alignment != nullptr) {
7064 ExprResult AlignResult =
7065 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7066 if (AlignResult.isInvalid())
7067 return nullptr;
7068 Alignment = AlignResult.get();
7069 }
7070 if (Vars.empty())
7071 return nullptr;
7072
7073 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7074 EndLoc, Vars, Alignment);
7075}
7076
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007077OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7078 SourceLocation StartLoc,
7079 SourceLocation LParenLoc,
7080 SourceLocation EndLoc) {
7081 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007082 SmallVector<Expr *, 8> SrcExprs;
7083 SmallVector<Expr *, 8> DstExprs;
7084 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007085 for (auto &RefExpr : VarList) {
7086 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7087 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007088 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007089 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007090 SrcExprs.push_back(nullptr);
7091 DstExprs.push_back(nullptr);
7092 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007093 continue;
7094 }
7095
Alexey Bataeved09d242014-05-28 05:53:51 +00007096 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007097 // OpenMP [2.1, C/C++]
7098 // A list item is a variable name.
7099 // OpenMP [2.14.4.1, Restrictions, p.1]
7100 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007101 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007102 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007103 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007104 continue;
7105 }
7106
7107 Decl *D = DE->getDecl();
7108 VarDecl *VD = cast<VarDecl>(D);
7109
7110 QualType Type = VD->getType();
7111 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7112 // It will be analyzed later.
7113 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007114 SrcExprs.push_back(nullptr);
7115 DstExprs.push_back(nullptr);
7116 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007117 continue;
7118 }
7119
7120 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7121 // A list item that appears in a copyin clause must be threadprivate.
7122 if (!DSAStack->isThreadPrivate(VD)) {
7123 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007124 << getOpenMPClauseName(OMPC_copyin)
7125 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007126 continue;
7127 }
7128
7129 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7130 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007131 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007132 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007133 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007134 auto *SrcVD =
7135 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7136 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007137 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007138 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7139 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007140 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7141 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007142 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007143 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007144 // For arrays generate assignment operation for single element and replace
7145 // it by the original array element in CodeGen.
7146 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7147 PseudoDstExpr, PseudoSrcExpr);
7148 if (AssignmentOp.isInvalid())
7149 continue;
7150 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7151 /*DiscardedValue=*/true);
7152 if (AssignmentOp.isInvalid())
7153 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007154
7155 DSAStack->addDSA(VD, DE, OMPC_copyin);
7156 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007157 SrcExprs.push_back(PseudoSrcExpr);
7158 DstExprs.push_back(PseudoDstExpr);
7159 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007160 }
7161
Alexey Bataeved09d242014-05-28 05:53:51 +00007162 if (Vars.empty())
7163 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007164
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007165 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7166 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007167}
7168
Alexey Bataevbae9a792014-06-27 10:37:06 +00007169OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7170 SourceLocation StartLoc,
7171 SourceLocation LParenLoc,
7172 SourceLocation EndLoc) {
7173 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007174 SmallVector<Expr *, 8> SrcExprs;
7175 SmallVector<Expr *, 8> DstExprs;
7176 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007177 for (auto &RefExpr : VarList) {
7178 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7179 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7180 // It will be analyzed later.
7181 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007182 SrcExprs.push_back(nullptr);
7183 DstExprs.push_back(nullptr);
7184 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007185 continue;
7186 }
7187
7188 SourceLocation ELoc = RefExpr->getExprLoc();
7189 // OpenMP [2.1, C/C++]
7190 // A list item is a variable name.
7191 // OpenMP [2.14.4.1, Restrictions, p.1]
7192 // A list item that appears in a copyin clause must be threadprivate.
7193 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7194 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7195 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7196 continue;
7197 }
7198
7199 Decl *D = DE->getDecl();
7200 VarDecl *VD = cast<VarDecl>(D);
7201
7202 QualType Type = VD->getType();
7203 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7204 // It will be analyzed later.
7205 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007206 SrcExprs.push_back(nullptr);
7207 DstExprs.push_back(nullptr);
7208 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007209 continue;
7210 }
7211
7212 // OpenMP [2.14.4.2, Restrictions, p.2]
7213 // A list item that appears in a copyprivate clause may not appear in a
7214 // private or firstprivate clause on the single construct.
7215 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007216 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007217 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7218 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007219 Diag(ELoc, diag::err_omp_wrong_dsa)
7220 << getOpenMPClauseName(DVar.CKind)
7221 << getOpenMPClauseName(OMPC_copyprivate);
7222 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7223 continue;
7224 }
7225
7226 // OpenMP [2.11.4.2, Restrictions, p.1]
7227 // All list items that appear in a copyprivate clause must be either
7228 // threadprivate or private in the enclosing context.
7229 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007230 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007231 if (DVar.CKind == OMPC_shared) {
7232 Diag(ELoc, diag::err_omp_required_access)
7233 << getOpenMPClauseName(OMPC_copyprivate)
7234 << "threadprivate or private in the enclosing context";
7235 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7236 continue;
7237 }
7238 }
7239 }
7240
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007241 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007242 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007243 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007244 << getOpenMPClauseName(OMPC_copyprivate) << Type
7245 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007246 bool IsDecl =
7247 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7248 Diag(VD->getLocation(),
7249 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7250 << VD;
7251 continue;
7252 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007253
Alexey Bataevbae9a792014-06-27 10:37:06 +00007254 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7255 // A variable of class type (or array thereof) that appears in a
7256 // copyin clause requires an accessible, unambiguous copy assignment
7257 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007258 Type = Context.getBaseElementType(Type.getNonReferenceType())
7259 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007260 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007261 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7262 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007263 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007264 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007265 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007266 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7267 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007268 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007269 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007270 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7271 PseudoDstExpr, PseudoSrcExpr);
7272 if (AssignmentOp.isInvalid())
7273 continue;
7274 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7275 /*DiscardedValue=*/true);
7276 if (AssignmentOp.isInvalid())
7277 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007278
7279 // No need to mark vars as copyprivate, they are already threadprivate or
7280 // implicitly private.
7281 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007282 SrcExprs.push_back(PseudoSrcExpr);
7283 DstExprs.push_back(PseudoDstExpr);
7284 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007285 }
7286
7287 if (Vars.empty())
7288 return nullptr;
7289
Alexey Bataeva63048e2015-03-23 06:18:07 +00007290 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7291 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007292}
7293
Alexey Bataev6125da92014-07-21 11:26:11 +00007294OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7295 SourceLocation StartLoc,
7296 SourceLocation LParenLoc,
7297 SourceLocation EndLoc) {
7298 if (VarList.empty())
7299 return nullptr;
7300
7301 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7302}
Alexey Bataevdea47612014-07-23 07:46:59 +00007303
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007304OMPClause *
7305Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7306 SourceLocation DepLoc, SourceLocation ColonLoc,
7307 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7308 SourceLocation LParenLoc, SourceLocation EndLoc) {
7309 if (DepKind == OMPC_DEPEND_unknown) {
7310 std::string Values;
7311 std::string Sep(", ");
7312 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7313 Values += "'";
7314 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7315 Values += "'";
7316 switch (i) {
7317 case OMPC_DEPEND_unknown - 2:
7318 Values += " or ";
7319 break;
7320 case OMPC_DEPEND_unknown - 1:
7321 break;
7322 default:
7323 Values += Sep;
7324 break;
7325 }
7326 }
7327 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7328 << Values << getOpenMPClauseName(OMPC_depend);
7329 return nullptr;
7330 }
7331 SmallVector<Expr *, 8> Vars;
7332 for (auto &RefExpr : VarList) {
7333 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7334 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7335 // It will be analyzed later.
7336 Vars.push_back(RefExpr);
7337 continue;
7338 }
7339
7340 SourceLocation ELoc = RefExpr->getExprLoc();
7341 // OpenMP [2.11.1.1, Restrictions, p.3]
7342 // A variable that is part of another variable (such as a field of a
7343 // structure) but is not an array element or an array section cannot appear
7344 // in a depend clause.
7345 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007346 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7347 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7348 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7349 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7350 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007351 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7352 !ASE->getBase()->getType()->isArrayType())) {
7353 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7354 << RefExpr->getSourceRange();
7355 continue;
7356 }
7357
7358 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7359 }
7360
7361 if (Vars.empty())
7362 return nullptr;
7363
7364 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7365 DepLoc, ColonLoc, Vars);
7366}
Michael Wonge710d542015-08-07 16:16:36 +00007367
7368OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7369 SourceLocation LParenLoc,
7370 SourceLocation EndLoc) {
7371 Expr *ValExpr = Device;
7372 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7373 !ValExpr->isInstantiationDependent()) {
7374 SourceLocation Loc = ValExpr->getExprLoc();
7375 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7376 if (Value.isInvalid())
7377 return nullptr;
7378
7379 // OpenMP [2.9.1, Restrictions]
7380 // The device expression must evaluate to a non-negative integer value.
7381 llvm::APSInt Result;
7382 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7383 Result.isSigned() && !Result.isStrictlyPositive()) {
7384 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7385 << "device" << ValExpr->getSourceRange();
7386 return nullptr;
7387 }
7388 }
Michael Wonge710d542015-08-07 16:16:36 +00007389 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7390}