1 //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit OpenMP nodes as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGCleanup.h"
14 #include "CGOpenMPRuntime.h"
15 #include "CodeGenFunction.h"
16 #include "CodeGenModule.h"
17 #include "TargetInfo.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/DeclOpenMP.h"
21 #include "clang/AST/OpenMPClause.h"
22 #include "clang/AST/Stmt.h"
23 #include "clang/AST/StmtOpenMP.h"
24 #include "clang/Basic/OpenMPKinds.h"
25 #include "clang/Basic/PrettyStackTrace.h"
26 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/Support/AtomicOrdering.h"
30 using namespace clang;
31 using namespace CodeGen;
32 using namespace llvm::omp;
33 
34 namespace {
35 /// Lexical scope for OpenMP executable constructs, that handles correct codegen
36 /// for captured expressions.
37 class OMPLexicalScope : public CodeGenFunction::LexicalScope {
38   void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
39     for (const auto *C : S.clauses()) {
40       if (const auto *CPI = OMPClauseWithPreInit::get(C)) {
41         if (const auto *PreInit =
42                 cast_or_null<DeclStmt>(CPI->getPreInitStmt())) {
43           for (const auto *I : PreInit->decls()) {
44             if (!I->hasAttr<OMPCaptureNoInitAttr>()) {
45               CGF.EmitVarDecl(cast<VarDecl>(*I));
46             } else {
47               CodeGenFunction::AutoVarEmission Emission =
48                   CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
49               CGF.EmitAutoVarCleanups(Emission);
50             }
51           }
52         }
53       }
54     }
55   }
56   CodeGenFunction::OMPPrivateScope InlinedShareds;
57 
58   static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) {
59     return CGF.LambdaCaptureFields.lookup(VD) ||
60            (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) ||
61            (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) &&
62             cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD));
63   }
64 
65 public:
66   OMPLexicalScope(
67       CodeGenFunction &CGF, const OMPExecutableDirective &S,
68       const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None,
69       const bool EmitPreInitStmt = true)
70       : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
71         InlinedShareds(CGF) {
72     if (EmitPreInitStmt)
73       emitPreInitStmt(CGF, S);
74     if (!CapturedRegion.hasValue())
75       return;
76     assert(S.hasAssociatedStmt() &&
77            "Expected associated statement for inlined directive.");
78     const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion);
79     for (const auto &C : CS->captures()) {
80       if (C.capturesVariable() || C.capturesVariableByCopy()) {
81         auto *VD = C.getCapturedVar();
82         assert(VD == VD->getCanonicalDecl() &&
83                "Canonical decl must be captured.");
84         DeclRefExpr DRE(
85             CGF.getContext(), const_cast<VarDecl *>(VD),
86             isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo &&
87                                        InlinedShareds.isGlobalVarCaptured(VD)),
88             VD->getType().getNonReferenceType(), VK_LValue, C.getLocation());
89         InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
90           return CGF.EmitLValue(&DRE).getAddress(CGF);
91         });
92       }
93     }
94     (void)InlinedShareds.Privatize();
95   }
96 };
97 
98 /// Lexical scope for OpenMP parallel construct, that handles correct codegen
99 /// for captured expressions.
100 class OMPParallelScope final : public OMPLexicalScope {
101   bool EmitPreInitStmt(const OMPExecutableDirective &S) {
102     OpenMPDirectiveKind Kind = S.getDirectiveKind();
103     return !(isOpenMPTargetExecutionDirective(Kind) ||
104              isOpenMPLoopBoundSharingDirective(Kind)) &&
105            isOpenMPParallelDirective(Kind);
106   }
107 
108 public:
109   OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
110       : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None,
111                         EmitPreInitStmt(S)) {}
112 };
113 
114 /// Lexical scope for OpenMP teams construct, that handles correct codegen
115 /// for captured expressions.
116 class OMPTeamsScope final : public OMPLexicalScope {
117   bool EmitPreInitStmt(const OMPExecutableDirective &S) {
118     OpenMPDirectiveKind Kind = S.getDirectiveKind();
119     return !isOpenMPTargetExecutionDirective(Kind) &&
120            isOpenMPTeamsDirective(Kind);
121   }
122 
123 public:
124   OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
125       : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None,
126                         EmitPreInitStmt(S)) {}
127 };
128 
129 /// Private scope for OpenMP loop-based directives, that supports capturing
130 /// of used expression from loop statement.
131 class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
132   void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) {
133     CodeGenFunction::OMPMapVars PreCondVars;
134     llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
135     for (const auto *E : S.counters()) {
136       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
137       EmittedAsPrivate.insert(VD->getCanonicalDecl());
138       (void)PreCondVars.setVarAddr(
139           CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType()));
140     }
141     // Mark private vars as undefs.
142     for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
143       for (const Expr *IRef : C->varlists()) {
144         const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
145         if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
146           (void)PreCondVars.setVarAddr(
147               CGF, OrigVD,
148               Address(llvm::UndefValue::get(
149                           CGF.ConvertTypeForMem(CGF.getContext().getPointerType(
150                               OrigVD->getType().getNonReferenceType()))),
151                       CGF.getContext().getDeclAlign(OrigVD)));
152         }
153       }
154     }
155     (void)PreCondVars.apply(CGF);
156     // Emit init, __range and __end variables for C++ range loops.
157     const Stmt *Body =
158         S.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers();
159     for (unsigned Cnt = 0; Cnt < S.getCollapsedNumber(); ++Cnt) {
160       Body = OMPLoopDirective::tryToFindNextInnerLoop(
161           Body, /*TryImperfectlyNestedLoops=*/true);
162       if (auto *For = dyn_cast<ForStmt>(Body)) {
163         Body = For->getBody();
164       } else {
165         assert(isa<CXXForRangeStmt>(Body) &&
166                "Expected canonical for loop or range-based for loop.");
167         auto *CXXFor = cast<CXXForRangeStmt>(Body);
168         if (const Stmt *Init = CXXFor->getInit())
169           CGF.EmitStmt(Init);
170         CGF.EmitStmt(CXXFor->getRangeStmt());
171         CGF.EmitStmt(CXXFor->getEndStmt());
172         Body = CXXFor->getBody();
173       }
174     }
175     if (const auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) {
176       for (const auto *I : PreInits->decls())
177         CGF.EmitVarDecl(cast<VarDecl>(*I));
178     }
179     PreCondVars.restore(CGF);
180   }
181 
182 public:
183   OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S)
184       : CodeGenFunction::RunCleanupsScope(CGF) {
185     emitPreInitStmt(CGF, S);
186   }
187 };
188 
189 class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope {
190   CodeGenFunction::OMPPrivateScope InlinedShareds;
191 
192   static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) {
193     return CGF.LambdaCaptureFields.lookup(VD) ||
194            (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) ||
195            (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) &&
196             cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD));
197   }
198 
199 public:
200   OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S)
201       : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
202         InlinedShareds(CGF) {
203     for (const auto *C : S.clauses()) {
204       if (const auto *CPI = OMPClauseWithPreInit::get(C)) {
205         if (const auto *PreInit =
206                 cast_or_null<DeclStmt>(CPI->getPreInitStmt())) {
207           for (const auto *I : PreInit->decls()) {
208             if (!I->hasAttr<OMPCaptureNoInitAttr>()) {
209               CGF.EmitVarDecl(cast<VarDecl>(*I));
210             } else {
211               CodeGenFunction::AutoVarEmission Emission =
212                   CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
213               CGF.EmitAutoVarCleanups(Emission);
214             }
215           }
216         }
217       } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) {
218         for (const Expr *E : UDP->varlists()) {
219           const Decl *D = cast<DeclRefExpr>(E)->getDecl();
220           if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D))
221             CGF.EmitVarDecl(*OED);
222         }
223       }
224     }
225     if (!isOpenMPSimdDirective(S.getDirectiveKind()))
226       CGF.EmitOMPPrivateClause(S, InlinedShareds);
227     if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) {
228       if (const Expr *E = TG->getReductionRef())
229         CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()));
230     }
231     const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt());
232     while (CS) {
233       for (auto &C : CS->captures()) {
234         if (C.capturesVariable() || C.capturesVariableByCopy()) {
235           auto *VD = C.getCapturedVar();
236           assert(VD == VD->getCanonicalDecl() &&
237                  "Canonical decl must be captured.");
238           DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD),
239                           isCapturedVar(CGF, VD) ||
240                               (CGF.CapturedStmtInfo &&
241                                InlinedShareds.isGlobalVarCaptured(VD)),
242                           VD->getType().getNonReferenceType(), VK_LValue,
243                           C.getLocation());
244           InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
245             return CGF.EmitLValue(&DRE).getAddress(CGF);
246           });
247         }
248       }
249       CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt());
250     }
251     (void)InlinedShareds.Privatize();
252   }
253 };
254 
255 } // namespace
256 
257 static void emitCommonOMPTargetDirective(CodeGenFunction &CGF,
258                                          const OMPExecutableDirective &S,
259                                          const RegionCodeGenTy &CodeGen);
260 
261 LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) {
262   if (const auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) {
263     if (const auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) {
264       OrigVD = OrigVD->getCanonicalDecl();
265       bool IsCaptured =
266           LambdaCaptureFields.lookup(OrigVD) ||
267           (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) ||
268           (CurCodeDecl && isa<BlockDecl>(CurCodeDecl));
269       DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), IsCaptured,
270                       OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc());
271       return EmitLValue(&DRE);
272     }
273   }
274   return EmitLValue(E);
275 }
276 
277 llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
278   ASTContext &C = getContext();
279   llvm::Value *Size = nullptr;
280   auto SizeInChars = C.getTypeSizeInChars(Ty);
281   if (SizeInChars.isZero()) {
282     // getTypeSizeInChars() returns 0 for a VLA.
283     while (const VariableArrayType *VAT = C.getAsVariableArrayType(Ty)) {
284       VlaSizePair VlaSize = getVLASize(VAT);
285       Ty = VlaSize.Type;
286       Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts)
287                   : VlaSize.NumElts;
288     }
289     SizeInChars = C.getTypeSizeInChars(Ty);
290     if (SizeInChars.isZero())
291       return llvm::ConstantInt::get(SizeTy, /*V=*/0);
292     return Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
293   }
294   return CGM.getSize(SizeInChars);
295 }
296 
297 void CodeGenFunction::GenerateOpenMPCapturedVars(
298     const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
299   const RecordDecl *RD = S.getCapturedRecordDecl();
300   auto CurField = RD->field_begin();
301   auto CurCap = S.captures().begin();
302   for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
303                                                  E = S.capture_init_end();
304        I != E; ++I, ++CurField, ++CurCap) {
305     if (CurField->hasCapturedVLAType()) {
306       const VariableArrayType *VAT = CurField->getCapturedVLAType();
307       llvm::Value *Val = VLASizeMap[VAT->getSizeExpr()];
308       CapturedVars.push_back(Val);
309     } else if (CurCap->capturesThis()) {
310       CapturedVars.push_back(CXXThisValue);
311     } else if (CurCap->capturesVariableByCopy()) {
312       llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation());
313 
314       // If the field is not a pointer, we need to save the actual value
315       // and load it as a void pointer.
316       if (!CurField->getType()->isAnyPointerType()) {
317         ASTContext &Ctx = getContext();
318         Address DstAddr = CreateMemTemp(
319             Ctx.getUIntPtrType(),
320             Twine(CurCap->getCapturedVar()->getName(), ".casted"));
321         LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType());
322 
323         llvm::Value *SrcAddrVal = EmitScalarConversion(
324             DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()),
325             Ctx.getPointerType(CurField->getType()), CurCap->getLocation());
326         LValue SrcLV =
327             MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType());
328 
329         // Store the value using the source type pointer.
330         EmitStoreThroughLValue(RValue::get(CV), SrcLV);
331 
332         // Load the value using the destination type pointer.
333         CV = EmitLoadOfScalar(DstLV, CurCap->getLocation());
334       }
335       CapturedVars.push_back(CV);
336     } else {
337       assert(CurCap->capturesVariable() && "Expected capture by reference.");
338       CapturedVars.push_back(EmitLValue(*I).getAddress(*this).getPointer());
339     }
340   }
341 }
342 
343 static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc,
344                                     QualType DstType, StringRef Name,
345                                     LValue AddrLV) {
346   ASTContext &Ctx = CGF.getContext();
347 
348   llvm::Value *CastedPtr = CGF.EmitScalarConversion(
349       AddrLV.getAddress(CGF).getPointer(), Ctx.getUIntPtrType(),
350       Ctx.getPointerType(DstType), Loc);
351   Address TmpAddr =
352       CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
353           .getAddress(CGF);
354   return TmpAddr;
355 }
356 
357 static QualType getCanonicalParamType(ASTContext &C, QualType T) {
358   if (T->isLValueReferenceType())
359     return C.getLValueReferenceType(
360         getCanonicalParamType(C, T.getNonReferenceType()),
361         /*SpelledAsLValue=*/false);
362   if (T->isPointerType())
363     return C.getPointerType(getCanonicalParamType(C, T->getPointeeType()));
364   if (const ArrayType *A = T->getAsArrayTypeUnsafe()) {
365     if (const auto *VLA = dyn_cast<VariableArrayType>(A))
366       return getCanonicalParamType(C, VLA->getElementType());
367     if (!A->isVariablyModifiedType())
368       return C.getCanonicalType(T);
369   }
370   return C.getCanonicalParamType(T);
371 }
372 
373 namespace {
374 /// Contains required data for proper outlined function codegen.
375 struct FunctionOptions {
376   /// Captured statement for which the function is generated.
377   const CapturedStmt *S = nullptr;
378   /// true if cast to/from  UIntPtr is required for variables captured by
379   /// value.
380   const bool UIntPtrCastRequired = true;
381   /// true if only casted arguments must be registered as local args or VLA
382   /// sizes.
383   const bool RegisterCastedArgsOnly = false;
384   /// Name of the generated function.
385   const StringRef FunctionName;
386   /// Location of the non-debug version of the outlined function.
387   SourceLocation Loc;
388   explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired,
389                            bool RegisterCastedArgsOnly, StringRef FunctionName,
390                            SourceLocation Loc)
391       : S(S), UIntPtrCastRequired(UIntPtrCastRequired),
392         RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly),
393         FunctionName(FunctionName), Loc(Loc) {}
394 };
395 } // namespace
396 
397 static llvm::Function *emitOutlinedFunctionPrologue(
398     CodeGenFunction &CGF, FunctionArgList &Args,
399     llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>>
400         &LocalAddrs,
401     llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>>
402         &VLASizes,
403     llvm::Value *&CXXThisValue, const FunctionOptions &FO) {
404   const CapturedDecl *CD = FO.S->getCapturedDecl();
405   const RecordDecl *RD = FO.S->getCapturedRecordDecl();
406   assert(CD->hasBody() && "missing CapturedDecl body");
407 
408   CXXThisValue = nullptr;
409   // Build the argument list.
410   CodeGenModule &CGM = CGF.CGM;
411   ASTContext &Ctx = CGM.getContext();
412   FunctionArgList TargetArgs;
413   Args.append(CD->param_begin(),
414               std::next(CD->param_begin(), CD->getContextParamPosition()));
415   TargetArgs.append(
416       CD->param_begin(),
417       std::next(CD->param_begin(), CD->getContextParamPosition()));
418   auto I = FO.S->captures().begin();
419   FunctionDecl *DebugFunctionDecl = nullptr;
420   if (!FO.UIntPtrCastRequired) {
421     FunctionProtoType::ExtProtoInfo EPI;
422     QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI);
423     DebugFunctionDecl = FunctionDecl::Create(
424         Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(),
425         SourceLocation(), DeclarationName(), FunctionTy,
426         Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static,
427         /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false);
428   }
429   for (const FieldDecl *FD : RD->fields()) {
430     QualType ArgType = FD->getType();
431     IdentifierInfo *II = nullptr;
432     VarDecl *CapVar = nullptr;
433 
434     // If this is a capture by copy and the type is not a pointer, the outlined
435     // function argument type should be uintptr and the value properly casted to
436     // uintptr. This is necessary given that the runtime library is only able to
437     // deal with pointers. We can pass in the same way the VLA type sizes to the
438     // outlined function.
439     if (FO.UIntPtrCastRequired &&
440         ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
441          I->capturesVariableArrayType()))
442       ArgType = Ctx.getUIntPtrType();
443 
444     if (I->capturesVariable() || I->capturesVariableByCopy()) {
445       CapVar = I->getCapturedVar();
446       II = CapVar->getIdentifier();
447     } else if (I->capturesThis()) {
448       II = &Ctx.Idents.get("this");
449     } else {
450       assert(I->capturesVariableArrayType());
451       II = &Ctx.Idents.get("vla");
452     }
453     if (ArgType->isVariablyModifiedType())
454       ArgType = getCanonicalParamType(Ctx, ArgType);
455     VarDecl *Arg;
456     if (DebugFunctionDecl && (CapVar || I->capturesThis())) {
457       Arg = ParmVarDecl::Create(
458           Ctx, DebugFunctionDecl,
459           CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(),
460           CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType,
461           /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr);
462     } else {
463       Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(),
464                                       II, ArgType, ImplicitParamDecl::Other);
465     }
466     Args.emplace_back(Arg);
467     // Do not cast arguments if we emit function with non-original types.
468     TargetArgs.emplace_back(
469         FO.UIntPtrCastRequired
470             ? Arg
471             : CGM.getOpenMPRuntime().translateParameter(FD, Arg));
472     ++I;
473   }
474   Args.append(
475       std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
476       CD->param_end());
477   TargetArgs.append(
478       std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
479       CD->param_end());
480 
481   // Create the function declaration.
482   const CGFunctionInfo &FuncInfo =
483       CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs);
484   llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
485 
486   auto *F =
487       llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
488                              FO.FunctionName, &CGM.getModule());
489   CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
490   if (CD->isNothrow())
491     F->setDoesNotThrow();
492   F->setDoesNotRecurse();
493 
494   // Generate the function.
495   CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs,
496                     FO.UIntPtrCastRequired ? FO.Loc : FO.S->getBeginLoc(),
497                     FO.UIntPtrCastRequired ? FO.Loc
498                                            : CD->getBody()->getBeginLoc());
499   unsigned Cnt = CD->getContextParamPosition();
500   I = FO.S->captures().begin();
501   for (const FieldDecl *FD : RD->fields()) {
502     // Do not map arguments if we emit function with non-original types.
503     Address LocalAddr(Address::invalid());
504     if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) {
505       LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt],
506                                                              TargetArgs[Cnt]);
507     } else {
508       LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]);
509     }
510     // If we are capturing a pointer by copy we don't need to do anything, just
511     // use the value that we get from the arguments.
512     if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
513       const VarDecl *CurVD = I->getCapturedVar();
514       if (!FO.RegisterCastedArgsOnly)
515         LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}});
516       ++Cnt;
517       ++I;
518       continue;
519     }
520 
521     LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(),
522                                         AlignmentSource::Decl);
523     if (FD->hasCapturedVLAType()) {
524       if (FO.UIntPtrCastRequired) {
525         ArgLVal = CGF.MakeAddrLValue(
526             castValueFromUintptr(CGF, I->getLocation(), FD->getType(),
527                                  Args[Cnt]->getName(), ArgLVal),
528             FD->getType(), AlignmentSource::Decl);
529       }
530       llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation());
531       const VariableArrayType *VAT = FD->getCapturedVLAType();
532       VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg);
533     } else if (I->capturesVariable()) {
534       const VarDecl *Var = I->getCapturedVar();
535       QualType VarTy = Var->getType();
536       Address ArgAddr = ArgLVal.getAddress(CGF);
537       if (ArgLVal.getType()->isLValueReferenceType()) {
538         ArgAddr = CGF.EmitLoadOfReference(ArgLVal);
539       } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) {
540         assert(ArgLVal.getType()->isPointerType());
541         ArgAddr = CGF.EmitLoadOfPointer(
542             ArgAddr, ArgLVal.getType()->castAs<PointerType>());
543       }
544       if (!FO.RegisterCastedArgsOnly) {
545         LocalAddrs.insert(
546             {Args[Cnt],
547              {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}});
548       }
549     } else if (I->capturesVariableByCopy()) {
550       assert(!FD->getType()->isAnyPointerType() &&
551              "Not expecting a captured pointer.");
552       const VarDecl *Var = I->getCapturedVar();
553       LocalAddrs.insert({Args[Cnt],
554                          {Var, FO.UIntPtrCastRequired
555                                    ? castValueFromUintptr(
556                                          CGF, I->getLocation(), FD->getType(),
557                                          Args[Cnt]->getName(), ArgLVal)
558                                    : ArgLVal.getAddress(CGF)}});
559     } else {
560       // If 'this' is captured, load it into CXXThisValue.
561       assert(I->capturesThis());
562       CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation());
563       LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress(CGF)}});
564     }
565     ++Cnt;
566     ++I;
567   }
568 
569   return F;
570 }
571 
572 llvm::Function *
573 CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S,
574                                                     SourceLocation Loc) {
575   assert(
576       CapturedStmtInfo &&
577       "CapturedStmtInfo should be set when generating the captured function");
578   const CapturedDecl *CD = S.getCapturedDecl();
579   // Build the argument list.
580   bool NeedWrapperFunction =
581       getDebugInfo() && CGM.getCodeGenOpts().hasReducedDebugInfo();
582   FunctionArgList Args;
583   llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs;
584   llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes;
585   SmallString<256> Buffer;
586   llvm::raw_svector_ostream Out(Buffer);
587   Out << CapturedStmtInfo->getHelperName();
588   if (NeedWrapperFunction)
589     Out << "_debug__";
590   FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false,
591                      Out.str(), Loc);
592   llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs,
593                                                    VLASizes, CXXThisValue, FO);
594   CodeGenFunction::OMPPrivateScope LocalScope(*this);
595   for (const auto &LocalAddrPair : LocalAddrs) {
596     if (LocalAddrPair.second.first) {
597       LocalScope.addPrivate(LocalAddrPair.second.first, [&LocalAddrPair]() {
598         return LocalAddrPair.second.second;
599       });
600     }
601   }
602   (void)LocalScope.Privatize();
603   for (const auto &VLASizePair : VLASizes)
604     VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second;
605   PGO.assignRegionCounters(GlobalDecl(CD), F);
606   CapturedStmtInfo->EmitBody(*this, CD->getBody());
607   (void)LocalScope.ForceCleanup();
608   FinishFunction(CD->getBodyRBrace());
609   if (!NeedWrapperFunction)
610     return F;
611 
612   FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true,
613                             /*RegisterCastedArgsOnly=*/true,
614                             CapturedStmtInfo->getHelperName(), Loc);
615   CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true);
616   WrapperCGF.CapturedStmtInfo = CapturedStmtInfo;
617   Args.clear();
618   LocalAddrs.clear();
619   VLASizes.clear();
620   llvm::Function *WrapperF =
621       emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes,
622                                    WrapperCGF.CXXThisValue, WrapperFO);
623   llvm::SmallVector<llvm::Value *, 4> CallArgs;
624   for (const auto *Arg : Args) {
625     llvm::Value *CallArg;
626     auto I = LocalAddrs.find(Arg);
627     if (I != LocalAddrs.end()) {
628       LValue LV = WrapperCGF.MakeAddrLValue(
629           I->second.second,
630           I->second.first ? I->second.first->getType() : Arg->getType(),
631           AlignmentSource::Decl);
632       CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc());
633     } else {
634       auto EI = VLASizes.find(Arg);
635       if (EI != VLASizes.end()) {
636         CallArg = EI->second.second;
637       } else {
638         LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg),
639                                               Arg->getType(),
640                                               AlignmentSource::Decl);
641         CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc());
642       }
643     }
644     CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType()));
645   }
646   CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, Loc, F, CallArgs);
647   WrapperCGF.FinishFunction();
648   return WrapperF;
649 }
650 
651 //===----------------------------------------------------------------------===//
652 //                              OpenMP Directive Emission
653 //===----------------------------------------------------------------------===//
654 void CodeGenFunction::EmitOMPAggregateAssign(
655     Address DestAddr, Address SrcAddr, QualType OriginalType,
656     const llvm::function_ref<void(Address, Address)> CopyGen) {
657   // Perform element-by-element initialization.
658   QualType ElementTy;
659 
660   // Drill down to the base element type on both arrays.
661   const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe();
662   llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
663   SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
664 
665   llvm::Value *SrcBegin = SrcAddr.getPointer();
666   llvm::Value *DestBegin = DestAddr.getPointer();
667   // Cast from pointer to array type to pointer to single element.
668   llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements);
669   // The basic structure here is a while-do loop.
670   llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body");
671   llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done");
672   llvm::Value *IsEmpty =
673       Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
674   Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
675 
676   // Enter the loop body, making that address the current address.
677   llvm::BasicBlock *EntryBB = Builder.GetInsertBlock();
678   EmitBlock(BodyBB);
679 
680   CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
681 
682   llvm::PHINode *SrcElementPHI =
683     Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
684   SrcElementPHI->addIncoming(SrcBegin, EntryBB);
685   Address SrcElementCurrent =
686       Address(SrcElementPHI,
687               SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
688 
689   llvm::PHINode *DestElementPHI =
690     Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
691   DestElementPHI->addIncoming(DestBegin, EntryBB);
692   Address DestElementCurrent =
693     Address(DestElementPHI,
694             DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
695 
696   // Emit copy.
697   CopyGen(DestElementCurrent, SrcElementCurrent);
698 
699   // Shift the address forward by one element.
700   llvm::Value *DestElementNext = Builder.CreateConstGEP1_32(
701       DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
702   llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32(
703       SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
704   // Check whether we've reached the end.
705   llvm::Value *Done =
706       Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
707   Builder.CreateCondBr(Done, DoneBB, BodyBB);
708   DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
709   SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
710 
711   // Done.
712   EmitBlock(DoneBB, /*IsFinished=*/true);
713 }
714 
715 void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
716                                   Address SrcAddr, const VarDecl *DestVD,
717                                   const VarDecl *SrcVD, const Expr *Copy) {
718   if (OriginalType->isArrayType()) {
719     const auto *BO = dyn_cast<BinaryOperator>(Copy);
720     if (BO && BO->getOpcode() == BO_Assign) {
721       // Perform simple memcpy for simple copying.
722       LValue Dest = MakeAddrLValue(DestAddr, OriginalType);
723       LValue Src = MakeAddrLValue(SrcAddr, OriginalType);
724       EmitAggregateAssign(Dest, Src, OriginalType);
725     } else {
726       // For arrays with complex element types perform element by element
727       // copying.
728       EmitOMPAggregateAssign(
729           DestAddr, SrcAddr, OriginalType,
730           [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
731             // Working with the single array element, so have to remap
732             // destination and source variables to corresponding array
733             // elements.
734             CodeGenFunction::OMPPrivateScope Remap(*this);
735             Remap.addPrivate(DestVD, [DestElement]() { return DestElement; });
736             Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; });
737             (void)Remap.Privatize();
738             EmitIgnoredExpr(Copy);
739           });
740     }
741   } else {
742     // Remap pseudo source variable to private copy.
743     CodeGenFunction::OMPPrivateScope Remap(*this);
744     Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; });
745     Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; });
746     (void)Remap.Privatize();
747     // Emit copying of the whole variable.
748     EmitIgnoredExpr(Copy);
749   }
750 }
751 
752 bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
753                                                 OMPPrivateScope &PrivateScope) {
754   if (!HaveInsertPoint())
755     return false;
756   bool DeviceConstTarget =
757       getLangOpts().OpenMPIsDevice &&
758       isOpenMPTargetExecutionDirective(D.getDirectiveKind());
759   bool FirstprivateIsLastprivate = false;
760   llvm::DenseMap<const VarDecl *, OpenMPLastprivateModifier> Lastprivates;
761   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
762     for (const auto *D : C->varlists())
763       Lastprivates.try_emplace(
764           cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl(),
765           C->getKind());
766   }
767   llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
768   llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
769   getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind());
770   // Force emission of the firstprivate copy if the directive does not emit
771   // outlined function, like omp for, omp simd, omp distribute etc.
772   bool MustEmitFirstprivateCopy =
773       CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown;
774   for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
775     const auto *IRef = C->varlist_begin();
776     const auto *InitsRef = C->inits().begin();
777     for (const Expr *IInit : C->private_copies()) {
778       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
779       bool ThisFirstprivateIsLastprivate =
780           Lastprivates.count(OrigVD->getCanonicalDecl()) > 0;
781       const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD);
782       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
783       if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD &&
784           !FD->getType()->isReferenceType() &&
785           (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) {
786         EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl());
787         ++IRef;
788         ++InitsRef;
789         continue;
790       }
791       // Do not emit copy for firstprivate constant variables in target regions,
792       // captured by reference.
793       if (DeviceConstTarget && OrigVD->getType().isConstant(getContext()) &&
794           FD && FD->getType()->isReferenceType() &&
795           (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) {
796         (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this,
797                                                                     OrigVD);
798         ++IRef;
799         ++InitsRef;
800         continue;
801       }
802       FirstprivateIsLastprivate =
803           FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate;
804       if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
805         const auto *VDInit =
806             cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
807         bool IsRegistered;
808         DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
809                         /*RefersToEnclosingVariableOrCapture=*/FD != nullptr,
810                         (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
811         LValue OriginalLVal;
812         if (!FD) {
813           // Check if the firstprivate variable is just a constant value.
814           ConstantEmission CE = tryEmitAsConstant(&DRE);
815           if (CE && !CE.isReference()) {
816             // Constant value, no need to create a copy.
817             ++IRef;
818             ++InitsRef;
819             continue;
820           }
821           if (CE && CE.isReference()) {
822             OriginalLVal = CE.getReferenceLValue(*this, &DRE);
823           } else {
824             assert(!CE && "Expected non-constant firstprivate.");
825             OriginalLVal = EmitLValue(&DRE);
826           }
827         } else {
828           OriginalLVal = EmitLValue(&DRE);
829         }
830         QualType Type = VD->getType();
831         if (Type->isArrayType()) {
832           // Emit VarDecl with copy init for arrays.
833           // Get the address of the original variable captured in current
834           // captured region.
835           IsRegistered = PrivateScope.addPrivate(
836               OrigVD, [this, VD, Type, OriginalLVal, VDInit]() {
837                 AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
838                 const Expr *Init = VD->getInit();
839                 if (!isa<CXXConstructExpr>(Init) ||
840                     isTrivialInitializer(Init)) {
841                   // Perform simple memcpy.
842                   LValue Dest =
843                       MakeAddrLValue(Emission.getAllocatedAddress(), Type);
844                   EmitAggregateAssign(Dest, OriginalLVal, Type);
845                 } else {
846                   EmitOMPAggregateAssign(
847                       Emission.getAllocatedAddress(),
848                       OriginalLVal.getAddress(*this), Type,
849                       [this, VDInit, Init](Address DestElement,
850                                            Address SrcElement) {
851                         // Clean up any temporaries needed by the
852                         // initialization.
853                         RunCleanupsScope InitScope(*this);
854                         // Emit initialization for single element.
855                         setAddrOfLocalVar(VDInit, SrcElement);
856                         EmitAnyExprToMem(Init, DestElement,
857                                          Init->getType().getQualifiers(),
858                                          /*IsInitializer*/ false);
859                         LocalDeclMap.erase(VDInit);
860                       });
861                 }
862                 EmitAutoVarCleanups(Emission);
863                 return Emission.getAllocatedAddress();
864               });
865         } else {
866           Address OriginalAddr = OriginalLVal.getAddress(*this);
867           IsRegistered =
868               PrivateScope.addPrivate(OrigVD, [this, VDInit, OriginalAddr, VD,
869                                                ThisFirstprivateIsLastprivate,
870                                                OrigVD, &Lastprivates, IRef]() {
871                 // Emit private VarDecl with copy init.
872                 // Remap temp VDInit variable to the address of the original
873                 // variable (for proper handling of captured global variables).
874                 setAddrOfLocalVar(VDInit, OriginalAddr);
875                 EmitDecl(*VD);
876                 LocalDeclMap.erase(VDInit);
877                 if (ThisFirstprivateIsLastprivate &&
878                     Lastprivates[OrigVD->getCanonicalDecl()] ==
879                         OMPC_LASTPRIVATE_conditional) {
880                   // Create/init special variable for lastprivate conditionals.
881                   Address VDAddr =
882                       CGM.getOpenMPRuntime().emitLastprivateConditionalInit(
883                           *this, OrigVD);
884                   llvm::Value *V = EmitLoadOfScalar(
885                       MakeAddrLValue(GetAddrOfLocalVar(VD), (*IRef)->getType(),
886                                      AlignmentSource::Decl),
887                       (*IRef)->getExprLoc());
888                   EmitStoreOfScalar(V,
889                                     MakeAddrLValue(VDAddr, (*IRef)->getType(),
890                                                    AlignmentSource::Decl));
891                   LocalDeclMap.erase(VD);
892                   setAddrOfLocalVar(VD, VDAddr);
893                   return VDAddr;
894                 }
895                 return GetAddrOfLocalVar(VD);
896               });
897         }
898         assert(IsRegistered &&
899                "firstprivate var already registered as private");
900         // Silence the warning about unused variable.
901         (void)IsRegistered;
902       }
903       ++IRef;
904       ++InitsRef;
905     }
906   }
907   return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
908 }
909 
910 void CodeGenFunction::EmitOMPPrivateClause(
911     const OMPExecutableDirective &D,
912     CodeGenFunction::OMPPrivateScope &PrivateScope) {
913   if (!HaveInsertPoint())
914     return;
915   llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
916   for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
917     auto IRef = C->varlist_begin();
918     for (const Expr *IInit : C->private_copies()) {
919       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
920       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
921         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
922         bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() {
923           // Emit private VarDecl with copy init.
924           EmitDecl(*VD);
925           return GetAddrOfLocalVar(VD);
926         });
927         assert(IsRegistered && "private var already registered as private");
928         // Silence the warning about unused variable.
929         (void)IsRegistered;
930       }
931       ++IRef;
932     }
933   }
934 }
935 
936 bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
937   if (!HaveInsertPoint())
938     return false;
939   // threadprivate_var1 = master_threadprivate_var1;
940   // operator=(threadprivate_var2, master_threadprivate_var2);
941   // ...
942   // __kmpc_barrier(&loc, global_tid);
943   llvm::DenseSet<const VarDecl *> CopiedVars;
944   llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
945   for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
946     auto IRef = C->varlist_begin();
947     auto ISrcRef = C->source_exprs().begin();
948     auto IDestRef = C->destination_exprs().begin();
949     for (const Expr *AssignOp : C->assignment_ops()) {
950       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
951       QualType Type = VD->getType();
952       if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
953         // Get the address of the master variable. If we are emitting code with
954         // TLS support, the address is passed from the master as field in the
955         // captured declaration.
956         Address MasterAddr = Address::invalid();
957         if (getLangOpts().OpenMPUseTLS &&
958             getContext().getTargetInfo().isTLSSupported()) {
959           assert(CapturedStmtInfo->lookup(VD) &&
960                  "Copyin threadprivates should have been captured!");
961           DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), true,
962                           (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
963           MasterAddr = EmitLValue(&DRE).getAddress(*this);
964           LocalDeclMap.erase(VD);
965         } else {
966           MasterAddr =
967             Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
968                                         : CGM.GetAddrOfGlobal(VD),
969                     getContext().getDeclAlign(VD));
970         }
971         // Get the address of the threadprivate variable.
972         Address PrivateAddr = EmitLValue(*IRef).getAddress(*this);
973         if (CopiedVars.size() == 1) {
974           // At first check if current thread is a master thread. If it is, no
975           // need to copy data.
976           CopyBegin = createBasicBlock("copyin.not.master");
977           CopyEnd = createBasicBlock("copyin.not.master.end");
978           Builder.CreateCondBr(
979               Builder.CreateICmpNE(
980                   Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
981                   Builder.CreatePtrToInt(PrivateAddr.getPointer(),
982                                          CGM.IntPtrTy)),
983               CopyBegin, CopyEnd);
984           EmitBlock(CopyBegin);
985         }
986         const auto *SrcVD =
987             cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
988         const auto *DestVD =
989             cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
990         EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
991       }
992       ++IRef;
993       ++ISrcRef;
994       ++IDestRef;
995     }
996   }
997   if (CopyEnd) {
998     // Exit out of copying procedure for non-master thread.
999     EmitBlock(CopyEnd, /*IsFinished=*/true);
1000     return true;
1001   }
1002   return false;
1003 }
1004 
1005 bool CodeGenFunction::EmitOMPLastprivateClauseInit(
1006     const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
1007   if (!HaveInsertPoint())
1008     return false;
1009   bool HasAtLeastOneLastprivate = false;
1010   llvm::DenseSet<const VarDecl *> SIMDLCVs;
1011   if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1012     const auto *LoopDirective = cast<OMPLoopDirective>(&D);
1013     for (const Expr *C : LoopDirective->counters()) {
1014       SIMDLCVs.insert(
1015           cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1016     }
1017   }
1018   llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
1019   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
1020     HasAtLeastOneLastprivate = true;
1021     if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) &&
1022         !getLangOpts().OpenMPSimd)
1023       break;
1024     const auto *IRef = C->varlist_begin();
1025     const auto *IDestRef = C->destination_exprs().begin();
1026     for (const Expr *IInit : C->private_copies()) {
1027       // Keep the address of the original variable for future update at the end
1028       // of the loop.
1029       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1030       // Taskloops do not require additional initialization, it is done in
1031       // runtime support library.
1032       if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
1033         const auto *DestVD =
1034             cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
1035         PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() {
1036           DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
1037                           /*RefersToEnclosingVariableOrCapture=*/
1038                               CapturedStmtInfo->lookup(OrigVD) != nullptr,
1039                           (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
1040           return EmitLValue(&DRE).getAddress(*this);
1041         });
1042         // Check if the variable is also a firstprivate: in this case IInit is
1043         // not generated. Initialization of this variable will happen in codegen
1044         // for 'firstprivate' clause.
1045         if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
1046           const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
1047           bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD, C,
1048                                                                OrigVD]() {
1049             if (C->getKind() == OMPC_LASTPRIVATE_conditional) {
1050               Address VDAddr =
1051                   CGM.getOpenMPRuntime().emitLastprivateConditionalInit(*this,
1052                                                                         OrigVD);
1053               setAddrOfLocalVar(VD, VDAddr);
1054               return VDAddr;
1055             }
1056             // Emit private VarDecl with copy init.
1057             EmitDecl(*VD);
1058             return GetAddrOfLocalVar(VD);
1059           });
1060           assert(IsRegistered &&
1061                  "lastprivate var already registered as private");
1062           (void)IsRegistered;
1063         }
1064       }
1065       ++IRef;
1066       ++IDestRef;
1067     }
1068   }
1069   return HasAtLeastOneLastprivate;
1070 }
1071 
1072 void CodeGenFunction::EmitOMPLastprivateClauseFinal(
1073     const OMPExecutableDirective &D, bool NoFinals,
1074     llvm::Value *IsLastIterCond) {
1075   if (!HaveInsertPoint())
1076     return;
1077   // Emit following code:
1078   // if (<IsLastIterCond>) {
1079   //   orig_var1 = private_orig_var1;
1080   //   ...
1081   //   orig_varn = private_orig_varn;
1082   // }
1083   llvm::BasicBlock *ThenBB = nullptr;
1084   llvm::BasicBlock *DoneBB = nullptr;
1085   if (IsLastIterCond) {
1086     // Emit implicit barrier if at least one lastprivate conditional is found
1087     // and this is not a simd mode.
1088     if (!getLangOpts().OpenMPSimd &&
1089         llvm::any_of(D.getClausesOfKind<OMPLastprivateClause>(),
1090                      [](const OMPLastprivateClause *C) {
1091                        return C->getKind() == OMPC_LASTPRIVATE_conditional;
1092                      })) {
1093       CGM.getOpenMPRuntime().emitBarrierCall(*this, D.getBeginLoc(),
1094                                              OMPD_unknown,
1095                                              /*EmitChecks=*/false,
1096                                              /*ForceSimpleCall=*/true);
1097     }
1098     ThenBB = createBasicBlock(".omp.lastprivate.then");
1099     DoneBB = createBasicBlock(".omp.lastprivate.done");
1100     Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
1101     EmitBlock(ThenBB);
1102   }
1103   llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
1104   llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
1105   if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
1106     auto IC = LoopDirective->counters().begin();
1107     for (const Expr *F : LoopDirective->finals()) {
1108       const auto *D =
1109           cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
1110       if (NoFinals)
1111         AlreadyEmittedVars.insert(D);
1112       else
1113         LoopCountersAndUpdates[D] = F;
1114       ++IC;
1115     }
1116   }
1117   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
1118     auto IRef = C->varlist_begin();
1119     auto ISrcRef = C->source_exprs().begin();
1120     auto IDestRef = C->destination_exprs().begin();
1121     for (const Expr *AssignOp : C->assignment_ops()) {
1122       const auto *PrivateVD =
1123           cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
1124       QualType Type = PrivateVD->getType();
1125       const auto *CanonicalVD = PrivateVD->getCanonicalDecl();
1126       if (AlreadyEmittedVars.insert(CanonicalVD).second) {
1127         // If lastprivate variable is a loop control variable for loop-based
1128         // directive, update its value before copyin back to original
1129         // variable.
1130         if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
1131           EmitIgnoredExpr(FinalExpr);
1132         const auto *SrcVD =
1133             cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
1134         const auto *DestVD =
1135             cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
1136         // Get the address of the private variable.
1137         Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
1138         if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>())
1139           PrivateAddr =
1140               Address(Builder.CreateLoad(PrivateAddr),
1141                       CGM.getNaturalTypeAlignment(RefTy->getPointeeType()));
1142         // Store the last value to the private copy in the last iteration.
1143         if (C->getKind() == OMPC_LASTPRIVATE_conditional)
1144           CGM.getOpenMPRuntime().emitLastprivateConditionalFinalUpdate(
1145               *this, MakeAddrLValue(PrivateAddr, (*IRef)->getType()), PrivateVD,
1146               (*IRef)->getExprLoc());
1147         // Get the address of the original variable.
1148         Address OriginalAddr = GetAddrOfLocalVar(DestVD);
1149         EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
1150       }
1151       ++IRef;
1152       ++ISrcRef;
1153       ++IDestRef;
1154     }
1155     if (const Expr *PostUpdate = C->getPostUpdateExpr())
1156       EmitIgnoredExpr(PostUpdate);
1157   }
1158   if (IsLastIterCond)
1159     EmitBlock(DoneBB, /*IsFinished=*/true);
1160 }
1161 
1162 void CodeGenFunction::EmitOMPReductionClauseInit(
1163     const OMPExecutableDirective &D,
1164     CodeGenFunction::OMPPrivateScope &PrivateScope) {
1165   if (!HaveInsertPoint())
1166     return;
1167   SmallVector<const Expr *, 4> Shareds;
1168   SmallVector<const Expr *, 4> Privates;
1169   SmallVector<const Expr *, 4> ReductionOps;
1170   SmallVector<const Expr *, 4> LHSs;
1171   SmallVector<const Expr *, 4> RHSs;
1172   OMPTaskDataTy Data;
1173   SmallVector<const Expr *, 4> TaskLHSs;
1174   SmallVector<const Expr *, 4> TaskRHSs;
1175   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1176     Shareds.append(C->varlist_begin(), C->varlist_end());
1177     Privates.append(C->privates().begin(), C->privates().end());
1178     ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1179     LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1180     RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1181     if (C->getModifier() == OMPC_REDUCTION_task) {
1182       Data.ReductionVars.append(C->privates().begin(), C->privates().end());
1183       Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end());
1184       Data.ReductionCopies.append(C->privates().begin(), C->privates().end());
1185       Data.ReductionOps.append(C->reduction_ops().begin(),
1186                                C->reduction_ops().end());
1187       TaskLHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1188       TaskRHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1189     }
1190   }
1191   ReductionCodeGen RedCG(Shareds, Shareds, Privates, ReductionOps);
1192   unsigned Count = 0;
1193   auto *ILHS = LHSs.begin();
1194   auto *IRHS = RHSs.begin();
1195   auto *IPriv = Privates.begin();
1196   for (const Expr *IRef : Shareds) {
1197     const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
1198     // Emit private VarDecl with reduction init.
1199     RedCG.emitSharedOrigLValue(*this, Count);
1200     RedCG.emitAggregateType(*this, Count);
1201     AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1202     RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(),
1203                              RedCG.getSharedLValue(Count),
1204                              [&Emission](CodeGenFunction &CGF) {
1205                                CGF.EmitAutoVarInit(Emission);
1206                                return true;
1207                              });
1208     EmitAutoVarCleanups(Emission);
1209     Address BaseAddr = RedCG.adjustPrivateAddress(
1210         *this, Count, Emission.getAllocatedAddress());
1211     bool IsRegistered = PrivateScope.addPrivate(
1212         RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; });
1213     assert(IsRegistered && "private var already registered as private");
1214     // Silence the warning about unused variable.
1215     (void)IsRegistered;
1216 
1217     const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
1218     const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
1219     QualType Type = PrivateVD->getType();
1220     bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef);
1221     if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) {
1222       // Store the address of the original variable associated with the LHS
1223       // implicit variable.
1224       PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() {
1225         return RedCG.getSharedLValue(Count).getAddress(*this);
1226       });
1227       PrivateScope.addPrivate(
1228           RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); });
1229     } else if ((isaOMPArraySectionExpr && Type->isScalarType()) ||
1230                isa<ArraySubscriptExpr>(IRef)) {
1231       // Store the address of the original variable associated with the LHS
1232       // implicit variable.
1233       PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() {
1234         return RedCG.getSharedLValue(Count).getAddress(*this);
1235       });
1236       PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() {
1237         return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD),
1238                                             ConvertTypeForMem(RHSVD->getType()),
1239                                             "rhs.begin");
1240       });
1241     } else {
1242       QualType Type = PrivateVD->getType();
1243       bool IsArray = getContext().getAsArrayType(Type) != nullptr;
1244       Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(*this);
1245       // Store the address of the original variable associated with the LHS
1246       // implicit variable.
1247       if (IsArray) {
1248         OriginalAddr = Builder.CreateElementBitCast(
1249             OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1250       }
1251       PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; });
1252       PrivateScope.addPrivate(
1253           RHSVD, [this, PrivateVD, RHSVD, IsArray]() {
1254             return IsArray
1255                        ? Builder.CreateElementBitCast(
1256                              GetAddrOfLocalVar(PrivateVD),
1257                              ConvertTypeForMem(RHSVD->getType()), "rhs.begin")
1258                        : GetAddrOfLocalVar(PrivateVD);
1259           });
1260     }
1261     ++ILHS;
1262     ++IRHS;
1263     ++IPriv;
1264     ++Count;
1265   }
1266   if (!Data.ReductionVars.empty()) {
1267     Data.IsReductionWithTaskMod = true;
1268     Data.IsWorksharingReduction =
1269         isOpenMPWorksharingDirective(D.getDirectiveKind());
1270     llvm::Value *ReductionDesc = CGM.getOpenMPRuntime().emitTaskReductionInit(
1271         *this, D.getBeginLoc(), TaskLHSs, TaskRHSs, Data);
1272     const Expr *TaskRedRef = nullptr;
1273     switch (D.getDirectiveKind()) {
1274     case OMPD_parallel:
1275       TaskRedRef = cast<OMPParallelDirective>(D).getTaskReductionRefExpr();
1276       break;
1277     case OMPD_for:
1278       TaskRedRef = cast<OMPForDirective>(D).getTaskReductionRefExpr();
1279       break;
1280     case OMPD_sections:
1281       TaskRedRef = cast<OMPSectionsDirective>(D).getTaskReductionRefExpr();
1282       break;
1283     case OMPD_parallel_for:
1284       TaskRedRef = cast<OMPParallelForDirective>(D).getTaskReductionRefExpr();
1285       break;
1286     case OMPD_parallel_master:
1287       TaskRedRef =
1288           cast<OMPParallelMasterDirective>(D).getTaskReductionRefExpr();
1289       break;
1290     case OMPD_parallel_sections:
1291       TaskRedRef =
1292           cast<OMPParallelSectionsDirective>(D).getTaskReductionRefExpr();
1293       break;
1294     case OMPD_target_parallel:
1295       TaskRedRef =
1296           cast<OMPTargetParallelDirective>(D).getTaskReductionRefExpr();
1297       break;
1298     case OMPD_target_parallel_for:
1299       TaskRedRef =
1300           cast<OMPTargetParallelForDirective>(D).getTaskReductionRefExpr();
1301       break;
1302     case OMPD_distribute_parallel_for:
1303       TaskRedRef =
1304           cast<OMPDistributeParallelForDirective>(D).getTaskReductionRefExpr();
1305       break;
1306     case OMPD_teams_distribute_parallel_for:
1307       TaskRedRef = cast<OMPTeamsDistributeParallelForDirective>(D)
1308                        .getTaskReductionRefExpr();
1309       break;
1310     case OMPD_target_teams_distribute_parallel_for:
1311       TaskRedRef = cast<OMPTargetTeamsDistributeParallelForDirective>(D)
1312                        .getTaskReductionRefExpr();
1313       break;
1314     case OMPD_simd:
1315     case OMPD_for_simd:
1316     case OMPD_section:
1317     case OMPD_single:
1318     case OMPD_master:
1319     case OMPD_critical:
1320     case OMPD_parallel_for_simd:
1321     case OMPD_task:
1322     case OMPD_taskyield:
1323     case OMPD_barrier:
1324     case OMPD_taskwait:
1325     case OMPD_taskgroup:
1326     case OMPD_flush:
1327     case OMPD_depobj:
1328     case OMPD_scan:
1329     case OMPD_ordered:
1330     case OMPD_atomic:
1331     case OMPD_teams:
1332     case OMPD_target:
1333     case OMPD_cancellation_point:
1334     case OMPD_cancel:
1335     case OMPD_target_data:
1336     case OMPD_target_enter_data:
1337     case OMPD_target_exit_data:
1338     case OMPD_taskloop:
1339     case OMPD_taskloop_simd:
1340     case OMPD_master_taskloop:
1341     case OMPD_master_taskloop_simd:
1342     case OMPD_parallel_master_taskloop:
1343     case OMPD_parallel_master_taskloop_simd:
1344     case OMPD_distribute:
1345     case OMPD_target_update:
1346     case OMPD_distribute_parallel_for_simd:
1347     case OMPD_distribute_simd:
1348     case OMPD_target_parallel_for_simd:
1349     case OMPD_target_simd:
1350     case OMPD_teams_distribute:
1351     case OMPD_teams_distribute_simd:
1352     case OMPD_teams_distribute_parallel_for_simd:
1353     case OMPD_target_teams:
1354     case OMPD_target_teams_distribute:
1355     case OMPD_target_teams_distribute_parallel_for_simd:
1356     case OMPD_target_teams_distribute_simd:
1357     case OMPD_declare_target:
1358     case OMPD_end_declare_target:
1359     case OMPD_threadprivate:
1360     case OMPD_allocate:
1361     case OMPD_declare_reduction:
1362     case OMPD_declare_mapper:
1363     case OMPD_declare_simd:
1364     case OMPD_requires:
1365     case OMPD_declare_variant:
1366     case OMPD_begin_declare_variant:
1367     case OMPD_end_declare_variant:
1368     case OMPD_unknown:
1369       llvm_unreachable("Enexpected directive with task reductions.");
1370     }
1371 
1372     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(TaskRedRef)->getDecl());
1373     EmitVarDecl(*VD);
1374     EmitStoreOfScalar(ReductionDesc, GetAddrOfLocalVar(VD),
1375                       /*Volatile=*/false, TaskRedRef->getType());
1376   }
1377 }
1378 
1379 void CodeGenFunction::EmitOMPReductionClauseFinal(
1380     const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) {
1381   if (!HaveInsertPoint())
1382     return;
1383   llvm::SmallVector<const Expr *, 8> Privates;
1384   llvm::SmallVector<const Expr *, 8> LHSExprs;
1385   llvm::SmallVector<const Expr *, 8> RHSExprs;
1386   llvm::SmallVector<const Expr *, 8> ReductionOps;
1387   bool HasAtLeastOneReduction = false;
1388   bool IsReductionWithTaskMod = false;
1389   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1390     HasAtLeastOneReduction = true;
1391     Privates.append(C->privates().begin(), C->privates().end());
1392     LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1393     RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1394     ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1395     IsReductionWithTaskMod =
1396         IsReductionWithTaskMod || C->getModifier() == OMPC_REDUCTION_task;
1397   }
1398   if (HasAtLeastOneReduction) {
1399     if (IsReductionWithTaskMod) {
1400       CGM.getOpenMPRuntime().emitTaskReductionFini(
1401           *this, D.getBeginLoc(),
1402           isOpenMPWorksharingDirective(D.getDirectiveKind()));
1403     }
1404     bool WithNowait = D.getSingleClause<OMPNowaitClause>() ||
1405                       isOpenMPParallelDirective(D.getDirectiveKind()) ||
1406                       ReductionKind == OMPD_simd;
1407     bool SimpleReduction = ReductionKind == OMPD_simd;
1408     // Emit nowait reduction if nowait clause is present or directive is a
1409     // parallel directive (it always has implicit barrier).
1410     CGM.getOpenMPRuntime().emitReduction(
1411         *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps,
1412         {WithNowait, SimpleReduction, ReductionKind});
1413   }
1414 }
1415 
1416 static void emitPostUpdateForReductionClause(
1417     CodeGenFunction &CGF, const OMPExecutableDirective &D,
1418     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) {
1419   if (!CGF.HaveInsertPoint())
1420     return;
1421   llvm::BasicBlock *DoneBB = nullptr;
1422   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1423     if (const Expr *PostUpdate = C->getPostUpdateExpr()) {
1424       if (!DoneBB) {
1425         if (llvm::Value *Cond = CondGen(CGF)) {
1426           // If the first post-update expression is found, emit conditional
1427           // block if it was requested.
1428           llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1429           DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1430           CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1431           CGF.EmitBlock(ThenBB);
1432         }
1433       }
1434       CGF.EmitIgnoredExpr(PostUpdate);
1435     }
1436   }
1437   if (DoneBB)
1438     CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1439 }
1440 
1441 namespace {
1442 /// Codegen lambda for appending distribute lower and upper bounds to outlined
1443 /// parallel function. This is necessary for combined constructs such as
1444 /// 'distribute parallel for'
1445 typedef llvm::function_ref<void(CodeGenFunction &,
1446                                 const OMPExecutableDirective &,
1447                                 llvm::SmallVectorImpl<llvm::Value *> &)>
1448     CodeGenBoundParametersTy;
1449 } // anonymous namespace
1450 
1451 static void
1452 checkForLastprivateConditionalUpdate(CodeGenFunction &CGF,
1453                                      const OMPExecutableDirective &S) {
1454   if (CGF.getLangOpts().OpenMP < 50)
1455     return;
1456   llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> PrivateDecls;
1457   for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
1458     for (const Expr *Ref : C->varlists()) {
1459       if (!Ref->getType()->isScalarType())
1460         continue;
1461       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
1462       if (!DRE)
1463         continue;
1464       PrivateDecls.insert(cast<VarDecl>(DRE->getDecl()));
1465       CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref);
1466     }
1467   }
1468   for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
1469     for (const Expr *Ref : C->varlists()) {
1470       if (!Ref->getType()->isScalarType())
1471         continue;
1472       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
1473       if (!DRE)
1474         continue;
1475       PrivateDecls.insert(cast<VarDecl>(DRE->getDecl()));
1476       CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref);
1477     }
1478   }
1479   for (const auto *C : S.getClausesOfKind<OMPLinearClause>()) {
1480     for (const Expr *Ref : C->varlists()) {
1481       if (!Ref->getType()->isScalarType())
1482         continue;
1483       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
1484       if (!DRE)
1485         continue;
1486       PrivateDecls.insert(cast<VarDecl>(DRE->getDecl()));
1487       CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref);
1488     }
1489   }
1490   // Privates should ne analyzed since they are not captured at all.
1491   // Task reductions may be skipped - tasks are ignored.
1492   // Firstprivates do not return value but may be passed by reference - no need
1493   // to check for updated lastprivate conditional.
1494   for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
1495     for (const Expr *Ref : C->varlists()) {
1496       if (!Ref->getType()->isScalarType())
1497         continue;
1498       const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
1499       if (!DRE)
1500         continue;
1501       PrivateDecls.insert(cast<VarDecl>(DRE->getDecl()));
1502     }
1503   }
1504   CGF.CGM.getOpenMPRuntime().checkAndEmitSharedLastprivateConditional(
1505       CGF, S, PrivateDecls);
1506 }
1507 
1508 static void emitCommonOMPParallelDirective(
1509     CodeGenFunction &CGF, const OMPExecutableDirective &S,
1510     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
1511     const CodeGenBoundParametersTy &CodeGenBoundParameters) {
1512   const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
1513   llvm::Function *OutlinedFn =
1514       CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction(
1515           S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
1516   if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
1517     CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
1518     llvm::Value *NumThreads =
1519         CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1520                            /*IgnoreResultAssign=*/true);
1521     CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1522         CGF, NumThreads, NumThreadsClause->getBeginLoc());
1523   }
1524   if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
1525     CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
1526     CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1527         CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc());
1528   }
1529   const Expr *IfCond = nullptr;
1530   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1531     if (C->getNameModifier() == OMPD_unknown ||
1532         C->getNameModifier() == OMPD_parallel) {
1533       IfCond = C->getCondition();
1534       break;
1535     }
1536   }
1537 
1538   OMPParallelScope Scope(CGF, S);
1539   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
1540   // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk
1541   // lower and upper bounds with the pragma 'for' chunking mechanism.
1542   // The following lambda takes care of appending the lower and upper bound
1543   // parameters when necessary
1544   CodeGenBoundParameters(CGF, S, CapturedVars);
1545   CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
1546   CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn,
1547                                               CapturedVars, IfCond);
1548 }
1549 
1550 static void emitEmptyBoundParameters(CodeGenFunction &,
1551                                      const OMPExecutableDirective &,
1552                                      llvm::SmallVectorImpl<llvm::Value *> &) {}
1553 
1554 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
1555   if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) {
1556     // Check if we have any if clause associated with the directive.
1557     llvm::Value *IfCond = nullptr;
1558     if (const auto *C = S.getSingleClause<OMPIfClause>())
1559       IfCond = EmitScalarExpr(C->getCondition(),
1560                               /*IgnoreResultAssign=*/true);
1561 
1562     llvm::Value *NumThreads = nullptr;
1563     if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>())
1564       NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads(),
1565                                   /*IgnoreResultAssign=*/true);
1566 
1567     ProcBindKind ProcBind = OMP_PROC_BIND_default;
1568     if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>())
1569       ProcBind = ProcBindClause->getProcBindKind();
1570 
1571     using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
1572 
1573     // The cleanup callback that finalizes all variabels at the given location,
1574     // thus calls destructors etc.
1575     auto FiniCB = [this](InsertPointTy IP) {
1576       OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
1577     };
1578 
1579     // Privatization callback that performs appropriate action for
1580     // shared/private/firstprivate/lastprivate/copyin/... variables.
1581     //
1582     // TODO: This defaults to shared right now.
1583     auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
1584                      llvm::Value &Val, llvm::Value *&ReplVal) {
1585       // The next line is appropriate only for variables (Val) with the
1586       // data-sharing attribute "shared".
1587       ReplVal = &Val;
1588 
1589       return CodeGenIP;
1590     };
1591 
1592     const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
1593     const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt();
1594 
1595     auto BodyGenCB = [ParallelRegionBodyStmt,
1596                       this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
1597                             llvm::BasicBlock &ContinuationBB) {
1598       OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP,
1599                                                       ContinuationBB);
1600       OMPBuilderCBHelpers::EmitOMPRegionBody(*this, ParallelRegionBodyStmt,
1601                                              CodeGenIP, ContinuationBB);
1602     };
1603 
1604     CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);
1605     CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI);
1606     Builder.restoreIP(OMPBuilder->CreateParallel(Builder, BodyGenCB, PrivCB,
1607                                                  FiniCB, IfCond, NumThreads,
1608                                                  ProcBind, S.hasCancel()));
1609     return;
1610   }
1611 
1612   // Emit parallel region as a standalone region.
1613   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
1614     Action.Enter(CGF);
1615     OMPPrivateScope PrivateScope(CGF);
1616     bool Copyins = CGF.EmitOMPCopyinClause(S);
1617     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1618     if (Copyins) {
1619       // Emit implicit barrier to synchronize threads and avoid data races on
1620       // propagation master's thread values of threadprivate variables to local
1621       // instances of that variables of all other implicit threads.
1622       CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1623           CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
1624           /*ForceSimpleCall=*/true);
1625     }
1626     CGF.EmitOMPPrivateClause(S, PrivateScope);
1627     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1628     (void)PrivateScope.Privatize();
1629     CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt());
1630     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
1631   };
1632   {
1633     auto LPCRegion =
1634         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
1635     emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen,
1636                                    emitEmptyBoundParameters);
1637     emitPostUpdateForReductionClause(*this, S,
1638                                      [](CodeGenFunction &) { return nullptr; });
1639   }
1640   // Check for outer lastprivate conditional update.
1641   checkForLastprivateConditionalUpdate(*this, S);
1642 }
1643 
1644 static void emitBody(CodeGenFunction &CGF, const Stmt *S, const Stmt *NextLoop,
1645                      int MaxLevel, int Level = 0) {
1646   assert(Level < MaxLevel && "Too deep lookup during loop body codegen.");
1647   const Stmt *SimplifiedS = S->IgnoreContainers();
1648   if (const auto *CS = dyn_cast<CompoundStmt>(SimplifiedS)) {
1649     PrettyStackTraceLoc CrashInfo(
1650         CGF.getContext().getSourceManager(), CS->getLBracLoc(),
1651         "LLVM IR generation of compound statement ('{}')");
1652 
1653     // Keep track of the current cleanup stack depth, including debug scopes.
1654     CodeGenFunction::LexicalScope Scope(CGF, S->getSourceRange());
1655     for (const Stmt *CurStmt : CS->body())
1656       emitBody(CGF, CurStmt, NextLoop, MaxLevel, Level);
1657     return;
1658   }
1659   if (SimplifiedS == NextLoop) {
1660     if (const auto *For = dyn_cast<ForStmt>(SimplifiedS)) {
1661       S = For->getBody();
1662     } else {
1663       assert(isa<CXXForRangeStmt>(SimplifiedS) &&
1664              "Expected canonical for loop or range-based for loop.");
1665       const auto *CXXFor = cast<CXXForRangeStmt>(SimplifiedS);
1666       CGF.EmitStmt(CXXFor->getLoopVarStmt());
1667       S = CXXFor->getBody();
1668     }
1669     if (Level + 1 < MaxLevel) {
1670       NextLoop = OMPLoopDirective::tryToFindNextInnerLoop(
1671           S, /*TryImperfectlyNestedLoops=*/true);
1672       emitBody(CGF, S, NextLoop, MaxLevel, Level + 1);
1673       return;
1674     }
1675   }
1676   CGF.EmitStmt(S);
1677 }
1678 
1679 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1680                                       JumpDest LoopExit) {
1681   RunCleanupsScope BodyScope(*this);
1682   // Update counters values on current iteration.
1683   for (const Expr *UE : D.updates())
1684     EmitIgnoredExpr(UE);
1685   // Update the linear variables.
1686   // In distribute directives only loop counters may be marked as linear, no
1687   // need to generate the code for them.
1688   if (!isOpenMPDistributeDirective(D.getDirectiveKind())) {
1689     for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1690       for (const Expr *UE : C->updates())
1691         EmitIgnoredExpr(UE);
1692     }
1693   }
1694 
1695   // On a continue in the body, jump to the end.
1696   JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue");
1697   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1698   for (const Expr *E : D.finals_conditions()) {
1699     if (!E)
1700       continue;
1701     // Check that loop counter in non-rectangular nest fits into the iteration
1702     // space.
1703     llvm::BasicBlock *NextBB = createBasicBlock("omp.body.next");
1704     EmitBranchOnBoolExpr(E, NextBB, Continue.getBlock(),
1705                          getProfileCount(D.getBody()));
1706     EmitBlock(NextBB);
1707   }
1708   // Emit loop variables for C++ range loops.
1709   const Stmt *Body =
1710       D.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers();
1711   // Emit loop body.
1712   emitBody(*this, Body,
1713            OMPLoopDirective::tryToFindNextInnerLoop(
1714                Body, /*TryImperfectlyNestedLoops=*/true),
1715            D.getCollapsedNumber());
1716 
1717   // The end (updates/cleanups).
1718   EmitBlock(Continue.getBlock());
1719   BreakContinueStack.pop_back();
1720 }
1721 
1722 void CodeGenFunction::EmitOMPInnerLoop(
1723     const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1724     const Expr *IncExpr,
1725     const llvm::function_ref<void(CodeGenFunction &)> BodyGen,
1726     const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) {
1727   auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
1728 
1729   // Start the loop with a block that tests the condition.
1730   auto CondBlock = createBasicBlock("omp.inner.for.cond");
1731   EmitBlock(CondBlock);
1732   const SourceRange R = S.getSourceRange();
1733   LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
1734                  SourceLocToDebugLoc(R.getEnd()));
1735 
1736   // If there are any cleanups between here and the loop-exit scope,
1737   // create a block to stage a loop exit along.
1738   llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
1739   if (RequiresCleanup)
1740     ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
1741 
1742   llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body");
1743 
1744   // Emit condition.
1745   EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
1746   if (ExitBlock != LoopExit.getBlock()) {
1747     EmitBlock(ExitBlock);
1748     EmitBranchThroughCleanup(LoopExit);
1749   }
1750 
1751   EmitBlock(LoopBody);
1752   incrementProfileCounter(&S);
1753 
1754   // Create a block for the increment.
1755   JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
1756   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1757 
1758   BodyGen(*this);
1759 
1760   // Emit "IV = IV + 1" and a back-edge to the condition block.
1761   EmitBlock(Continue.getBlock());
1762   EmitIgnoredExpr(IncExpr);
1763   PostIncGen(*this);
1764   BreakContinueStack.pop_back();
1765   EmitBranch(CondBlock);
1766   LoopStack.pop();
1767   // Emit the fall-through block.
1768   EmitBlock(LoopExit.getBlock());
1769 }
1770 
1771 bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
1772   if (!HaveInsertPoint())
1773     return false;
1774   // Emit inits for the linear variables.
1775   bool HasLinears = false;
1776   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1777     for (const Expr *Init : C->inits()) {
1778       HasLinears = true;
1779       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
1780       if (const auto *Ref =
1781               dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1782         AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1783         const auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1784         DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
1785                         CapturedStmtInfo->lookup(OrigVD) != nullptr,
1786                         VD->getInit()->getType(), VK_LValue,
1787                         VD->getInit()->getExprLoc());
1788         EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1789                                                 VD->getType()),
1790                        /*capturedByInit=*/false);
1791         EmitAutoVarCleanups(Emission);
1792       } else {
1793         EmitVarDecl(*VD);
1794       }
1795     }
1796     // Emit the linear steps for the linear clauses.
1797     // If a step is not constant, it is pre-calculated before the loop.
1798     if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1799       if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
1800         EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
1801         // Emit calculation of the linear step.
1802         EmitIgnoredExpr(CS);
1803       }
1804   }
1805   return HasLinears;
1806 }
1807 
1808 void CodeGenFunction::EmitOMPLinearClauseFinal(
1809     const OMPLoopDirective &D,
1810     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) {
1811   if (!HaveInsertPoint())
1812     return;
1813   llvm::BasicBlock *DoneBB = nullptr;
1814   // Emit the final values of the linear variables.
1815   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1816     auto IC = C->varlist_begin();
1817     for (const Expr *F : C->finals()) {
1818       if (!DoneBB) {
1819         if (llvm::Value *Cond = CondGen(*this)) {
1820           // If the first post-update expression is found, emit conditional
1821           // block if it was requested.
1822           llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu");
1823           DoneBB = createBasicBlock(".omp.linear.pu.done");
1824           Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1825           EmitBlock(ThenBB);
1826         }
1827       }
1828       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1829       DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD),
1830                       CapturedStmtInfo->lookup(OrigVD) != nullptr,
1831                       (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
1832       Address OrigAddr = EmitLValue(&DRE).getAddress(*this);
1833       CodeGenFunction::OMPPrivateScope VarScope(*this);
1834       VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; });
1835       (void)VarScope.Privatize();
1836       EmitIgnoredExpr(F);
1837       ++IC;
1838     }
1839     if (const Expr *PostUpdate = C->getPostUpdateExpr())
1840       EmitIgnoredExpr(PostUpdate);
1841   }
1842   if (DoneBB)
1843     EmitBlock(DoneBB, /*IsFinished=*/true);
1844 }
1845 
1846 static void emitAlignedClause(CodeGenFunction &CGF,
1847                               const OMPExecutableDirective &D) {
1848   if (!CGF.HaveInsertPoint())
1849     return;
1850   for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
1851     llvm::APInt ClauseAlignment(64, 0);
1852     if (const Expr *AlignmentExpr = Clause->getAlignment()) {
1853       auto *AlignmentCI =
1854           cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1855       ClauseAlignment = AlignmentCI->getValue();
1856     }
1857     for (const Expr *E : Clause->varlists()) {
1858       llvm::APInt Alignment(ClauseAlignment);
1859       if (Alignment == 0) {
1860         // OpenMP [2.8.1, Description]
1861         // If no optional parameter is specified, implementation-defined default
1862         // alignments for SIMD instructions on the target platforms are assumed.
1863         Alignment =
1864             CGF.getContext()
1865                 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1866                     E->getType()->getPointeeType()))
1867                 .getQuantity();
1868       }
1869       assert((Alignment == 0 || Alignment.isPowerOf2()) &&
1870              "alignment is not power of 2");
1871       if (Alignment != 0) {
1872         llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1873         CGF.emitAlignmentAssumption(
1874             PtrValue, E, /*No second loc needed*/ SourceLocation(),
1875             llvm::ConstantInt::get(CGF.getLLVMContext(), Alignment));
1876       }
1877     }
1878   }
1879 }
1880 
1881 void CodeGenFunction::EmitOMPPrivateLoopCounters(
1882     const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1883   if (!HaveInsertPoint())
1884     return;
1885   auto I = S.private_counters().begin();
1886   for (const Expr *E : S.counters()) {
1887     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1888     const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
1889     // Emit var without initialization.
1890     AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD);
1891     EmitAutoVarCleanups(VarEmission);
1892     LocalDeclMap.erase(PrivateVD);
1893     (void)LoopScope.addPrivate(VD, [&VarEmission]() {
1894       return VarEmission.getAllocatedAddress();
1895     });
1896     if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1897         VD->hasGlobalStorage()) {
1898       (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() {
1899         DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD),
1900                         LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1901                         E->getType(), VK_LValue, E->getExprLoc());
1902         return EmitLValue(&DRE).getAddress(*this);
1903       });
1904     } else {
1905       (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() {
1906         return VarEmission.getAllocatedAddress();
1907       });
1908     }
1909     ++I;
1910   }
1911   // Privatize extra loop counters used in loops for ordered(n) clauses.
1912   for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) {
1913     if (!C->getNumForLoops())
1914       continue;
1915     for (unsigned I = S.getCollapsedNumber(),
1916                   E = C->getLoopNumIterations().size();
1917          I < E; ++I) {
1918       const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I));
1919       const auto *VD = cast<VarDecl>(DRE->getDecl());
1920       // Override only those variables that can be captured to avoid re-emission
1921       // of the variables declared within the loops.
1922       if (DRE->refersToEnclosingVariableOrCapture()) {
1923         (void)LoopScope.addPrivate(VD, [this, DRE, VD]() {
1924           return CreateMemTemp(DRE->getType(), VD->getName());
1925         });
1926       }
1927     }
1928   }
1929 }
1930 
1931 static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1932                         const Expr *Cond, llvm::BasicBlock *TrueBlock,
1933                         llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
1934   if (!CGF.HaveInsertPoint())
1935     return;
1936   {
1937     CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
1938     CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
1939     (void)PreCondScope.Privatize();
1940     // Get initial values of real counters.
1941     for (const Expr *I : S.inits()) {
1942       CGF.EmitIgnoredExpr(I);
1943     }
1944   }
1945   // Create temp loop control variables with their init values to support
1946   // non-rectangular loops.
1947   CodeGenFunction::OMPMapVars PreCondVars;
1948   for (const Expr * E: S.dependent_counters()) {
1949     if (!E)
1950       continue;
1951     assert(!E->getType().getNonReferenceType()->isRecordType() &&
1952            "dependent counter must not be an iterator.");
1953     const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1954     Address CounterAddr =
1955         CGF.CreateMemTemp(VD->getType().getNonReferenceType());
1956     (void)PreCondVars.setVarAddr(CGF, VD, CounterAddr);
1957   }
1958   (void)PreCondVars.apply(CGF);
1959   for (const Expr *E : S.dependent_inits()) {
1960     if (!E)
1961       continue;
1962     CGF.EmitIgnoredExpr(E);
1963   }
1964   // Check that loop is executed at least one time.
1965   CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1966   PreCondVars.restore(CGF);
1967 }
1968 
1969 void CodeGenFunction::EmitOMPLinearClause(
1970     const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1971   if (!HaveInsertPoint())
1972     return;
1973   llvm::DenseSet<const VarDecl *> SIMDLCVs;
1974   if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1975     const auto *LoopDirective = cast<OMPLoopDirective>(&D);
1976     for (const Expr *C : LoopDirective->counters()) {
1977       SIMDLCVs.insert(
1978           cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1979     }
1980   }
1981   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1982     auto CurPrivate = C->privates().begin();
1983     for (const Expr *E : C->varlists()) {
1984       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1985       const auto *PrivateVD =
1986           cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
1987       if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1988         bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() {
1989           // Emit private VarDecl with copy init.
1990           EmitVarDecl(*PrivateVD);
1991           return GetAddrOfLocalVar(PrivateVD);
1992         });
1993         assert(IsRegistered && "linear var already registered as private");
1994         // Silence the warning about unused variable.
1995         (void)IsRegistered;
1996       } else {
1997         EmitVarDecl(*PrivateVD);
1998       }
1999       ++CurPrivate;
2000     }
2001   }
2002 }
2003 
2004 static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
2005                                      const OMPExecutableDirective &D,
2006                                      bool IsMonotonic) {
2007   if (!CGF.HaveInsertPoint())
2008     return;
2009   if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
2010     RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
2011                                  /*ignoreResult=*/true);
2012     auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
2013     CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
2014     // In presence of finite 'safelen', it may be unsafe to mark all
2015     // the memory instructions parallel, because loop-carried
2016     // dependences of 'safelen' iterations are possible.
2017     if (!IsMonotonic)
2018       CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
2019   } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
2020     RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
2021                                  /*ignoreResult=*/true);
2022     auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
2023     CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
2024     // In presence of finite 'safelen', it may be unsafe to mark all
2025     // the memory instructions parallel, because loop-carried
2026     // dependences of 'safelen' iterations are possible.
2027     CGF.LoopStack.setParallel(/*Enable=*/false);
2028   }
2029 }
2030 
2031 void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
2032                                       bool IsMonotonic) {
2033   // Walk clauses and process safelen/lastprivate.
2034   LoopStack.setParallel(!IsMonotonic);
2035   LoopStack.setVectorizeEnable();
2036   emitSimdlenSafelenClause(*this, D, IsMonotonic);
2037   if (const auto *C = D.getSingleClause<OMPOrderClause>())
2038     if (C->getKind() == OMPC_ORDER_concurrent)
2039       LoopStack.setParallel(/*Enable=*/true);
2040 }
2041 
2042 void CodeGenFunction::EmitOMPSimdFinal(
2043     const OMPLoopDirective &D,
2044     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) {
2045   if (!HaveInsertPoint())
2046     return;
2047   llvm::BasicBlock *DoneBB = nullptr;
2048   auto IC = D.counters().begin();
2049   auto IPC = D.private_counters().begin();
2050   for (const Expr *F : D.finals()) {
2051     const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
2052     const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
2053     const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
2054     if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
2055         OrigVD->hasGlobalStorage() || CED) {
2056       if (!DoneBB) {
2057         if (llvm::Value *Cond = CondGen(*this)) {
2058           // If the first post-update expression is found, emit conditional
2059           // block if it was requested.
2060           llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then");
2061           DoneBB = createBasicBlock(".omp.final.done");
2062           Builder.CreateCondBr(Cond, ThenBB, DoneBB);
2063           EmitBlock(ThenBB);
2064         }
2065       }
2066       Address OrigAddr = Address::invalid();
2067       if (CED) {
2068         OrigAddr =
2069             EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(*this);
2070       } else {
2071         DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(PrivateVD),
2072                         /*RefersToEnclosingVariableOrCapture=*/false,
2073                         (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
2074         OrigAddr = EmitLValue(&DRE).getAddress(*this);
2075       }
2076       OMPPrivateScope VarScope(*this);
2077       VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; });
2078       (void)VarScope.Privatize();
2079       EmitIgnoredExpr(F);
2080     }
2081     ++IC;
2082     ++IPC;
2083   }
2084   if (DoneBB)
2085     EmitBlock(DoneBB, /*IsFinished=*/true);
2086 }
2087 
2088 static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF,
2089                                          const OMPLoopDirective &S,
2090                                          CodeGenFunction::JumpDest LoopExit) {
2091   CGF.EmitOMPLoopBody(S, LoopExit);
2092   CGF.EmitStopPoint(&S);
2093 }
2094 
2095 /// Emit a helper variable and return corresponding lvalue.
2096 static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
2097                                const DeclRefExpr *Helper) {
2098   auto VDecl = cast<VarDecl>(Helper->getDecl());
2099   CGF.EmitVarDecl(*VDecl);
2100   return CGF.EmitLValue(Helper);
2101 }
2102 
2103 static void emitCommonSimdLoop(CodeGenFunction &CGF, const OMPLoopDirective &S,
2104                                const RegionCodeGenTy &SimdInitGen,
2105                                const RegionCodeGenTy &BodyCodeGen) {
2106   auto &&ThenGen = [&S, &SimdInitGen, &BodyCodeGen](CodeGenFunction &CGF,
2107                                                     PrePostActionTy &) {
2108     CGOpenMPRuntime::NontemporalDeclsRAII NontemporalsRegion(CGF.CGM, S);
2109     CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF);
2110     SimdInitGen(CGF);
2111 
2112     BodyCodeGen(CGF);
2113   };
2114   auto &&ElseGen = [&BodyCodeGen](CodeGenFunction &CGF, PrePostActionTy &) {
2115     CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF);
2116     CGF.LoopStack.setVectorizeEnable(/*Enable=*/false);
2117 
2118     BodyCodeGen(CGF);
2119   };
2120   const Expr *IfCond = nullptr;
2121   if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2122     for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2123       if (CGF.getLangOpts().OpenMP >= 50 &&
2124           (C->getNameModifier() == OMPD_unknown ||
2125            C->getNameModifier() == OMPD_simd)) {
2126         IfCond = C->getCondition();
2127         break;
2128       }
2129     }
2130   }
2131   if (IfCond) {
2132     CGF.CGM.getOpenMPRuntime().emitIfClause(CGF, IfCond, ThenGen, ElseGen);
2133   } else {
2134     RegionCodeGenTy ThenRCG(ThenGen);
2135     ThenRCG(CGF);
2136   }
2137 }
2138 
2139 static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S,
2140                               PrePostActionTy &Action) {
2141   Action.Enter(CGF);
2142   assert(isOpenMPSimdDirective(S.getDirectiveKind()) &&
2143          "Expected simd directive");
2144   OMPLoopScope PreInitScope(CGF, S);
2145   // if (PreCond) {
2146   //   for (IV in 0..LastIteration) BODY;
2147   //   <Final counter/linear vars updates>;
2148   // }
2149   //
2150   if (isOpenMPDistributeDirective(S.getDirectiveKind()) ||
2151       isOpenMPWorksharingDirective(S.getDirectiveKind()) ||
2152       isOpenMPTaskLoopDirective(S.getDirectiveKind())) {
2153     (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()));
2154     (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()));
2155   }
2156 
2157   // Emit: if (PreCond) - begin.
2158   // If the condition constant folds and can be elided, avoid emitting the
2159   // whole loop.
2160   bool CondConstant;
2161   llvm::BasicBlock *ContBlock = nullptr;
2162   if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2163     if (!CondConstant)
2164       return;
2165   } else {
2166     llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then");
2167     ContBlock = CGF.createBasicBlock("simd.if.end");
2168     emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
2169                 CGF.getProfileCount(&S));
2170     CGF.EmitBlock(ThenBlock);
2171     CGF.incrementProfileCounter(&S);
2172   }
2173 
2174   // Emit the loop iteration variable.
2175   const Expr *IVExpr = S.getIterationVariable();
2176   const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
2177   CGF.EmitVarDecl(*IVDecl);
2178   CGF.EmitIgnoredExpr(S.getInit());
2179 
2180   // Emit the iterations count variable.
2181   // If it is not a variable, Sema decided to calculate iterations count on
2182   // each iteration (e.g., it is foldable into a constant).
2183   if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2184     CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2185     // Emit calculation of the iterations count.
2186     CGF.EmitIgnoredExpr(S.getCalcLastIteration());
2187   }
2188 
2189   emitAlignedClause(CGF, S);
2190   (void)CGF.EmitOMPLinearClauseInit(S);
2191   {
2192     CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2193     CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
2194     CGF.EmitOMPLinearClause(S, LoopScope);
2195     CGF.EmitOMPPrivateClause(S, LoopScope);
2196     CGF.EmitOMPReductionClauseInit(S, LoopScope);
2197     CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion(
2198         CGF, S, CGF.EmitLValue(S.getIterationVariable()));
2199     bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2200     (void)LoopScope.Privatize();
2201     if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
2202       CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S);
2203 
2204     emitCommonSimdLoop(
2205         CGF, S,
2206         [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2207           CGF.EmitOMPSimdInit(S);
2208         },
2209         [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) {
2210           CGF.EmitOMPInnerLoop(
2211               S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
2212               [&S](CodeGenFunction &CGF) {
2213                 CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest());
2214                 CGF.EmitStopPoint(&S);
2215               },
2216               [](CodeGenFunction &) {});
2217         });
2218     CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; });
2219     // Emit final copy of the lastprivate variables at the end of loops.
2220     if (HasLastprivateClause)
2221       CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
2222     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd);
2223     emitPostUpdateForReductionClause(CGF, S,
2224                                      [](CodeGenFunction &) { return nullptr; });
2225   }
2226   CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; });
2227   // Emit: if (PreCond) - end.
2228   if (ContBlock) {
2229     CGF.EmitBranch(ContBlock);
2230     CGF.EmitBlock(ContBlock, true);
2231   }
2232 }
2233 
2234 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
2235   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2236     emitOMPSimdRegion(CGF, S, Action);
2237   };
2238   {
2239     auto LPCRegion =
2240         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
2241     OMPLexicalScope Scope(*this, S, OMPD_unknown);
2242     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2243   }
2244   // Check for outer lastprivate conditional update.
2245   checkForLastprivateConditionalUpdate(*this, S);
2246 }
2247 
2248 void CodeGenFunction::EmitOMPOuterLoop(
2249     bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S,
2250     CodeGenFunction::OMPPrivateScope &LoopScope,
2251     const CodeGenFunction::OMPLoopArguments &LoopArgs,
2252     const CodeGenFunction::CodeGenLoopTy &CodeGenLoop,
2253     const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) {
2254   CGOpenMPRuntime &RT = CGM.getOpenMPRuntime();
2255 
2256   const Expr *IVExpr = S.getIterationVariable();
2257   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2258   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2259 
2260   JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
2261 
2262   // Start the loop with a block that tests the condition.
2263   llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond");
2264   EmitBlock(CondBlock);
2265   const SourceRange R = S.getSourceRange();
2266   LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()),
2267                  SourceLocToDebugLoc(R.getEnd()));
2268 
2269   llvm::Value *BoolCondVal = nullptr;
2270   if (!DynamicOrOrdered) {
2271     // UB = min(UB, GlobalUB) or
2272     // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g.
2273     // 'distribute parallel for')
2274     EmitIgnoredExpr(LoopArgs.EUB);
2275     // IV = LB
2276     EmitIgnoredExpr(LoopArgs.Init);
2277     // IV < UB
2278     BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond);
2279   } else {
2280     BoolCondVal =
2281         RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL,
2282                        LoopArgs.LB, LoopArgs.UB, LoopArgs.ST);
2283   }
2284 
2285   // If there are any cleanups between here and the loop-exit scope,
2286   // create a block to stage a loop exit along.
2287   llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
2288   if (LoopScope.requiresCleanups())
2289     ExitBlock = createBasicBlock("omp.dispatch.cleanup");
2290 
2291   llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body");
2292   Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
2293   if (ExitBlock != LoopExit.getBlock()) {
2294     EmitBlock(ExitBlock);
2295     EmitBranchThroughCleanup(LoopExit);
2296   }
2297   EmitBlock(LoopBody);
2298 
2299   // Emit "IV = LB" (in case of static schedule, we have already calculated new
2300   // LB for loop condition and emitted it above).
2301   if (DynamicOrOrdered)
2302     EmitIgnoredExpr(LoopArgs.Init);
2303 
2304   // Create a block for the increment.
2305   JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
2306   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
2307 
2308   emitCommonSimdLoop(
2309       *this, S,
2310       [&S, IsMonotonic](CodeGenFunction &CGF, PrePostActionTy &) {
2311         // Generate !llvm.loop.parallel metadata for loads and stores for loops
2312         // with dynamic/guided scheduling and without ordered clause.
2313         if (!isOpenMPSimdDirective(S.getDirectiveKind())) {
2314           CGF.LoopStack.setParallel(!IsMonotonic);
2315           if (const auto *C = S.getSingleClause<OMPOrderClause>())
2316             if (C->getKind() == OMPC_ORDER_concurrent)
2317               CGF.LoopStack.setParallel(/*Enable=*/true);
2318         } else {
2319           CGF.EmitOMPSimdInit(S, IsMonotonic);
2320         }
2321       },
2322       [&S, &LoopArgs, LoopExit, &CodeGenLoop, IVSize, IVSigned, &CodeGenOrdered,
2323        &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) {
2324         SourceLocation Loc = S.getBeginLoc();
2325         // when 'distribute' is not combined with a 'for':
2326         // while (idx <= UB) { BODY; ++idx; }
2327         // when 'distribute' is combined with a 'for'
2328         // (e.g. 'distribute parallel for')
2329         // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; }
2330         CGF.EmitOMPInnerLoop(
2331             S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr,
2332             [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
2333               CodeGenLoop(CGF, S, LoopExit);
2334             },
2335             [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) {
2336               CodeGenOrdered(CGF, Loc, IVSize, IVSigned);
2337             });
2338       });
2339 
2340   EmitBlock(Continue.getBlock());
2341   BreakContinueStack.pop_back();
2342   if (!DynamicOrOrdered) {
2343     // Emit "LB = LB + Stride", "UB = UB + Stride".
2344     EmitIgnoredExpr(LoopArgs.NextLB);
2345     EmitIgnoredExpr(LoopArgs.NextUB);
2346   }
2347 
2348   EmitBranch(CondBlock);
2349   LoopStack.pop();
2350   // Emit the fall-through block.
2351   EmitBlock(LoopExit.getBlock());
2352 
2353   // Tell the runtime we are done.
2354   auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) {
2355     if (!DynamicOrOrdered)
2356       CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(),
2357                                                      S.getDirectiveKind());
2358   };
2359   OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
2360 }
2361 
2362 void CodeGenFunction::EmitOMPForOuterLoop(
2363     const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic,
2364     const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
2365     const OMPLoopArguments &LoopArgs,
2366     const CodeGenDispatchBoundsTy &CGDispatchBounds) {
2367   CGOpenMPRuntime &RT = CGM.getOpenMPRuntime();
2368 
2369   // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
2370   const bool DynamicOrOrdered =
2371       Ordered || RT.isDynamic(ScheduleKind.Schedule);
2372 
2373   assert((Ordered ||
2374           !RT.isStaticNonchunked(ScheduleKind.Schedule,
2375                                  LoopArgs.Chunk != nullptr)) &&
2376          "static non-chunked schedule does not need outer loop");
2377 
2378   // Emit outer loop.
2379   //
2380   // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2381   // When schedule(dynamic,chunk_size) is specified, the iterations are
2382   // distributed to threads in the team in chunks as the threads request them.
2383   // Each thread executes a chunk of iterations, then requests another chunk,
2384   // until no chunks remain to be distributed. Each chunk contains chunk_size
2385   // iterations, except for the last chunk to be distributed, which may have
2386   // fewer iterations. When no chunk_size is specified, it defaults to 1.
2387   //
2388   // When schedule(guided,chunk_size) is specified, the iterations are assigned
2389   // to threads in the team in chunks as the executing threads request them.
2390   // Each thread executes a chunk of iterations, then requests another chunk,
2391   // until no chunks remain to be assigned. For a chunk_size of 1, the size of
2392   // each chunk is proportional to the number of unassigned iterations divided
2393   // by the number of threads in the team, decreasing to 1. For a chunk_size
2394   // with value k (greater than 1), the size of each chunk is determined in the
2395   // same way, with the restriction that the chunks do not contain fewer than k
2396   // iterations (except for the last chunk to be assigned, which may have fewer
2397   // than k iterations).
2398   //
2399   // When schedule(auto) is specified, the decision regarding scheduling is
2400   // delegated to the compiler and/or runtime system. The programmer gives the
2401   // implementation the freedom to choose any possible mapping of iterations to
2402   // threads in the team.
2403   //
2404   // When schedule(runtime) is specified, the decision regarding scheduling is
2405   // deferred until run time, and the schedule and chunk size are taken from the
2406   // run-sched-var ICV. If the ICV is set to auto, the schedule is
2407   // implementation defined
2408   //
2409   // while(__kmpc_dispatch_next(&LB, &UB)) {
2410   //   idx = LB;
2411   //   while (idx <= UB) { BODY; ++idx;
2412   //   __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
2413   //   } // inner loop
2414   // }
2415   //
2416   // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2417   // When schedule(static, chunk_size) is specified, iterations are divided into
2418   // chunks of size chunk_size, and the chunks are assigned to the threads in
2419   // the team in a round-robin fashion in the order of the thread number.
2420   //
2421   // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
2422   //   while (idx <= UB) { BODY; ++idx; } // inner loop
2423   //   LB = LB + ST;
2424   //   UB = UB + ST;
2425   // }
2426   //
2427 
2428   const Expr *IVExpr = S.getIterationVariable();
2429   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2430   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2431 
2432   if (DynamicOrOrdered) {
2433     const std::pair<llvm::Value *, llvm::Value *> DispatchBounds =
2434         CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB);
2435     llvm::Value *LBVal = DispatchBounds.first;
2436     llvm::Value *UBVal = DispatchBounds.second;
2437     CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal,
2438                                                              LoopArgs.Chunk};
2439     RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize,
2440                            IVSigned, Ordered, DipatchRTInputValues);
2441   } else {
2442     CGOpenMPRuntime::StaticRTInput StaticInit(
2443         IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB,
2444         LoopArgs.ST, LoopArgs.Chunk);
2445     RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(),
2446                          ScheduleKind, StaticInit);
2447   }
2448 
2449   auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc,
2450                                     const unsigned IVSize,
2451                                     const bool IVSigned) {
2452     if (Ordered) {
2453       CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize,
2454                                                             IVSigned);
2455     }
2456   };
2457 
2458   OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST,
2459                                  LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB);
2460   OuterLoopArgs.IncExpr = S.getInc();
2461   OuterLoopArgs.Init = S.getInit();
2462   OuterLoopArgs.Cond = S.getCond();
2463   OuterLoopArgs.NextLB = S.getNextLowerBound();
2464   OuterLoopArgs.NextUB = S.getNextUpperBound();
2465   EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs,
2466                    emitOMPLoopBodyWithStopPoint, CodeGenOrdered);
2467 }
2468 
2469 static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc,
2470                              const unsigned IVSize, const bool IVSigned) {}
2471 
2472 void CodeGenFunction::EmitOMPDistributeOuterLoop(
2473     OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S,
2474     OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs,
2475     const CodeGenLoopTy &CodeGenLoopContent) {
2476 
2477   CGOpenMPRuntime &RT = CGM.getOpenMPRuntime();
2478 
2479   // Emit outer loop.
2480   // Same behavior as a OMPForOuterLoop, except that schedule cannot be
2481   // dynamic
2482   //
2483 
2484   const Expr *IVExpr = S.getIterationVariable();
2485   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2486   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2487 
2488   CGOpenMPRuntime::StaticRTInput StaticInit(
2489       IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB,
2490       LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk);
2491   RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit);
2492 
2493   // for combined 'distribute' and 'for' the increment expression of distribute
2494   // is stored in DistInc. For 'distribute' alone, it is in Inc.
2495   Expr *IncExpr;
2496   if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()))
2497     IncExpr = S.getDistInc();
2498   else
2499     IncExpr = S.getInc();
2500 
2501   // this routine is shared by 'omp distribute parallel for' and
2502   // 'omp distribute': select the right EUB expression depending on the
2503   // directive
2504   OMPLoopArguments OuterLoopArgs;
2505   OuterLoopArgs.LB = LoopArgs.LB;
2506   OuterLoopArgs.UB = LoopArgs.UB;
2507   OuterLoopArgs.ST = LoopArgs.ST;
2508   OuterLoopArgs.IL = LoopArgs.IL;
2509   OuterLoopArgs.Chunk = LoopArgs.Chunk;
2510   OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2511                           ? S.getCombinedEnsureUpperBound()
2512                           : S.getEnsureUpperBound();
2513   OuterLoopArgs.IncExpr = IncExpr;
2514   OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2515                            ? S.getCombinedInit()
2516                            : S.getInit();
2517   OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2518                            ? S.getCombinedCond()
2519                            : S.getCond();
2520   OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2521                              ? S.getCombinedNextLowerBound()
2522                              : S.getNextLowerBound();
2523   OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
2524                              ? S.getCombinedNextUpperBound()
2525                              : S.getNextUpperBound();
2526 
2527   EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S,
2528                    LoopScope, OuterLoopArgs, CodeGenLoopContent,
2529                    emitEmptyOrdered);
2530 }
2531 
2532 static std::pair<LValue, LValue>
2533 emitDistributeParallelForInnerBounds(CodeGenFunction &CGF,
2534                                      const OMPExecutableDirective &S) {
2535   const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2536   LValue LB =
2537       EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2538   LValue UB =
2539       EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2540 
2541   // When composing 'distribute' with 'for' (e.g. as in 'distribute
2542   // parallel for') we need to use the 'distribute'
2543   // chunk lower and upper bounds rather than the whole loop iteration
2544   // space. These are parameters to the outlined function for 'parallel'
2545   // and we copy the bounds of the previous schedule into the
2546   // the current ones.
2547   LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable());
2548   LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable());
2549   llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar(
2550       PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc());
2551   PrevLBVal = CGF.EmitScalarConversion(
2552       PrevLBVal, LS.getPrevLowerBoundVariable()->getType(),
2553       LS.getIterationVariable()->getType(),
2554       LS.getPrevLowerBoundVariable()->getExprLoc());
2555   llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar(
2556       PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc());
2557   PrevUBVal = CGF.EmitScalarConversion(
2558       PrevUBVal, LS.getPrevUpperBoundVariable()->getType(),
2559       LS.getIterationVariable()->getType(),
2560       LS.getPrevUpperBoundVariable()->getExprLoc());
2561 
2562   CGF.EmitStoreOfScalar(PrevLBVal, LB);
2563   CGF.EmitStoreOfScalar(PrevUBVal, UB);
2564 
2565   return {LB, UB};
2566 }
2567 
2568 /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then
2569 /// we need to use the LB and UB expressions generated by the worksharing
2570 /// code generation support, whereas in non combined situations we would
2571 /// just emit 0 and the LastIteration expression
2572 /// This function is necessary due to the difference of the LB and UB
2573 /// types for the RT emission routines for 'for_static_init' and
2574 /// 'for_dispatch_init'
2575 static std::pair<llvm::Value *, llvm::Value *>
2576 emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF,
2577                                         const OMPExecutableDirective &S,
2578                                         Address LB, Address UB) {
2579   const OMPLoopDirective &LS = cast<OMPLoopDirective>(S);
2580   const Expr *IVExpr = LS.getIterationVariable();
2581   // when implementing a dynamic schedule for a 'for' combined with a
2582   // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop
2583   // is not normalized as each team only executes its own assigned
2584   // distribute chunk
2585   QualType IteratorTy = IVExpr->getType();
2586   llvm::Value *LBVal =
2587       CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc());
2588   llvm::Value *UBVal =
2589       CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc());
2590   return {LBVal, UBVal};
2591 }
2592 
2593 static void emitDistributeParallelForDistributeInnerBoundParams(
2594     CodeGenFunction &CGF, const OMPExecutableDirective &S,
2595     llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) {
2596   const auto &Dir = cast<OMPLoopDirective>(S);
2597   LValue LB =
2598       CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable()));
2599   llvm::Value *LBCast =
2600       CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(LB.getAddress(CGF)),
2601                                 CGF.SizeTy, /*isSigned=*/false);
2602   CapturedVars.push_back(LBCast);
2603   LValue UB =
2604       CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable()));
2605 
2606   llvm::Value *UBCast =
2607       CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(UB.getAddress(CGF)),
2608                                 CGF.SizeTy, /*isSigned=*/false);
2609   CapturedVars.push_back(UBCast);
2610 }
2611 
2612 static void
2613 emitInnerParallelForWhenCombined(CodeGenFunction &CGF,
2614                                  const OMPLoopDirective &S,
2615                                  CodeGenFunction::JumpDest LoopExit) {
2616   auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF,
2617                                          PrePostActionTy &Action) {
2618     Action.Enter(CGF);
2619     bool HasCancel = false;
2620     if (!isOpenMPSimdDirective(S.getDirectiveKind())) {
2621       if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S))
2622         HasCancel = D->hasCancel();
2623       else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S))
2624         HasCancel = D->hasCancel();
2625       else if (const auto *D =
2626                    dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S))
2627         HasCancel = D->hasCancel();
2628     }
2629     CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(),
2630                                                      HasCancel);
2631     CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(),
2632                                emitDistributeParallelForInnerBounds,
2633                                emitDistributeParallelForDispatchBounds);
2634   };
2635 
2636   emitCommonOMPParallelDirective(
2637       CGF, S,
2638       isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for,
2639       CGInlinedWorksharingLoop,
2640       emitDistributeParallelForDistributeInnerBoundParams);
2641 }
2642 
2643 void CodeGenFunction::EmitOMPDistributeParallelForDirective(
2644     const OMPDistributeParallelForDirective &S) {
2645   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2646     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
2647                               S.getDistInc());
2648   };
2649   OMPLexicalScope Scope(*this, S, OMPD_parallel);
2650   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen);
2651 }
2652 
2653 void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective(
2654     const OMPDistributeParallelForSimdDirective &S) {
2655   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2656     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
2657                               S.getDistInc());
2658   };
2659   OMPLexicalScope Scope(*this, S, OMPD_parallel);
2660   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen);
2661 }
2662 
2663 void CodeGenFunction::EmitOMPDistributeSimdDirective(
2664     const OMPDistributeSimdDirective &S) {
2665   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2666     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
2667   };
2668   OMPLexicalScope Scope(*this, S, OMPD_unknown);
2669   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2670 }
2671 
2672 void CodeGenFunction::EmitOMPTargetSimdDeviceFunction(
2673     CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) {
2674   // Emit SPMD target parallel for region as a standalone region.
2675   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2676     emitOMPSimdRegion(CGF, S, Action);
2677   };
2678   llvm::Function *Fn;
2679   llvm::Constant *Addr;
2680   // Emit target region as a standalone region.
2681   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
2682       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
2683   assert(Fn && Addr && "Target device function emission failed.");
2684 }
2685 
2686 void CodeGenFunction::EmitOMPTargetSimdDirective(
2687     const OMPTargetSimdDirective &S) {
2688   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2689     emitOMPSimdRegion(CGF, S, Action);
2690   };
2691   emitCommonOMPTargetDirective(*this, S, CodeGen);
2692 }
2693 
2694 namespace {
2695   struct ScheduleKindModifiersTy {
2696     OpenMPScheduleClauseKind Kind;
2697     OpenMPScheduleClauseModifier M1;
2698     OpenMPScheduleClauseModifier M2;
2699     ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
2700                             OpenMPScheduleClauseModifier M1,
2701                             OpenMPScheduleClauseModifier M2)
2702         : Kind(Kind), M1(M1), M2(M2) {}
2703   };
2704 } // namespace
2705 
2706 bool CodeGenFunction::EmitOMPWorksharingLoop(
2707     const OMPLoopDirective &S, Expr *EUB,
2708     const CodeGenLoopBoundsTy &CodeGenLoopBounds,
2709     const CodeGenDispatchBoundsTy &CGDispatchBounds) {
2710   // Emit the loop iteration variable.
2711   const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2712   const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl());
2713   EmitVarDecl(*IVDecl);
2714 
2715   // Emit the iterations count variable.
2716   // If it is not a variable, Sema decided to calculate iterations count on each
2717   // iteration (e.g., it is foldable into a constant).
2718   if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2719     EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2720     // Emit calculation of the iterations count.
2721     EmitIgnoredExpr(S.getCalcLastIteration());
2722   }
2723 
2724   CGOpenMPRuntime &RT = CGM.getOpenMPRuntime();
2725 
2726   bool HasLastprivateClause;
2727   // Check pre-condition.
2728   {
2729     OMPLoopScope PreInitScope(*this, S);
2730     // Skip the entire loop if we don't meet the precondition.
2731     // If the condition constant folds and can be elided, avoid emitting the
2732     // whole loop.
2733     bool CondConstant;
2734     llvm::BasicBlock *ContBlock = nullptr;
2735     if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2736       if (!CondConstant)
2737         return false;
2738     } else {
2739       llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then");
2740       ContBlock = createBasicBlock("omp.precond.end");
2741       emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
2742                   getProfileCount(&S));
2743       EmitBlock(ThenBlock);
2744       incrementProfileCounter(&S);
2745     }
2746 
2747     RunCleanupsScope DoacrossCleanupScope(*this);
2748     bool Ordered = false;
2749     if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) {
2750       if (OrderedClause->getNumForLoops())
2751         RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations());
2752       else
2753         Ordered = true;
2754     }
2755 
2756     llvm::DenseSet<const Expr *> EmittedFinals;
2757     emitAlignedClause(*this, S);
2758     bool HasLinears = EmitOMPLinearClauseInit(S);
2759     // Emit helper vars inits.
2760 
2761     std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S);
2762     LValue LB = Bounds.first;
2763     LValue UB = Bounds.second;
2764     LValue ST =
2765         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2766     LValue IL =
2767         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2768 
2769     // Emit 'then' code.
2770     {
2771       OMPPrivateScope LoopScope(*this);
2772       if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) {
2773         // Emit implicit barrier to synchronize threads and avoid data races on
2774         // initialization of firstprivate variables and post-update of
2775         // lastprivate variables.
2776         CGM.getOpenMPRuntime().emitBarrierCall(
2777             *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
2778             /*ForceSimpleCall=*/true);
2779       }
2780       EmitOMPPrivateClause(S, LoopScope);
2781       CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion(
2782           *this, S, EmitLValue(S.getIterationVariable()));
2783       HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
2784       EmitOMPReductionClauseInit(S, LoopScope);
2785       EmitOMPPrivateLoopCounters(S, LoopScope);
2786       EmitOMPLinearClause(S, LoopScope);
2787       (void)LoopScope.Privatize();
2788       if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
2789         CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S);
2790 
2791       // Detect the loop schedule kind and chunk.
2792       const Expr *ChunkExpr = nullptr;
2793       OpenMPScheduleTy ScheduleKind;
2794       if (const auto *C = S.getSingleClause<OMPScheduleClause>()) {
2795         ScheduleKind.Schedule = C->getScheduleKind();
2796         ScheduleKind.M1 = C->getFirstScheduleModifier();
2797         ScheduleKind.M2 = C->getSecondScheduleModifier();
2798         ChunkExpr = C->getChunkSize();
2799       } else {
2800         // Default behaviour for schedule clause.
2801         CGM.getOpenMPRuntime().getDefaultScheduleAndChunk(
2802             *this, S, ScheduleKind.Schedule, ChunkExpr);
2803       }
2804       bool HasChunkSizeOne = false;
2805       llvm::Value *Chunk = nullptr;
2806       if (ChunkExpr) {
2807         Chunk = EmitScalarExpr(ChunkExpr);
2808         Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(),
2809                                      S.getIterationVariable()->getType(),
2810                                      S.getBeginLoc());
2811         Expr::EvalResult Result;
2812         if (ChunkExpr->EvaluateAsInt(Result, getContext())) {
2813           llvm::APSInt EvaluatedChunk = Result.Val.getInt();
2814           HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1);
2815         }
2816       }
2817       const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2818       const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2819       // OpenMP 4.5, 2.7.1 Loop Construct, Description.
2820       // If the static schedule kind is specified or if the ordered clause is
2821       // specified, and if no monotonic modifier is specified, the effect will
2822       // be as if the monotonic modifier was specified.
2823       bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule,
2824           /* Chunked */ Chunk != nullptr) && HasChunkSizeOne &&
2825           isOpenMPLoopBoundSharingDirective(S.getDirectiveKind());
2826       if ((RT.isStaticNonchunked(ScheduleKind.Schedule,
2827                                  /* Chunked */ Chunk != nullptr) ||
2828            StaticChunkedOne) &&
2829           !Ordered) {
2830         JumpDest LoopExit =
2831             getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
2832         emitCommonSimdLoop(
2833             *this, S,
2834             [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2835               if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2836                 CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true);
2837               } else if (const auto *C = S.getSingleClause<OMPOrderClause>()) {
2838                 if (C->getKind() == OMPC_ORDER_concurrent)
2839                   CGF.LoopStack.setParallel(/*Enable=*/true);
2840               }
2841             },
2842             [IVSize, IVSigned, Ordered, IL, LB, UB, ST, StaticChunkedOne, Chunk,
2843              &S, ScheduleKind, LoopExit,
2844              &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) {
2845               // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
2846               // When no chunk_size is specified, the iteration space is divided
2847               // into chunks that are approximately equal in size, and at most
2848               // one chunk is distributed to each thread. Note that the size of
2849               // the chunks is unspecified in this case.
2850               CGOpenMPRuntime::StaticRTInput StaticInit(
2851                   IVSize, IVSigned, Ordered, IL.getAddress(CGF),
2852                   LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF),
2853                   StaticChunkedOne ? Chunk : nullptr);
2854               CGF.CGM.getOpenMPRuntime().emitForStaticInit(
2855                   CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind,
2856                   StaticInit);
2857               // UB = min(UB, GlobalUB);
2858               if (!StaticChunkedOne)
2859                 CGF.EmitIgnoredExpr(S.getEnsureUpperBound());
2860               // IV = LB;
2861               CGF.EmitIgnoredExpr(S.getInit());
2862               // For unchunked static schedule generate:
2863               //
2864               // while (idx <= UB) {
2865               //   BODY;
2866               //   ++idx;
2867               // }
2868               //
2869               // For static schedule with chunk one:
2870               //
2871               // while (IV <= PrevUB) {
2872               //   BODY;
2873               //   IV += ST;
2874               // }
2875               CGF.EmitOMPInnerLoop(
2876                   S, LoopScope.requiresCleanups(),
2877                   StaticChunkedOne ? S.getCombinedParForInDistCond()
2878                                    : S.getCond(),
2879                   StaticChunkedOne ? S.getDistInc() : S.getInc(),
2880                   [&S, LoopExit](CodeGenFunction &CGF) {
2881                     CGF.EmitOMPLoopBody(S, LoopExit);
2882                     CGF.EmitStopPoint(&S);
2883                   },
2884                   [](CodeGenFunction &) {});
2885             });
2886         EmitBlock(LoopExit.getBlock());
2887         // Tell the runtime we are done.
2888         auto &&CodeGen = [&S](CodeGenFunction &CGF) {
2889           CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(),
2890                                                          S.getDirectiveKind());
2891         };
2892         OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen);
2893       } else {
2894         const bool IsMonotonic =
2895             Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static ||
2896             ScheduleKind.Schedule == OMPC_SCHEDULE_unknown ||
2897             ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
2898             ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
2899         // Emit the outer loop, which requests its work chunk [LB..UB] from
2900         // runtime and runs the inner loop to process it.
2901         const OMPLoopArguments LoopArguments(
2902             LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this),
2903             IL.getAddress(*this), Chunk, EUB);
2904         EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
2905                             LoopArguments, CGDispatchBounds);
2906       }
2907       if (isOpenMPSimdDirective(S.getDirectiveKind())) {
2908         EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) {
2909           return CGF.Builder.CreateIsNotNull(
2910               CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
2911         });
2912       }
2913       EmitOMPReductionClauseFinal(
2914           S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind())
2915                  ? /*Parallel and Simd*/ OMPD_parallel_for_simd
2916                  : /*Parallel only*/ OMPD_parallel);
2917       // Emit post-update of the reduction variables if IsLastIter != 0.
2918       emitPostUpdateForReductionClause(
2919           *this, S, [IL, &S](CodeGenFunction &CGF) {
2920             return CGF.Builder.CreateIsNotNull(
2921                 CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
2922           });
2923       // Emit final copy of the lastprivate variables if IsLastIter != 0.
2924       if (HasLastprivateClause)
2925         EmitOMPLastprivateClauseFinal(
2926             S, isOpenMPSimdDirective(S.getDirectiveKind()),
2927             Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc())));
2928     }
2929     EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) {
2930       return CGF.Builder.CreateIsNotNull(
2931           CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
2932     });
2933     DoacrossCleanupScope.ForceCleanup();
2934     // We're now done with the loop, so jump to the continuation block.
2935     if (ContBlock) {
2936       EmitBranch(ContBlock);
2937       EmitBlock(ContBlock, /*IsFinished=*/true);
2938     }
2939   }
2940   return HasLastprivateClause;
2941 }
2942 
2943 /// The following two functions generate expressions for the loop lower
2944 /// and upper bounds in case of static and dynamic (dispatch) schedule
2945 /// of the associated 'for' or 'distribute' loop.
2946 static std::pair<LValue, LValue>
2947 emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
2948   const auto &LS = cast<OMPLoopDirective>(S);
2949   LValue LB =
2950       EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable()));
2951   LValue UB =
2952       EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable()));
2953   return {LB, UB};
2954 }
2955 
2956 /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not
2957 /// consider the lower and upper bound expressions generated by the
2958 /// worksharing loop support, but we use 0 and the iteration space size as
2959 /// constants
2960 static std::pair<llvm::Value *, llvm::Value *>
2961 emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S,
2962                           Address LB, Address UB) {
2963   const auto &LS = cast<OMPLoopDirective>(S);
2964   const Expr *IVExpr = LS.getIterationVariable();
2965   const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType());
2966   llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0);
2967   llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration());
2968   return {LBVal, UBVal};
2969 }
2970 
2971 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
2972   bool HasLastprivates = false;
2973   auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2974                                           PrePostActionTy &) {
2975     OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel());
2976     HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
2977                                                  emitForLoopBounds,
2978                                                  emitDispatchForLoopBounds);
2979   };
2980   {
2981     auto LPCRegion =
2982         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
2983     OMPLexicalScope Scope(*this, S, OMPD_unknown);
2984     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2985                                                 S.hasCancel());
2986   }
2987 
2988   // Emit an implicit barrier at the end.
2989   if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates)
2990     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for);
2991   // Check for outer lastprivate conditional update.
2992   checkForLastprivateConditionalUpdate(*this, S);
2993 }
2994 
2995 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
2996   bool HasLastprivates = false;
2997   auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2998                                           PrePostActionTy &) {
2999     HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(),
3000                                                  emitForLoopBounds,
3001                                                  emitDispatchForLoopBounds);
3002   };
3003   {
3004     auto LPCRegion =
3005         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3006     OMPLexicalScope Scope(*this, S, OMPD_unknown);
3007     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
3008   }
3009 
3010   // Emit an implicit barrier at the end.
3011   if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates)
3012     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for);
3013   // Check for outer lastprivate conditional update.
3014   checkForLastprivateConditionalUpdate(*this, S);
3015 }
3016 
3017 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
3018                                 const Twine &Name,
3019                                 llvm::Value *Init = nullptr) {
3020   LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
3021   if (Init)
3022     CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true);
3023   return LVal;
3024 }
3025 
3026 void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
3027   const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt();
3028   const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt);
3029   bool HasLastprivates = false;
3030   auto &&CodeGen = [&S, CapturedStmt, CS,
3031                     &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) {
3032     const ASTContext &C = CGF.getContext();
3033     QualType KmpInt32Ty =
3034         C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
3035     // Emit helper vars inits.
3036     LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
3037                                   CGF.Builder.getInt32(0));
3038     llvm::ConstantInt *GlobalUBVal = CS != nullptr
3039                                          ? CGF.Builder.getInt32(CS->size() - 1)
3040                                          : CGF.Builder.getInt32(0);
3041     LValue UB =
3042         createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
3043     LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
3044                                   CGF.Builder.getInt32(1));
3045     LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
3046                                   CGF.Builder.getInt32(0));
3047     // Loop counter.
3048     LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
3049     OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue);
3050     CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
3051     OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue);
3052     CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
3053     // Generate condition for loop.
3054     BinaryOperator *Cond = BinaryOperator::Create(
3055         C, &IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, OK_Ordinary,
3056         S.getBeginLoc(), FPOptions(C.getLangOpts()));
3057     // Increment for loop counter.
3058     UnaryOperator *Inc = UnaryOperator::Create(
3059         C, &IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
3060         S.getBeginLoc(), true, FPOptions(C.getLangOpts()));
3061     auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) {
3062       // Iterate through all sections and emit a switch construct:
3063       // switch (IV) {
3064       //   case 0:
3065       //     <SectionStmt[0]>;
3066       //     break;
3067       // ...
3068       //   case <NumSection> - 1:
3069       //     <SectionStmt[<NumSection> - 1]>;
3070       //     break;
3071       // }
3072       // .omp.sections.exit:
3073       llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
3074       llvm::SwitchInst *SwitchStmt =
3075           CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()),
3076                                    ExitBB, CS == nullptr ? 1 : CS->size());
3077       if (CS) {
3078         unsigned CaseNumber = 0;
3079         for (const Stmt *SubStmt : CS->children()) {
3080           auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
3081           CGF.EmitBlock(CaseBB);
3082           SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
3083           CGF.EmitStmt(SubStmt);
3084           CGF.EmitBranch(ExitBB);
3085           ++CaseNumber;
3086         }
3087       } else {
3088         llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case");
3089         CGF.EmitBlock(CaseBB);
3090         SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
3091         CGF.EmitStmt(CapturedStmt);
3092         CGF.EmitBranch(ExitBB);
3093       }
3094       CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
3095     };
3096 
3097     CodeGenFunction::OMPPrivateScope LoopScope(CGF);
3098     if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
3099       // Emit implicit barrier to synchronize threads and avoid data races on
3100       // initialization of firstprivate variables and post-update of lastprivate
3101       // variables.
3102       CGF.CGM.getOpenMPRuntime().emitBarrierCall(
3103           CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
3104           /*ForceSimpleCall=*/true);
3105     }
3106     CGF.EmitOMPPrivateClause(S, LoopScope);
3107     CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion(CGF, S, IV);
3108     HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
3109     CGF.EmitOMPReductionClauseInit(S, LoopScope);
3110     (void)LoopScope.Privatize();
3111     if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
3112       CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S);
3113 
3114     // Emit static non-chunked loop.
3115     OpenMPScheduleTy ScheduleKind;
3116     ScheduleKind.Schedule = OMPC_SCHEDULE_static;
3117     CGOpenMPRuntime::StaticRTInput StaticInit(
3118         /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(CGF),
3119         LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF));
3120     CGF.CGM.getOpenMPRuntime().emitForStaticInit(
3121         CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit);
3122     // UB = min(UB, GlobalUB);
3123     llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc());
3124     llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect(
3125         CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
3126     CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
3127     // IV = LB;
3128     CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV);
3129     // while (idx <= UB) { BODY; ++idx; }
3130     CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, Cond, Inc, BodyGen,
3131                          [](CodeGenFunction &) {});
3132     // Tell the runtime we are done.
3133     auto &&CodeGen = [&S](CodeGenFunction &CGF) {
3134       CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(),
3135                                                      S.getDirectiveKind());
3136     };
3137     CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen);
3138     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
3139     // Emit post-update of the reduction variables if IsLastIter != 0.
3140     emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) {
3141       return CGF.Builder.CreateIsNotNull(
3142           CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
3143     });
3144 
3145     // Emit final copy of the lastprivate variables if IsLastIter != 0.
3146     if (HasLastprivates)
3147       CGF.EmitOMPLastprivateClauseFinal(
3148           S, /*NoFinals=*/false,
3149           CGF.Builder.CreateIsNotNull(
3150               CGF.EmitLoadOfScalar(IL, S.getBeginLoc())));
3151   };
3152 
3153   bool HasCancel = false;
3154   if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
3155     HasCancel = OSD->hasCancel();
3156   else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
3157     HasCancel = OPSD->hasCancel();
3158   OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel);
3159   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
3160                                               HasCancel);
3161   // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
3162   // clause. Otherwise the barrier will be generated by the codegen for the
3163   // directive.
3164   if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
3165     // Emit implicit barrier to synchronize threads and avoid data races on
3166     // initialization of firstprivate variables.
3167     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(),
3168                                            OMPD_unknown);
3169   }
3170 }
3171 
3172 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
3173   {
3174     auto LPCRegion =
3175         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3176     OMPLexicalScope Scope(*this, S, OMPD_unknown);
3177     EmitSections(S);
3178   }
3179   // Emit an implicit barrier at the end.
3180   if (!S.getSingleClause<OMPNowaitClause>()) {
3181     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(),
3182                                            OMPD_sections);
3183   }
3184   // Check for outer lastprivate conditional update.
3185   checkForLastprivateConditionalUpdate(*this, S);
3186 }
3187 
3188 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
3189   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3190     CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
3191   };
3192   OMPLexicalScope Scope(*this, S, OMPD_unknown);
3193   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
3194                                               S.hasCancel());
3195 }
3196 
3197 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
3198   llvm::SmallVector<const Expr *, 8> CopyprivateVars;
3199   llvm::SmallVector<const Expr *, 8> DestExprs;
3200   llvm::SmallVector<const Expr *, 8> SrcExprs;
3201   llvm::SmallVector<const Expr *, 8> AssignmentOps;
3202   // Check if there are any 'copyprivate' clauses associated with this
3203   // 'single' construct.
3204   // Build a list of copyprivate variables along with helper expressions
3205   // (<source>, <destination>, <destination>=<source> expressions)
3206   for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
3207     CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
3208     DestExprs.append(C->destination_exprs().begin(),
3209                      C->destination_exprs().end());
3210     SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
3211     AssignmentOps.append(C->assignment_ops().begin(),
3212                          C->assignment_ops().end());
3213   }
3214   // Emit code for 'single' region along with 'copyprivate' clauses
3215   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3216     Action.Enter(CGF);
3217     OMPPrivateScope SingleScope(CGF);
3218     (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
3219     CGF.EmitOMPPrivateClause(S, SingleScope);
3220     (void)SingleScope.Privatize();
3221     CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
3222   };
3223   {
3224     auto LPCRegion =
3225         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3226     OMPLexicalScope Scope(*this, S, OMPD_unknown);
3227     CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(),
3228                                             CopyprivateVars, DestExprs,
3229                                             SrcExprs, AssignmentOps);
3230   }
3231   // Emit an implicit barrier at the end (to avoid data race on firstprivate
3232   // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
3233   if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
3234     CGM.getOpenMPRuntime().emitBarrierCall(
3235         *this, S.getBeginLoc(),
3236         S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
3237   }
3238   // Check for outer lastprivate conditional update.
3239   checkForLastprivateConditionalUpdate(*this, S);
3240 }
3241 
3242 static void emitMaster(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
3243   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3244     Action.Enter(CGF);
3245     CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
3246   };
3247   CGF.CGM.getOpenMPRuntime().emitMasterRegion(CGF, CodeGen, S.getBeginLoc());
3248 }
3249 
3250 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
3251   if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) {
3252     using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
3253 
3254     const CapturedStmt *CS = S.getInnermostCapturedStmt();
3255     const Stmt *MasterRegionBodyStmt = CS->getCapturedStmt();
3256 
3257     auto FiniCB = [this](InsertPointTy IP) {
3258       OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
3259     };
3260 
3261     auto BodyGenCB = [MasterRegionBodyStmt, this](InsertPointTy AllocaIP,
3262                                                   InsertPointTy CodeGenIP,
3263                                                   llvm::BasicBlock &FiniBB) {
3264       OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB);
3265       OMPBuilderCBHelpers::EmitOMPRegionBody(*this, MasterRegionBodyStmt,
3266                                              CodeGenIP, FiniBB);
3267     };
3268 
3269     CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);
3270     CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI);
3271     Builder.restoreIP(OMPBuilder->CreateMaster(Builder, BodyGenCB, FiniCB));
3272 
3273     return;
3274   }
3275   OMPLexicalScope Scope(*this, S, OMPD_unknown);
3276   emitMaster(*this, S);
3277 }
3278 
3279 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
3280   if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) {
3281     using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
3282 
3283     const CapturedStmt *CS = S.getInnermostCapturedStmt();
3284     const Stmt *CriticalRegionBodyStmt = CS->getCapturedStmt();
3285     const Expr *Hint = nullptr;
3286     if (const auto *HintClause = S.getSingleClause<OMPHintClause>())
3287       Hint = HintClause->getHint();
3288 
3289     // TODO: This is slightly different from what's currently being done in
3290     // clang. Fix the Int32Ty to IntPtrTy (pointer width size) when everything
3291     // about typing is final.
3292     llvm::Value *HintInst = nullptr;
3293     if (Hint)
3294       HintInst =
3295           Builder.CreateIntCast(EmitScalarExpr(Hint), CGM.Int32Ty, false);
3296 
3297     auto FiniCB = [this](InsertPointTy IP) {
3298       OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
3299     };
3300 
3301     auto BodyGenCB = [CriticalRegionBodyStmt, this](InsertPointTy AllocaIP,
3302                                                     InsertPointTy CodeGenIP,
3303                                                     llvm::BasicBlock &FiniBB) {
3304       OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB);
3305       OMPBuilderCBHelpers::EmitOMPRegionBody(*this, CriticalRegionBodyStmt,
3306                                              CodeGenIP, FiniBB);
3307     };
3308 
3309     CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);
3310     CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI);
3311     Builder.restoreIP(OMPBuilder->CreateCritical(
3312         Builder, BodyGenCB, FiniCB, S.getDirectiveName().getAsString(),
3313         HintInst));
3314 
3315     return;
3316   }
3317 
3318   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3319     Action.Enter(CGF);
3320     CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
3321   };
3322   const Expr *Hint = nullptr;
3323   if (const auto *HintClause = S.getSingleClause<OMPHintClause>())
3324     Hint = HintClause->getHint();
3325   OMPLexicalScope Scope(*this, S, OMPD_unknown);
3326   CGM.getOpenMPRuntime().emitCriticalRegion(*this,
3327                                             S.getDirectiveName().getAsString(),
3328                                             CodeGen, S.getBeginLoc(), Hint);
3329 }
3330 
3331 void CodeGenFunction::EmitOMPParallelForDirective(
3332     const OMPParallelForDirective &S) {
3333   // Emit directive as a combined directive that consists of two implicit
3334   // directives: 'parallel' with 'for' directive.
3335   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3336     Action.Enter(CGF);
3337     OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel());
3338     CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
3339                                emitDispatchForLoopBounds);
3340   };
3341   {
3342     auto LPCRegion =
3343         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3344     emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen,
3345                                    emitEmptyBoundParameters);
3346   }
3347   // Check for outer lastprivate conditional update.
3348   checkForLastprivateConditionalUpdate(*this, S);
3349 }
3350 
3351 void CodeGenFunction::EmitOMPParallelForSimdDirective(
3352     const OMPParallelForSimdDirective &S) {
3353   // Emit directive as a combined directive that consists of two implicit
3354   // directives: 'parallel' with 'for' directive.
3355   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3356     Action.Enter(CGF);
3357     CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
3358                                emitDispatchForLoopBounds);
3359   };
3360   {
3361     auto LPCRegion =
3362         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3363     emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen,
3364                                    emitEmptyBoundParameters);
3365   }
3366   // Check for outer lastprivate conditional update.
3367   checkForLastprivateConditionalUpdate(*this, S);
3368 }
3369 
3370 void CodeGenFunction::EmitOMPParallelMasterDirective(
3371     const OMPParallelMasterDirective &S) {
3372   // Emit directive as a combined directive that consists of two implicit
3373   // directives: 'parallel' with 'master' directive.
3374   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3375     Action.Enter(CGF);
3376     OMPPrivateScope PrivateScope(CGF);
3377     bool Copyins = CGF.EmitOMPCopyinClause(S);
3378     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3379     if (Copyins) {
3380       // Emit implicit barrier to synchronize threads and avoid data races on
3381       // propagation master's thread values of threadprivate variables to local
3382       // instances of that variables of all other implicit threads.
3383       CGF.CGM.getOpenMPRuntime().emitBarrierCall(
3384           CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
3385           /*ForceSimpleCall=*/true);
3386     }
3387     CGF.EmitOMPPrivateClause(S, PrivateScope);
3388     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
3389     (void)PrivateScope.Privatize();
3390     emitMaster(CGF, S);
3391     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
3392   };
3393   {
3394     auto LPCRegion =
3395         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3396     emitCommonOMPParallelDirective(*this, S, OMPD_master, CodeGen,
3397                                    emitEmptyBoundParameters);
3398     emitPostUpdateForReductionClause(*this, S,
3399                                      [](CodeGenFunction &) { return nullptr; });
3400   }
3401   // Check for outer lastprivate conditional update.
3402   checkForLastprivateConditionalUpdate(*this, S);
3403 }
3404 
3405 void CodeGenFunction::EmitOMPParallelSectionsDirective(
3406     const OMPParallelSectionsDirective &S) {
3407   // Emit directive as a combined directive that consists of two implicit
3408   // directives: 'parallel' with 'sections' directive.
3409   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3410     Action.Enter(CGF);
3411     CGF.EmitSections(S);
3412   };
3413   {
3414     auto LPCRegion =
3415         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3416     emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen,
3417                                    emitEmptyBoundParameters);
3418   }
3419   // Check for outer lastprivate conditional update.
3420   checkForLastprivateConditionalUpdate(*this, S);
3421 }
3422 
3423 void CodeGenFunction::EmitOMPTaskBasedDirective(
3424     const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion,
3425     const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen,
3426     OMPTaskDataTy &Data) {
3427   // Emit outlined function for task construct.
3428   const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion);
3429   auto I = CS->getCapturedDecl()->param_begin();
3430   auto PartId = std::next(I);
3431   auto TaskT = std::next(I, 4);
3432   // Check if the task is final
3433   if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
3434     // If the condition constant folds and can be elided, try to avoid emitting
3435     // the condition and the dead arm of the if/else.
3436     const Expr *Cond = Clause->getCondition();
3437     bool CondConstant;
3438     if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
3439       Data.Final.setInt(CondConstant);
3440     else
3441       Data.Final.setPointer(EvaluateExprAsBool(Cond));
3442   } else {
3443     // By default the task is not final.
3444     Data.Final.setInt(/*IntVal=*/false);
3445   }
3446   // Check if the task has 'priority' clause.
3447   if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) {
3448     const Expr *Prio = Clause->getPriority();
3449     Data.Priority.setInt(/*IntVal=*/true);
3450     Data.Priority.setPointer(EmitScalarConversion(
3451         EmitScalarExpr(Prio), Prio->getType(),
3452         getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
3453         Prio->getExprLoc()));
3454   }
3455   // The first function argument for tasks is a thread id, the second one is a
3456   // part id (0 for tied tasks, >=0 for untied task).
3457   llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
3458   // Get list of private variables.
3459   for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
3460     auto IRef = C->varlist_begin();
3461     for (const Expr *IInit : C->private_copies()) {
3462       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
3463       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
3464         Data.PrivateVars.push_back(*IRef);
3465         Data.PrivateCopies.push_back(IInit);
3466       }
3467       ++IRef;
3468     }
3469   }
3470   EmittedAsPrivate.clear();
3471   // Get list of firstprivate variables.
3472   for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
3473     auto IRef = C->varlist_begin();
3474     auto IElemInitRef = C->inits().begin();
3475     for (const Expr *IInit : C->private_copies()) {
3476       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
3477       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
3478         Data.FirstprivateVars.push_back(*IRef);
3479         Data.FirstprivateCopies.push_back(IInit);
3480         Data.FirstprivateInits.push_back(*IElemInitRef);
3481       }
3482       ++IRef;
3483       ++IElemInitRef;
3484     }
3485   }
3486   // Get list of lastprivate variables (for taskloops).
3487   llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs;
3488   for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
3489     auto IRef = C->varlist_begin();
3490     auto ID = C->destination_exprs().begin();
3491     for (const Expr *IInit : C->private_copies()) {
3492       const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
3493       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
3494         Data.LastprivateVars.push_back(*IRef);
3495         Data.LastprivateCopies.push_back(IInit);
3496       }
3497       LastprivateDstsOrigs.insert(
3498           {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()),
3499            cast<DeclRefExpr>(*IRef)});
3500       ++IRef;
3501       ++ID;
3502     }
3503   }
3504   SmallVector<const Expr *, 4> LHSs;
3505   SmallVector<const Expr *, 4> RHSs;
3506   for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) {
3507     Data.ReductionVars.append(C->varlist_begin(), C->varlist_end());
3508     Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end());
3509     Data.ReductionCopies.append(C->privates().begin(), C->privates().end());
3510     Data.ReductionOps.append(C->reduction_ops().begin(),
3511                              C->reduction_ops().end());
3512     LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
3513     RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
3514   }
3515   Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit(
3516       *this, S.getBeginLoc(), LHSs, RHSs, Data);
3517   // Build list of dependences.
3518   for (const auto *C : S.getClausesOfKind<OMPDependClause>()) {
3519     OMPTaskDataTy::DependData &DD =
3520         Data.Dependences.emplace_back(C->getDependencyKind(), C->getModifier());
3521     DD.DepExprs.append(C->varlist_begin(), C->varlist_end());
3522   }
3523   auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs,
3524                     CapturedRegion](CodeGenFunction &CGF,
3525                                     PrePostActionTy &Action) {
3526     // Set proper addresses for generated private copies.
3527     OMPPrivateScope Scope(CGF);
3528     llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> FirstprivatePtrs;
3529     if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() ||
3530         !Data.LastprivateVars.empty()) {
3531       llvm::FunctionType *CopyFnTy = llvm::FunctionType::get(
3532           CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true);
3533       enum { PrivatesParam = 2, CopyFnParam = 3 };
3534       llvm::Value *CopyFn = CGF.Builder.CreateLoad(
3535           CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam)));
3536       llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(
3537           CS->getCapturedDecl()->getParam(PrivatesParam)));
3538       // Map privates.
3539       llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
3540       llvm::SmallVector<llvm::Value *, 16> CallArgs;
3541       CallArgs.push_back(PrivatesPtr);
3542       for (const Expr *E : Data.PrivateVars) {
3543         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3544         Address PrivatePtr = CGF.CreateMemTemp(
3545             CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
3546         PrivatePtrs.emplace_back(VD, PrivatePtr);
3547         CallArgs.push_back(PrivatePtr.getPointer());
3548       }
3549       for (const Expr *E : Data.FirstprivateVars) {
3550         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3551         Address PrivatePtr =
3552             CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
3553                               ".firstpriv.ptr.addr");
3554         PrivatePtrs.emplace_back(VD, PrivatePtr);
3555         FirstprivatePtrs.emplace_back(VD, PrivatePtr);
3556         CallArgs.push_back(PrivatePtr.getPointer());
3557       }
3558       for (const Expr *E : Data.LastprivateVars) {
3559         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3560         Address PrivatePtr =
3561             CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
3562                               ".lastpriv.ptr.addr");
3563         PrivatePtrs.emplace_back(VD, PrivatePtr);
3564         CallArgs.push_back(PrivatePtr.getPointer());
3565       }
3566       CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
3567           CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs);
3568       for (const auto &Pair : LastprivateDstsOrigs) {
3569         const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl());
3570         DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(OrigVD),
3571                         /*RefersToEnclosingVariableOrCapture=*/
3572                             CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr,
3573                         Pair.second->getType(), VK_LValue,
3574                         Pair.second->getExprLoc());
3575         Scope.addPrivate(Pair.first, [&CGF, &DRE]() {
3576           return CGF.EmitLValue(&DRE).getAddress(CGF);
3577         });
3578       }
3579       for (const auto &Pair : PrivatePtrs) {
3580         Address Replacement(CGF.Builder.CreateLoad(Pair.second),
3581                             CGF.getContext().getDeclAlign(Pair.first));
3582         Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
3583       }
3584     }
3585     if (Data.Reductions) {
3586       OMPPrivateScope FirstprivateScope(CGF);
3587       for (const auto &Pair : FirstprivatePtrs) {
3588         Address Replacement(CGF.Builder.CreateLoad(Pair.second),
3589                             CGF.getContext().getDeclAlign(Pair.first));
3590         FirstprivateScope.addPrivate(Pair.first,
3591                                      [Replacement]() { return Replacement; });
3592       }
3593       (void)FirstprivateScope.Privatize();
3594       OMPLexicalScope LexScope(CGF, S, CapturedRegion);
3595       ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionVars,
3596                              Data.ReductionCopies, Data.ReductionOps);
3597       llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad(
3598           CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9)));
3599       for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) {
3600         RedCG.emitSharedOrigLValue(CGF, Cnt);
3601         RedCG.emitAggregateType(CGF, Cnt);
3602         // FIXME: This must removed once the runtime library is fixed.
3603         // Emit required threadprivate variables for
3604         // initializer/combiner/finalizer.
3605         CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(),
3606                                                            RedCG, Cnt);
3607         Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
3608             CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
3609         Replacement =
3610             Address(CGF.EmitScalarConversion(
3611                         Replacement.getPointer(), CGF.getContext().VoidPtrTy,
3612                         CGF.getContext().getPointerType(
3613                             Data.ReductionCopies[Cnt]->getType()),
3614                         Data.ReductionCopies[Cnt]->getExprLoc()),
3615                     Replacement.getAlignment());
3616         Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
3617         Scope.addPrivate(RedCG.getBaseDecl(Cnt),
3618                          [Replacement]() { return Replacement; });
3619       }
3620     }
3621     // Privatize all private variables except for in_reduction items.
3622     (void)Scope.Privatize();
3623     SmallVector<const Expr *, 4> InRedVars;
3624     SmallVector<const Expr *, 4> InRedPrivs;
3625     SmallVector<const Expr *, 4> InRedOps;
3626     SmallVector<const Expr *, 4> TaskgroupDescriptors;
3627     for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) {
3628       auto IPriv = C->privates().begin();
3629       auto IRed = C->reduction_ops().begin();
3630       auto ITD = C->taskgroup_descriptors().begin();
3631       for (const Expr *Ref : C->varlists()) {
3632         InRedVars.emplace_back(Ref);
3633         InRedPrivs.emplace_back(*IPriv);
3634         InRedOps.emplace_back(*IRed);
3635         TaskgroupDescriptors.emplace_back(*ITD);
3636         std::advance(IPriv, 1);
3637         std::advance(IRed, 1);
3638         std::advance(ITD, 1);
3639       }
3640     }
3641     // Privatize in_reduction items here, because taskgroup descriptors must be
3642     // privatized earlier.
3643     OMPPrivateScope InRedScope(CGF);
3644     if (!InRedVars.empty()) {
3645       ReductionCodeGen RedCG(InRedVars, InRedVars, InRedPrivs, InRedOps);
3646       for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) {
3647         RedCG.emitSharedOrigLValue(CGF, Cnt);
3648         RedCG.emitAggregateType(CGF, Cnt);
3649         // The taskgroup descriptor variable is always implicit firstprivate and
3650         // privatized already during processing of the firstprivates.
3651         // FIXME: This must removed once the runtime library is fixed.
3652         // Emit required threadprivate variables for
3653         // initializer/combiner/finalizer.
3654         CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(),
3655                                                            RedCG, Cnt);
3656         llvm::Value *ReductionsPtr;
3657         if (const Expr *TRExpr = TaskgroupDescriptors[Cnt]) {
3658           ReductionsPtr = CGF.EmitLoadOfScalar(CGF.EmitLValue(TRExpr),
3659                                                TRExpr->getExprLoc());
3660         } else {
3661           ReductionsPtr = llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
3662         }
3663         Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem(
3664             CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt));
3665         Replacement = Address(
3666             CGF.EmitScalarConversion(
3667                 Replacement.getPointer(), CGF.getContext().VoidPtrTy,
3668                 CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()),
3669                 InRedPrivs[Cnt]->getExprLoc()),
3670             Replacement.getAlignment());
3671         Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement);
3672         InRedScope.addPrivate(RedCG.getBaseDecl(Cnt),
3673                               [Replacement]() { return Replacement; });
3674       }
3675     }
3676     (void)InRedScope.Privatize();
3677 
3678     Action.Enter(CGF);
3679     BodyGen(CGF);
3680   };
3681   llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
3682       S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
3683       Data.NumberOfParts);
3684   OMPLexicalScope Scope(*this, S, llvm::None,
3685                         !isOpenMPParallelDirective(S.getDirectiveKind()) &&
3686                             !isOpenMPSimdDirective(S.getDirectiveKind()));
3687   TaskGen(*this, OutlinedFn, Data);
3688 }
3689 
3690 static ImplicitParamDecl *
3691 createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data,
3692                                   QualType Ty, CapturedDecl *CD,
3693                                   SourceLocation Loc) {
3694   auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty,
3695                                            ImplicitParamDecl::Other);
3696   auto *OrigRef = DeclRefExpr::Create(
3697       C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD,
3698       /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue);
3699   auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty,
3700                                               ImplicitParamDecl::Other);
3701   auto *PrivateRef = DeclRefExpr::Create(
3702       C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD,
3703       /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue);
3704   QualType ElemType = C.getBaseElementType(Ty);
3705   auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType,
3706                                            ImplicitParamDecl::Other);
3707   auto *InitRef = DeclRefExpr::Create(
3708       C, NestedNameSpecifierLoc(), SourceLocation(), InitVD,
3709       /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue);
3710   PrivateVD->setInitStyle(VarDecl::CInit);
3711   PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue,
3712                                               InitRef, /*BasePath=*/nullptr,
3713                                               VK_RValue));
3714   Data.FirstprivateVars.emplace_back(OrigRef);
3715   Data.FirstprivateCopies.emplace_back(PrivateRef);
3716   Data.FirstprivateInits.emplace_back(InitRef);
3717   return OrigVD;
3718 }
3719 
3720 void CodeGenFunction::EmitOMPTargetTaskBasedDirective(
3721     const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen,
3722     OMPTargetDataInfo &InputInfo) {
3723   // Emit outlined function for task construct.
3724   const CapturedStmt *CS = S.getCapturedStmt(OMPD_task);
3725   Address CapturedStruct = GenerateCapturedStmtArgument(*CS);
3726   QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
3727   auto I = CS->getCapturedDecl()->param_begin();
3728   auto PartId = std::next(I);
3729   auto TaskT = std::next(I, 4);
3730   OMPTaskDataTy Data;
3731   // The task is not final.
3732   Data.Final.setInt(/*IntVal=*/false);
3733   // Get list of firstprivate variables.
3734   for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
3735     auto IRef = C->varlist_begin();
3736     auto IElemInitRef = C->inits().begin();
3737     for (auto *IInit : C->private_copies()) {
3738       Data.FirstprivateVars.push_back(*IRef);
3739       Data.FirstprivateCopies.push_back(IInit);
3740       Data.FirstprivateInits.push_back(*IElemInitRef);
3741       ++IRef;
3742       ++IElemInitRef;
3743     }
3744   }
3745   OMPPrivateScope TargetScope(*this);
3746   VarDecl *BPVD = nullptr;
3747   VarDecl *PVD = nullptr;
3748   VarDecl *SVD = nullptr;
3749   if (InputInfo.NumberOfTargetItems > 0) {
3750     auto *CD = CapturedDecl::Create(
3751         getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0);
3752     llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems);
3753     QualType BaseAndPointersType = getContext().getConstantArrayType(
3754         getContext().VoidPtrTy, ArrSize, nullptr, ArrayType::Normal,
3755         /*IndexTypeQuals=*/0);
3756     BPVD = createImplicitFirstprivateForType(
3757         getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc());
3758     PVD = createImplicitFirstprivateForType(
3759         getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc());
3760     QualType SizesType = getContext().getConstantArrayType(
3761         getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1),
3762         ArrSize, nullptr, ArrayType::Normal,
3763         /*IndexTypeQuals=*/0);
3764     SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD,
3765                                             S.getBeginLoc());
3766     TargetScope.addPrivate(
3767         BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; });
3768     TargetScope.addPrivate(PVD,
3769                            [&InputInfo]() { return InputInfo.PointersArray; });
3770     TargetScope.addPrivate(SVD,
3771                            [&InputInfo]() { return InputInfo.SizesArray; });
3772   }
3773   (void)TargetScope.Privatize();
3774   // Build list of dependences.
3775   for (const auto *C : S.getClausesOfKind<OMPDependClause>()) {
3776     OMPTaskDataTy::DependData &DD =
3777         Data.Dependences.emplace_back(C->getDependencyKind(), C->getModifier());
3778     DD.DepExprs.append(C->varlist_begin(), C->varlist_end());
3779   }
3780   auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD,
3781                     &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) {
3782     // Set proper addresses for generated private copies.
3783     OMPPrivateScope Scope(CGF);
3784     if (!Data.FirstprivateVars.empty()) {
3785       llvm::FunctionType *CopyFnTy = llvm::FunctionType::get(
3786           CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true);
3787       enum { PrivatesParam = 2, CopyFnParam = 3 };
3788       llvm::Value *CopyFn = CGF.Builder.CreateLoad(
3789           CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam)));
3790       llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(
3791           CS->getCapturedDecl()->getParam(PrivatesParam)));
3792       // Map privates.
3793       llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
3794       llvm::SmallVector<llvm::Value *, 16> CallArgs;
3795       CallArgs.push_back(PrivatesPtr);
3796       for (const Expr *E : Data.FirstprivateVars) {
3797         const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3798         Address PrivatePtr =
3799             CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
3800                               ".firstpriv.ptr.addr");
3801         PrivatePtrs.emplace_back(VD, PrivatePtr);
3802         CallArgs.push_back(PrivatePtr.getPointer());
3803       }
3804       CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
3805           CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs);
3806       for (const auto &Pair : PrivatePtrs) {
3807         Address Replacement(CGF.Builder.CreateLoad(Pair.second),
3808                             CGF.getContext().getDeclAlign(Pair.first));
3809         Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
3810       }
3811     }
3812     // Privatize all private variables except for in_reduction items.
3813     (void)Scope.Privatize();
3814     if (InputInfo.NumberOfTargetItems > 0) {
3815       InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP(
3816           CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0);
3817       InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP(
3818           CGF.GetAddrOfLocalVar(PVD), /*Index=*/0);
3819       InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP(
3820           CGF.GetAddrOfLocalVar(SVD), /*Index=*/0);
3821     }
3822 
3823     Action.Enter(CGF);
3824     OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false);
3825     BodyGen(CGF);
3826   };
3827   llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
3828       S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true,
3829       Data.NumberOfParts);
3830   llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0);
3831   IntegerLiteral IfCond(getContext(), TrueOrFalse,
3832                         getContext().getIntTypeForBitwidth(32, /*Signed=*/0),
3833                         SourceLocation());
3834 
3835   CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn,
3836                                       SharedsTy, CapturedStruct, &IfCond, Data);
3837 }
3838 
3839 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
3840   // Emit outlined function for task construct.
3841   const CapturedStmt *CS = S.getCapturedStmt(OMPD_task);
3842   Address CapturedStruct = GenerateCapturedStmtArgument(*CS);
3843   QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
3844   const Expr *IfCond = nullptr;
3845   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3846     if (C->getNameModifier() == OMPD_unknown ||
3847         C->getNameModifier() == OMPD_task) {
3848       IfCond = C->getCondition();
3849       break;
3850     }
3851   }
3852 
3853   OMPTaskDataTy Data;
3854   // Check if we should emit tied or untied task.
3855   Data.Tied = !S.getSingleClause<OMPUntiedClause>();
3856   auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
3857     CGF.EmitStmt(CS->getCapturedStmt());
3858   };
3859   auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
3860                     IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn,
3861                             const OMPTaskDataTy &Data) {
3862     CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn,
3863                                             SharedsTy, CapturedStruct, IfCond,
3864                                             Data);
3865   };
3866   auto LPCRegion =
3867       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
3868   EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data);
3869 }
3870 
3871 void CodeGenFunction::EmitOMPTaskyieldDirective(
3872     const OMPTaskyieldDirective &S) {
3873   CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc());
3874 }
3875 
3876 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
3877   CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier);
3878 }
3879 
3880 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
3881   CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc());
3882 }
3883 
3884 void CodeGenFunction::EmitOMPTaskgroupDirective(
3885     const OMPTaskgroupDirective &S) {
3886   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3887     Action.Enter(CGF);
3888     if (const Expr *E = S.getReductionRef()) {
3889       SmallVector<const Expr *, 4> LHSs;
3890       SmallVector<const Expr *, 4> RHSs;
3891       OMPTaskDataTy Data;
3892       for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) {
3893         Data.ReductionVars.append(C->varlist_begin(), C->varlist_end());
3894         Data.ReductionOrigs.append(C->varlist_begin(), C->varlist_end());
3895         Data.ReductionCopies.append(C->privates().begin(), C->privates().end());
3896         Data.ReductionOps.append(C->reduction_ops().begin(),
3897                                  C->reduction_ops().end());
3898         LHSs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
3899         RHSs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
3900       }
3901       llvm::Value *ReductionDesc =
3902           CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(),
3903                                                            LHSs, RHSs, Data);
3904       const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
3905       CGF.EmitVarDecl(*VD);
3906       CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD),
3907                             /*Volatile=*/false, E->getType());
3908     }
3909     CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
3910   };
3911   OMPLexicalScope Scope(*this, S, OMPD_unknown);
3912   CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc());
3913 }
3914 
3915 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
3916   llvm::AtomicOrdering AO = S.getSingleClause<OMPFlushClause>()
3917                                 ? llvm::AtomicOrdering::NotAtomic
3918                                 : llvm::AtomicOrdering::AcquireRelease;
3919   CGM.getOpenMPRuntime().emitFlush(
3920       *this,
3921       [&S]() -> ArrayRef<const Expr *> {
3922         if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>())
3923           return llvm::makeArrayRef(FlushClause->varlist_begin(),
3924                                     FlushClause->varlist_end());
3925         return llvm::None;
3926       }(),
3927       S.getBeginLoc(), AO);
3928 }
3929 
3930 void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) {
3931   const auto *DO = S.getSingleClause<OMPDepobjClause>();
3932   LValue DOLVal = EmitLValue(DO->getDepobj());
3933   if (const auto *DC = S.getSingleClause<OMPDependClause>()) {
3934     OMPTaskDataTy::DependData Dependencies(DC->getDependencyKind(),
3935                                            DC->getModifier());
3936     Dependencies.DepExprs.append(DC->varlist_begin(), DC->varlist_end());
3937     Address DepAddr = CGM.getOpenMPRuntime().emitDepobjDependClause(
3938         *this, Dependencies, DC->getBeginLoc());
3939     EmitStoreOfScalar(DepAddr.getPointer(), DOLVal);
3940     return;
3941   }
3942   if (const auto *DC = S.getSingleClause<OMPDestroyClause>()) {
3943     CGM.getOpenMPRuntime().emitDestroyClause(*this, DOLVal, DC->getBeginLoc());
3944     return;
3945   }
3946   if (const auto *UC = S.getSingleClause<OMPUpdateClause>()) {
3947     CGM.getOpenMPRuntime().emitUpdateClause(
3948         *this, DOLVal, UC->getDependencyKind(), UC->getBeginLoc());
3949     return;
3950   }
3951 }
3952 
3953 void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S,
3954                                             const CodeGenLoopTy &CodeGenLoop,
3955                                             Expr *IncExpr) {
3956   // Emit the loop iteration variable.
3957   const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
3958   const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl());
3959   EmitVarDecl(*IVDecl);
3960 
3961   // Emit the iterations count variable.
3962   // If it is not a variable, Sema decided to calculate iterations count on each
3963   // iteration (e.g., it is foldable into a constant).
3964   if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3965     EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3966     // Emit calculation of the iterations count.
3967     EmitIgnoredExpr(S.getCalcLastIteration());
3968   }
3969 
3970   CGOpenMPRuntime &RT = CGM.getOpenMPRuntime();
3971 
3972   bool HasLastprivateClause = false;
3973   // Check pre-condition.
3974   {
3975     OMPLoopScope PreInitScope(*this, S);
3976     // Skip the entire loop if we don't meet the precondition.
3977     // If the condition constant folds and can be elided, avoid emitting the
3978     // whole loop.
3979     bool CondConstant;
3980     llvm::BasicBlock *ContBlock = nullptr;
3981     if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3982       if (!CondConstant)
3983         return;
3984     } else {
3985       llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then");
3986       ContBlock = createBasicBlock("omp.precond.end");
3987       emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
3988                   getProfileCount(&S));
3989       EmitBlock(ThenBlock);
3990       incrementProfileCounter(&S);
3991     }
3992 
3993     emitAlignedClause(*this, S);
3994     // Emit 'then' code.
3995     {
3996       // Emit helper vars inits.
3997 
3998       LValue LB = EmitOMPHelperVar(
3999           *this, cast<DeclRefExpr>(
4000                      (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
4001                           ? S.getCombinedLowerBoundVariable()
4002                           : S.getLowerBoundVariable())));
4003       LValue UB = EmitOMPHelperVar(
4004           *this, cast<DeclRefExpr>(
4005                      (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
4006                           ? S.getCombinedUpperBoundVariable()
4007                           : S.getUpperBoundVariable())));
4008       LValue ST =
4009           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
4010       LValue IL =
4011           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
4012 
4013       OMPPrivateScope LoopScope(*this);
4014       if (EmitOMPFirstprivateClause(S, LoopScope)) {
4015         // Emit implicit barrier to synchronize threads and avoid data races
4016         // on initialization of firstprivate variables and post-update of
4017         // lastprivate variables.
4018         CGM.getOpenMPRuntime().emitBarrierCall(
4019             *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false,
4020             /*ForceSimpleCall=*/true);
4021       }
4022       EmitOMPPrivateClause(S, LoopScope);
4023       if (isOpenMPSimdDirective(S.getDirectiveKind()) &&
4024           !isOpenMPParallelDirective(S.getDirectiveKind()) &&
4025           !isOpenMPTeamsDirective(S.getDirectiveKind()))
4026         EmitOMPReductionClauseInit(S, LoopScope);
4027       HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
4028       EmitOMPPrivateLoopCounters(S, LoopScope);
4029       (void)LoopScope.Privatize();
4030       if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
4031         CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S);
4032 
4033       // Detect the distribute schedule kind and chunk.
4034       llvm::Value *Chunk = nullptr;
4035       OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
4036       if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
4037         ScheduleKind = C->getDistScheduleKind();
4038         if (const Expr *Ch = C->getChunkSize()) {
4039           Chunk = EmitScalarExpr(Ch);
4040           Chunk = EmitScalarConversion(Chunk, Ch->getType(),
4041                                        S.getIterationVariable()->getType(),
4042                                        S.getBeginLoc());
4043         }
4044       } else {
4045         // Default behaviour for dist_schedule clause.
4046         CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk(
4047             *this, S, ScheduleKind, Chunk);
4048       }
4049       const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
4050       const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
4051 
4052       // OpenMP [2.10.8, distribute Construct, Description]
4053       // If dist_schedule is specified, kind must be static. If specified,
4054       // iterations are divided into chunks of size chunk_size, chunks are
4055       // assigned to the teams of the league in a round-robin fashion in the
4056       // order of the team number. When no chunk_size is specified, the
4057       // iteration space is divided into chunks that are approximately equal
4058       // in size, and at most one chunk is distributed to each team of the
4059       // league. The size of the chunks is unspecified in this case.
4060       bool StaticChunked = RT.isStaticChunked(
4061           ScheduleKind, /* Chunked */ Chunk != nullptr) &&
4062           isOpenMPLoopBoundSharingDirective(S.getDirectiveKind());
4063       if (RT.isStaticNonchunked(ScheduleKind,
4064                                 /* Chunked */ Chunk != nullptr) ||
4065           StaticChunked) {
4066         CGOpenMPRuntime::StaticRTInput StaticInit(
4067             IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(*this),
4068             LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this),
4069             StaticChunked ? Chunk : nullptr);
4070         RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind,
4071                                     StaticInit);
4072         JumpDest LoopExit =
4073             getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
4074         // UB = min(UB, GlobalUB);
4075         EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
4076                             ? S.getCombinedEnsureUpperBound()
4077                             : S.getEnsureUpperBound());
4078         // IV = LB;
4079         EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
4080                             ? S.getCombinedInit()
4081                             : S.getInit());
4082 
4083         const Expr *Cond =
4084             isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())
4085                 ? S.getCombinedCond()
4086                 : S.getCond();
4087 
4088         if (StaticChunked)
4089           Cond = S.getCombinedDistCond();
4090 
4091         // For static unchunked schedules generate:
4092         //
4093         //  1. For distribute alone, codegen
4094         //    while (idx <= UB) {
4095         //      BODY;
4096         //      ++idx;
4097         //    }
4098         //
4099         //  2. When combined with 'for' (e.g. as in 'distribute parallel for')
4100         //    while (idx <= UB) {
4101         //      <CodeGen rest of pragma>(LB, UB);
4102         //      idx += ST;
4103         //    }
4104         //
4105         // For static chunk one schedule generate:
4106         //
4107         // while (IV <= GlobalUB) {
4108         //   <CodeGen rest of pragma>(LB, UB);
4109         //   LB += ST;
4110         //   UB += ST;
4111         //   UB = min(UB, GlobalUB);
4112         //   IV = LB;
4113         // }
4114         //
4115         emitCommonSimdLoop(
4116             *this, S,
4117             [&S](CodeGenFunction &CGF, PrePostActionTy &) {
4118               if (isOpenMPSimdDirective(S.getDirectiveKind()))
4119                 CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true);
4120             },
4121             [&S, &LoopScope, Cond, IncExpr, LoopExit, &CodeGenLoop,
4122              StaticChunked](CodeGenFunction &CGF, PrePostActionTy &) {
4123               CGF.EmitOMPInnerLoop(
4124                   S, LoopScope.requiresCleanups(), Cond, IncExpr,
4125                   [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) {
4126                     CodeGenLoop(CGF, S, LoopExit);
4127                   },
4128                   [&S, StaticChunked](CodeGenFunction &CGF) {
4129                     if (StaticChunked) {
4130                       CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound());
4131                       CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound());
4132                       CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound());
4133                       CGF.EmitIgnoredExpr(S.getCombinedInit());
4134                     }
4135                   });
4136             });
4137         EmitBlock(LoopExit.getBlock());
4138         // Tell the runtime we are done.
4139         RT.emitForStaticFinish(*this, S.getEndLoc(), S.getDirectiveKind());
4140       } else {
4141         // Emit the outer loop, which requests its work chunk [LB..UB] from
4142         // runtime and runs the inner loop to process it.
4143         const OMPLoopArguments LoopArguments = {
4144             LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this),
4145             IL.getAddress(*this), Chunk};
4146         EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments,
4147                                    CodeGenLoop);
4148       }
4149       if (isOpenMPSimdDirective(S.getDirectiveKind())) {
4150         EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) {
4151           return CGF.Builder.CreateIsNotNull(
4152               CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
4153         });
4154       }
4155       if (isOpenMPSimdDirective(S.getDirectiveKind()) &&
4156           !isOpenMPParallelDirective(S.getDirectiveKind()) &&
4157           !isOpenMPTeamsDirective(S.getDirectiveKind())) {
4158         EmitOMPReductionClauseFinal(S, OMPD_simd);
4159         // Emit post-update of the reduction variables if IsLastIter != 0.
4160         emitPostUpdateForReductionClause(
4161             *this, S, [IL, &S](CodeGenFunction &CGF) {
4162               return CGF.Builder.CreateIsNotNull(
4163                   CGF.EmitLoadOfScalar(IL, S.getBeginLoc()));
4164             });
4165       }
4166       // Emit final copy of the lastprivate variables if IsLastIter != 0.
4167       if (HasLastprivateClause) {
4168         EmitOMPLastprivateClauseFinal(
4169             S, /*NoFinals=*/false,
4170             Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc())));
4171       }
4172     }
4173 
4174     // We're now done with the loop, so jump to the continuation block.
4175     if (ContBlock) {
4176       EmitBranch(ContBlock);
4177       EmitBlock(ContBlock, true);
4178     }
4179   }
4180 }
4181 
4182 void CodeGenFunction::EmitOMPDistributeDirective(
4183     const OMPDistributeDirective &S) {
4184   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
4185     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
4186   };
4187   OMPLexicalScope Scope(*this, S, OMPD_unknown);
4188   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen);
4189 }
4190 
4191 static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
4192                                                    const CapturedStmt *S,
4193                                                    SourceLocation Loc) {
4194   CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
4195   CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
4196   CGF.CapturedStmtInfo = &CapStmtInfo;
4197   llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S, Loc);
4198   Fn->setDoesNotRecurse();
4199   return Fn;
4200 }
4201 
4202 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
4203   if (S.hasClausesOfKind<OMPDependClause>()) {
4204     assert(!S.getAssociatedStmt() &&
4205            "No associated statement must be in ordered depend construct.");
4206     for (const auto *DC : S.getClausesOfKind<OMPDependClause>())
4207       CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC);
4208     return;
4209   }
4210   const auto *C = S.getSingleClause<OMPSIMDClause>();
4211   auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
4212                                  PrePostActionTy &Action) {
4213     const CapturedStmt *CS = S.getInnermostCapturedStmt();
4214     if (C) {
4215       llvm::SmallVector<llvm::Value *, 16> CapturedVars;
4216       CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
4217       llvm::Function *OutlinedFn =
4218           emitOutlinedOrderedFunction(CGM, CS, S.getBeginLoc());
4219       CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(),
4220                                                       OutlinedFn, CapturedVars);
4221     } else {
4222       Action.Enter(CGF);
4223       CGF.EmitStmt(CS->getCapturedStmt());
4224     }
4225   };
4226   OMPLexicalScope Scope(*this, S, OMPD_unknown);
4227   CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C);
4228 }
4229 
4230 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
4231                                          QualType SrcType, QualType DestType,
4232                                          SourceLocation Loc) {
4233   assert(CGF.hasScalarEvaluationKind(DestType) &&
4234          "DestType must have scalar evaluation kind.");
4235   assert(!Val.isAggregate() && "Must be a scalar or complex.");
4236   return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
4237                                                    DestType, Loc)
4238                         : CGF.EmitComplexToScalarConversion(
4239                               Val.getComplexVal(), SrcType, DestType, Loc);
4240 }
4241 
4242 static CodeGenFunction::ComplexPairTy
4243 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
4244                       QualType DestType, SourceLocation Loc) {
4245   assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
4246          "DestType must have complex evaluation kind.");
4247   CodeGenFunction::ComplexPairTy ComplexVal;
4248   if (Val.isScalar()) {
4249     // Convert the input element to the element type of the complex.
4250     QualType DestElementType =
4251         DestType->castAs<ComplexType>()->getElementType();
4252     llvm::Value *ScalarVal = CGF.EmitScalarConversion(
4253         Val.getScalarVal(), SrcType, DestElementType, Loc);
4254     ComplexVal = CodeGenFunction::ComplexPairTy(
4255         ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
4256   } else {
4257     assert(Val.isComplex() && "Must be a scalar or complex.");
4258     QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
4259     QualType DestElementType =
4260         DestType->castAs<ComplexType>()->getElementType();
4261     ComplexVal.first = CGF.EmitScalarConversion(
4262         Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
4263     ComplexVal.second = CGF.EmitScalarConversion(
4264         Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
4265   }
4266   return ComplexVal;
4267 }
4268 
4269 static void emitSimpleAtomicStore(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
4270                                   LValue LVal, RValue RVal) {
4271   if (LVal.isGlobalReg())
4272     CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
4273   else
4274     CGF.EmitAtomicStore(RVal, LVal, AO, LVal.isVolatile(), /*isInit=*/false);
4275 }
4276 
4277 static RValue emitSimpleAtomicLoad(CodeGenFunction &CGF,
4278                                    llvm::AtomicOrdering AO, LValue LVal,
4279                                    SourceLocation Loc) {
4280   if (LVal.isGlobalReg())
4281     return CGF.EmitLoadOfLValue(LVal, Loc);
4282   return CGF.EmitAtomicLoad(
4283       LVal, Loc, llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO),
4284       LVal.isVolatile());
4285 }
4286 
4287 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
4288                                          QualType RValTy, SourceLocation Loc) {
4289   switch (getEvaluationKind(LVal.getType())) {
4290   case TEK_Scalar:
4291     EmitStoreThroughLValue(RValue::get(convertToScalarValue(
4292                                *this, RVal, RValTy, LVal.getType(), Loc)),
4293                            LVal);
4294     break;
4295   case TEK_Complex:
4296     EmitStoreOfComplex(
4297         convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
4298         /*isInit=*/false);
4299     break;
4300   case TEK_Aggregate:
4301     llvm_unreachable("Must be a scalar or complex.");
4302   }
4303 }
4304 
4305 static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO,
4306                                   const Expr *X, const Expr *V,
4307                                   SourceLocation Loc) {
4308   // v = x;
4309   assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
4310   assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
4311   LValue XLValue = CGF.EmitLValue(X);
4312   LValue VLValue = CGF.EmitLValue(V);
4313   RValue Res = emitSimpleAtomicLoad(CGF, AO, XLValue, Loc);
4314   // OpenMP, 2.17.7, atomic Construct
4315   // If the read or capture clause is specified and the acquire, acq_rel, or
4316   // seq_cst clause is specified then the strong flush on exit from the atomic
4317   // operation is also an acquire flush.
4318   switch (AO) {
4319   case llvm::AtomicOrdering::Acquire:
4320   case llvm::AtomicOrdering::AcquireRelease:
4321   case llvm::AtomicOrdering::SequentiallyConsistent:
4322     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4323                                          llvm::AtomicOrdering::Acquire);
4324     break;
4325   case llvm::AtomicOrdering::Monotonic:
4326   case llvm::AtomicOrdering::Release:
4327     break;
4328   case llvm::AtomicOrdering::NotAtomic:
4329   case llvm::AtomicOrdering::Unordered:
4330     llvm_unreachable("Unexpected ordering.");
4331   }
4332   CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
4333   CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V);
4334 }
4335 
4336 static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF,
4337                                    llvm::AtomicOrdering AO, const Expr *X,
4338                                    const Expr *E, SourceLocation Loc) {
4339   // x = expr;
4340   assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
4341   emitSimpleAtomicStore(CGF, AO, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
4342   CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X);
4343   // OpenMP, 2.17.7, atomic Construct
4344   // If the write, update, or capture clause is specified and the release,
4345   // acq_rel, or seq_cst clause is specified then the strong flush on entry to
4346   // the atomic operation is also a release flush.
4347   switch (AO) {
4348   case llvm::AtomicOrdering::Release:
4349   case llvm::AtomicOrdering::AcquireRelease:
4350   case llvm::AtomicOrdering::SequentiallyConsistent:
4351     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4352                                          llvm::AtomicOrdering::Release);
4353     break;
4354   case llvm::AtomicOrdering::Acquire:
4355   case llvm::AtomicOrdering::Monotonic:
4356     break;
4357   case llvm::AtomicOrdering::NotAtomic:
4358   case llvm::AtomicOrdering::Unordered:
4359     llvm_unreachable("Unexpected ordering.");
4360   }
4361 }
4362 
4363 static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
4364                                                 RValue Update,
4365                                                 BinaryOperatorKind BO,
4366                                                 llvm::AtomicOrdering AO,
4367                                                 bool IsXLHSInRHSPart) {
4368   ASTContext &Context = CGF.getContext();
4369   // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
4370   // expression is simple and atomic is allowed for the given type for the
4371   // target platform.
4372   if (BO == BO_Comma || !Update.isScalar() ||
4373       !Update.getScalarVal()->getType()->isIntegerTy() || !X.isSimple() ||
4374       (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
4375        (Update.getScalarVal()->getType() !=
4376         X.getAddress(CGF).getElementType())) ||
4377       !X.getAddress(CGF).getElementType()->isIntegerTy() ||
4378       !Context.getTargetInfo().hasBuiltinAtomic(
4379           Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
4380     return std::make_pair(false, RValue::get(nullptr));
4381 
4382   llvm::AtomicRMWInst::BinOp RMWOp;
4383   switch (BO) {
4384   case BO_Add:
4385     RMWOp = llvm::AtomicRMWInst::Add;
4386     break;
4387   case BO_Sub:
4388     if (!IsXLHSInRHSPart)
4389       return std::make_pair(false, RValue::get(nullptr));
4390     RMWOp = llvm::AtomicRMWInst::Sub;
4391     break;
4392   case BO_And:
4393     RMWOp = llvm::AtomicRMWInst::And;
4394     break;
4395   case BO_Or:
4396     RMWOp = llvm::AtomicRMWInst::Or;
4397     break;
4398   case BO_Xor:
4399     RMWOp = llvm::AtomicRMWInst::Xor;
4400     break;
4401   case BO_LT:
4402     RMWOp = X.getType()->hasSignedIntegerRepresentation()
4403                 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
4404                                    : llvm::AtomicRMWInst::Max)
4405                 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
4406                                    : llvm::AtomicRMWInst::UMax);
4407     break;
4408   case BO_GT:
4409     RMWOp = X.getType()->hasSignedIntegerRepresentation()
4410                 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
4411                                    : llvm::AtomicRMWInst::Min)
4412                 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
4413                                    : llvm::AtomicRMWInst::UMin);
4414     break;
4415   case BO_Assign:
4416     RMWOp = llvm::AtomicRMWInst::Xchg;
4417     break;
4418   case BO_Mul:
4419   case BO_Div:
4420   case BO_Rem:
4421   case BO_Shl:
4422   case BO_Shr:
4423   case BO_LAnd:
4424   case BO_LOr:
4425     return std::make_pair(false, RValue::get(nullptr));
4426   case BO_PtrMemD:
4427   case BO_PtrMemI:
4428   case BO_LE:
4429   case BO_GE:
4430   case BO_EQ:
4431   case BO_NE:
4432   case BO_Cmp:
4433   case BO_AddAssign:
4434   case BO_SubAssign:
4435   case BO_AndAssign:
4436   case BO_OrAssign:
4437   case BO_XorAssign:
4438   case BO_MulAssign:
4439   case BO_DivAssign:
4440   case BO_RemAssign:
4441   case BO_ShlAssign:
4442   case BO_ShrAssign:
4443   case BO_Comma:
4444     llvm_unreachable("Unsupported atomic update operation");
4445   }
4446   llvm::Value *UpdateVal = Update.getScalarVal();
4447   if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
4448     UpdateVal = CGF.Builder.CreateIntCast(
4449         IC, X.getAddress(CGF).getElementType(),
4450         X.getType()->hasSignedIntegerRepresentation());
4451   }
4452   llvm::Value *Res =
4453       CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(CGF), UpdateVal, AO);
4454   return std::make_pair(true, RValue::get(Res));
4455 }
4456 
4457 std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
4458     LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
4459     llvm::AtomicOrdering AO, SourceLocation Loc,
4460     const llvm::function_ref<RValue(RValue)> CommonGen) {
4461   // Update expressions are allowed to have the following forms:
4462   // x binop= expr; -> xrval + expr;
4463   // x++, ++x -> xrval + 1;
4464   // x--, --x -> xrval - 1;
4465   // x = x binop expr; -> xrval binop expr
4466   // x = expr Op x; - > expr binop xrval;
4467   auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
4468   if (!Res.first) {
4469     if (X.isGlobalReg()) {
4470       // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
4471       // 'xrval'.
4472       EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
4473     } else {
4474       // Perform compare-and-swap procedure.
4475       EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
4476     }
4477   }
4478   return Res;
4479 }
4480 
4481 static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF,
4482                                     llvm::AtomicOrdering AO, const Expr *X,
4483                                     const Expr *E, const Expr *UE,
4484                                     bool IsXLHSInRHSPart, SourceLocation Loc) {
4485   assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
4486          "Update expr in 'atomic update' must be a binary operator.");
4487   const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
4488   // Update expressions are allowed to have the following forms:
4489   // x binop= expr; -> xrval + expr;
4490   // x++, ++x -> xrval + 1;
4491   // x--, --x -> xrval - 1;
4492   // x = x binop expr; -> xrval binop expr
4493   // x = expr Op x; - > expr binop xrval;
4494   assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
4495   LValue XLValue = CGF.EmitLValue(X);
4496   RValue ExprRValue = CGF.EmitAnyExpr(E);
4497   const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
4498   const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
4499   const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
4500   const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
4501   auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) {
4502     CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
4503     CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
4504     return CGF.EmitAnyExpr(UE);
4505   };
4506   (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
4507       XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
4508   CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X);
4509   // OpenMP, 2.17.7, atomic Construct
4510   // If the write, update, or capture clause is specified and the release,
4511   // acq_rel, or seq_cst clause is specified then the strong flush on entry to
4512   // the atomic operation is also a release flush.
4513   switch (AO) {
4514   case llvm::AtomicOrdering::Release:
4515   case llvm::AtomicOrdering::AcquireRelease:
4516   case llvm::AtomicOrdering::SequentiallyConsistent:
4517     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4518                                          llvm::AtomicOrdering::Release);
4519     break;
4520   case llvm::AtomicOrdering::Acquire:
4521   case llvm::AtomicOrdering::Monotonic:
4522     break;
4523   case llvm::AtomicOrdering::NotAtomic:
4524   case llvm::AtomicOrdering::Unordered:
4525     llvm_unreachable("Unexpected ordering.");
4526   }
4527 }
4528 
4529 static RValue convertToType(CodeGenFunction &CGF, RValue Value,
4530                             QualType SourceType, QualType ResType,
4531                             SourceLocation Loc) {
4532   switch (CGF.getEvaluationKind(ResType)) {
4533   case TEK_Scalar:
4534     return RValue::get(
4535         convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
4536   case TEK_Complex: {
4537     auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
4538     return RValue::getComplex(Res.first, Res.second);
4539   }
4540   case TEK_Aggregate:
4541     break;
4542   }
4543   llvm_unreachable("Must be a scalar or complex.");
4544 }
4545 
4546 static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF,
4547                                      llvm::AtomicOrdering AO,
4548                                      bool IsPostfixUpdate, const Expr *V,
4549                                      const Expr *X, const Expr *E,
4550                                      const Expr *UE, bool IsXLHSInRHSPart,
4551                                      SourceLocation Loc) {
4552   assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
4553   assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
4554   RValue NewVVal;
4555   LValue VLValue = CGF.EmitLValue(V);
4556   LValue XLValue = CGF.EmitLValue(X);
4557   RValue ExprRValue = CGF.EmitAnyExpr(E);
4558   QualType NewVValType;
4559   if (UE) {
4560     // 'x' is updated with some additional value.
4561     assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
4562            "Update expr in 'atomic capture' must be a binary operator.");
4563     const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
4564     // Update expressions are allowed to have the following forms:
4565     // x binop= expr; -> xrval + expr;
4566     // x++, ++x -> xrval + 1;
4567     // x--, --x -> xrval - 1;
4568     // x = x binop expr; -> xrval binop expr
4569     // x = expr Op x; - > expr binop xrval;
4570     const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
4571     const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
4572     const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
4573     NewVValType = XRValExpr->getType();
4574     const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
4575     auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
4576                   IsPostfixUpdate](RValue XRValue) {
4577       CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
4578       CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
4579       RValue Res = CGF.EmitAnyExpr(UE);
4580       NewVVal = IsPostfixUpdate ? XRValue : Res;
4581       return Res;
4582     };
4583     auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
4584         XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
4585     CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X);
4586     if (Res.first) {
4587       // 'atomicrmw' instruction was generated.
4588       if (IsPostfixUpdate) {
4589         // Use old value from 'atomicrmw'.
4590         NewVVal = Res.second;
4591       } else {
4592         // 'atomicrmw' does not provide new value, so evaluate it using old
4593         // value of 'x'.
4594         CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
4595         CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
4596         NewVVal = CGF.EmitAnyExpr(UE);
4597       }
4598     }
4599   } else {
4600     // 'x' is simply rewritten with some 'expr'.
4601     NewVValType = X->getType().getNonReferenceType();
4602     ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
4603                                X->getType().getNonReferenceType(), Loc);
4604     auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) {
4605       NewVVal = XRValue;
4606       return ExprRValue;
4607     };
4608     // Try to perform atomicrmw xchg, otherwise simple exchange.
4609     auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
4610         XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
4611         Loc, Gen);
4612     CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X);
4613     if (Res.first) {
4614       // 'atomicrmw' instruction was generated.
4615       NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
4616     }
4617   }
4618   // Emit post-update store to 'v' of old/new 'x' value.
4619   CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
4620   CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V);
4621   // OpenMP, 2.17.7, atomic Construct
4622   // If the write, update, or capture clause is specified and the release,
4623   // acq_rel, or seq_cst clause is specified then the strong flush on entry to
4624   // the atomic operation is also a release flush.
4625   // If the read or capture clause is specified and the acquire, acq_rel, or
4626   // seq_cst clause is specified then the strong flush on exit from the atomic
4627   // operation is also an acquire flush.
4628   switch (AO) {
4629   case llvm::AtomicOrdering::Release:
4630     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4631                                          llvm::AtomicOrdering::Release);
4632     break;
4633   case llvm::AtomicOrdering::Acquire:
4634     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4635                                          llvm::AtomicOrdering::Acquire);
4636     break;
4637   case llvm::AtomicOrdering::AcquireRelease:
4638   case llvm::AtomicOrdering::SequentiallyConsistent:
4639     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc,
4640                                          llvm::AtomicOrdering::AcquireRelease);
4641     break;
4642   case llvm::AtomicOrdering::Monotonic:
4643     break;
4644   case llvm::AtomicOrdering::NotAtomic:
4645   case llvm::AtomicOrdering::Unordered:
4646     llvm_unreachable("Unexpected ordering.");
4647   }
4648 }
4649 
4650 static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
4651                               llvm::AtomicOrdering AO, bool IsPostfixUpdate,
4652                               const Expr *X, const Expr *V, const Expr *E,
4653                               const Expr *UE, bool IsXLHSInRHSPart,
4654                               SourceLocation Loc) {
4655   switch (Kind) {
4656   case OMPC_read:
4657     emitOMPAtomicReadExpr(CGF, AO, X, V, Loc);
4658     break;
4659   case OMPC_write:
4660     emitOMPAtomicWriteExpr(CGF, AO, X, E, Loc);
4661     break;
4662   case OMPC_unknown:
4663   case OMPC_update:
4664     emitOMPAtomicUpdateExpr(CGF, AO, X, E, UE, IsXLHSInRHSPart, Loc);
4665     break;
4666   case OMPC_capture:
4667     emitOMPAtomicCaptureExpr(CGF, AO, IsPostfixUpdate, V, X, E, UE,
4668                              IsXLHSInRHSPart, Loc);
4669     break;
4670   case OMPC_if:
4671   case OMPC_final:
4672   case OMPC_num_threads:
4673   case OMPC_private:
4674   case OMPC_firstprivate:
4675   case OMPC_lastprivate:
4676   case OMPC_reduction:
4677   case OMPC_task_reduction:
4678   case OMPC_in_reduction:
4679   case OMPC_safelen:
4680   case OMPC_simdlen:
4681   case OMPC_allocator:
4682   case OMPC_allocate:
4683   case OMPC_collapse:
4684   case OMPC_default:
4685   case OMPC_seq_cst:
4686   case OMPC_acq_rel:
4687   case OMPC_acquire:
4688   case OMPC_release:
4689   case OMPC_relaxed:
4690   case OMPC_shared:
4691   case OMPC_linear:
4692   case OMPC_aligned:
4693   case OMPC_copyin:
4694   case OMPC_copyprivate:
4695   case OMPC_flush:
4696   case OMPC_depobj:
4697   case OMPC_proc_bind:
4698   case OMPC_schedule:
4699   case OMPC_ordered:
4700   case OMPC_nowait:
4701   case OMPC_untied:
4702   case OMPC_threadprivate:
4703   case OMPC_depend:
4704   case OMPC_mergeable:
4705   case OMPC_device:
4706   case OMPC_threads:
4707   case OMPC_simd:
4708   case OMPC_map:
4709   case OMPC_num_teams:
4710   case OMPC_thread_limit:
4711   case OMPC_priority:
4712   case OMPC_grainsize:
4713   case OMPC_nogroup:
4714   case OMPC_num_tasks:
4715   case OMPC_hint:
4716   case OMPC_dist_schedule:
4717   case OMPC_defaultmap:
4718   case OMPC_uniform:
4719   case OMPC_to:
4720   case OMPC_from:
4721   case OMPC_use_device_ptr:
4722   case OMPC_is_device_ptr:
4723   case OMPC_unified_address:
4724   case OMPC_unified_shared_memory:
4725   case OMPC_reverse_offload:
4726   case OMPC_dynamic_allocators:
4727   case OMPC_atomic_default_mem_order:
4728   case OMPC_device_type:
4729   case OMPC_match:
4730   case OMPC_nontemporal:
4731   case OMPC_order:
4732   case OMPC_destroy:
4733   case OMPC_detach:
4734   case OMPC_inclusive:
4735   case OMPC_exclusive:
4736   case OMPC_uses_allocators:
4737   case OMPC_affinity:
4738     llvm_unreachable("Clause is not allowed in 'omp atomic'.");
4739   }
4740 }
4741 
4742 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
4743   llvm::AtomicOrdering AO = llvm::AtomicOrdering::Monotonic;
4744   bool MemOrderingSpecified = false;
4745   if (S.getSingleClause<OMPSeqCstClause>()) {
4746     AO = llvm::AtomicOrdering::SequentiallyConsistent;
4747     MemOrderingSpecified = true;
4748   } else if (S.getSingleClause<OMPAcqRelClause>()) {
4749     AO = llvm::AtomicOrdering::AcquireRelease;
4750     MemOrderingSpecified = true;
4751   } else if (S.getSingleClause<OMPAcquireClause>()) {
4752     AO = llvm::AtomicOrdering::Acquire;
4753     MemOrderingSpecified = true;
4754   } else if (S.getSingleClause<OMPReleaseClause>()) {
4755     AO = llvm::AtomicOrdering::Release;
4756     MemOrderingSpecified = true;
4757   } else if (S.getSingleClause<OMPRelaxedClause>()) {
4758     AO = llvm::AtomicOrdering::Monotonic;
4759     MemOrderingSpecified = true;
4760   }
4761   OpenMPClauseKind Kind = OMPC_unknown;
4762   for (const OMPClause *C : S.clauses()) {
4763     // Find first clause (skip seq_cst|acq_rel|aqcuire|release|relaxed clause,
4764     // if it is first).
4765     if (C->getClauseKind() != OMPC_seq_cst &&
4766         C->getClauseKind() != OMPC_acq_rel &&
4767         C->getClauseKind() != OMPC_acquire &&
4768         C->getClauseKind() != OMPC_release &&
4769         C->getClauseKind() != OMPC_relaxed) {
4770       Kind = C->getClauseKind();
4771       break;
4772     }
4773   }
4774   if (!MemOrderingSpecified) {
4775     llvm::AtomicOrdering DefaultOrder =
4776         CGM.getOpenMPRuntime().getDefaultMemoryOrdering();
4777     if (DefaultOrder == llvm::AtomicOrdering::Monotonic ||
4778         DefaultOrder == llvm::AtomicOrdering::SequentiallyConsistent ||
4779         (DefaultOrder == llvm::AtomicOrdering::AcquireRelease &&
4780          Kind == OMPC_capture)) {
4781       AO = DefaultOrder;
4782     } else if (DefaultOrder == llvm::AtomicOrdering::AcquireRelease) {
4783       if (Kind == OMPC_unknown || Kind == OMPC_update || Kind == OMPC_write) {
4784         AO = llvm::AtomicOrdering::Release;
4785       } else if (Kind == OMPC_read) {
4786         assert(Kind == OMPC_read && "Unexpected atomic kind.");
4787         AO = llvm::AtomicOrdering::Acquire;
4788       }
4789     }
4790   }
4791 
4792   const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers();
4793   if (const auto *FE = dyn_cast<FullExpr>(CS))
4794     enterFullExpression(FE);
4795   // Processing for statements under 'atomic capture'.
4796   if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
4797     for (const Stmt *C : Compound->body()) {
4798       if (const auto *FE = dyn_cast<FullExpr>(C))
4799         enterFullExpression(FE);
4800     }
4801   }
4802 
4803   auto &&CodeGen = [&S, Kind, AO, CS](CodeGenFunction &CGF,
4804                                             PrePostActionTy &) {
4805     CGF.EmitStopPoint(CS);
4806     emitOMPAtomicExpr(CGF, Kind, AO, S.isPostfixUpdate(), S.getX(), S.getV(),
4807                       S.getExpr(), S.getUpdateExpr(), S.isXLHSInRHSPart(),
4808                       S.getBeginLoc());
4809   };
4810   OMPLexicalScope Scope(*this, S, OMPD_unknown);
4811   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
4812 }
4813 
4814 static void emitCommonOMPTargetDirective(CodeGenFunction &CGF,
4815                                          const OMPExecutableDirective &S,
4816                                          const RegionCodeGenTy &CodeGen) {
4817   assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind()));
4818   CodeGenModule &CGM = CGF.CGM;
4819 
4820   // On device emit this construct as inlined code.
4821   if (CGM.getLangOpts().OpenMPIsDevice) {
4822     OMPLexicalScope Scope(CGF, S, OMPD_target);
4823     CGM.getOpenMPRuntime().emitInlinedDirective(
4824         CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
4825           CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
4826         });
4827     return;
4828   }
4829 
4830   auto LPCRegion =
4831       CGOpenMPRuntime::LastprivateConditionalRAII::disable(CGF, S);
4832   llvm::Function *Fn = nullptr;
4833   llvm::Constant *FnID = nullptr;
4834 
4835   const Expr *IfCond = nullptr;
4836   // Check for the at most one if clause associated with the target region.
4837   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
4838     if (C->getNameModifier() == OMPD_unknown ||
4839         C->getNameModifier() == OMPD_target) {
4840       IfCond = C->getCondition();
4841       break;
4842     }
4843   }
4844 
4845   // Check if we have any device clause associated with the directive.
4846   llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device(
4847       nullptr, OMPC_DEVICE_unknown);
4848   if (auto *C = S.getSingleClause<OMPDeviceClause>())
4849     Device.setPointerAndInt(C->getDevice(), C->getModifier());
4850 
4851   // Check if we have an if clause whose conditional always evaluates to false
4852   // or if we do not have any targets specified. If so the target region is not
4853   // an offload entry point.
4854   bool IsOffloadEntry = true;
4855   if (IfCond) {
4856     bool Val;
4857     if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
4858       IsOffloadEntry = false;
4859   }
4860   if (CGM.getLangOpts().OMPTargetTriples.empty())
4861     IsOffloadEntry = false;
4862 
4863   assert(CGF.CurFuncDecl && "No parent declaration for target region!");
4864   StringRef ParentName;
4865   // In case we have Ctors/Dtors we use the complete type variant to produce
4866   // the mangling of the device outlined kernel.
4867   if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl))
4868     ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
4869   else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl))
4870     ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
4871   else
4872     ParentName =
4873         CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl)));
4874 
4875   // Emit target region as a standalone region.
4876   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID,
4877                                                     IsOffloadEntry, CodeGen);
4878   OMPLexicalScope Scope(CGF, S, OMPD_task);
4879   auto &&SizeEmitter =
4880       [IsOffloadEntry](CodeGenFunction &CGF,
4881                        const OMPLoopDirective &D) -> llvm::Value * {
4882     if (IsOffloadEntry) {
4883       OMPLoopScope(CGF, D);
4884       // Emit calculation of the iterations count.
4885       llvm::Value *NumIterations = CGF.EmitScalarExpr(D.getNumIterations());
4886       NumIterations = CGF.Builder.CreateIntCast(NumIterations, CGF.Int64Ty,
4887                                                 /*isSigned=*/false);
4888       return NumIterations;
4889     }
4890     return nullptr;
4891   };
4892   CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device,
4893                                         SizeEmitter);
4894 }
4895 
4896 static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S,
4897                              PrePostActionTy &Action) {
4898   Action.Enter(CGF);
4899   CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4900   (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4901   CGF.EmitOMPPrivateClause(S, PrivateScope);
4902   (void)PrivateScope.Privatize();
4903   if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
4904     CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S);
4905 
4906   CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt());
4907 }
4908 
4909 void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM,
4910                                                   StringRef ParentName,
4911                                                   const OMPTargetDirective &S) {
4912   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4913     emitTargetRegion(CGF, S, Action);
4914   };
4915   llvm::Function *Fn;
4916   llvm::Constant *Addr;
4917   // Emit target region as a standalone region.
4918   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
4919       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
4920   assert(Fn && Addr && "Target device function emission failed.");
4921 }
4922 
4923 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
4924   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4925     emitTargetRegion(CGF, S, Action);
4926   };
4927   emitCommonOMPTargetDirective(*this, S, CodeGen);
4928 }
4929 
4930 static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
4931                                         const OMPExecutableDirective &S,
4932                                         OpenMPDirectiveKind InnermostKind,
4933                                         const RegionCodeGenTy &CodeGen) {
4934   const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams);
4935   llvm::Function *OutlinedFn =
4936       CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction(
4937           S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
4938 
4939   const auto *NT = S.getSingleClause<OMPNumTeamsClause>();
4940   const auto *TL = S.getSingleClause<OMPThreadLimitClause>();
4941   if (NT || TL) {
4942     const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr;
4943     const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr;
4944 
4945     CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
4946                                                   S.getBeginLoc());
4947   }
4948 
4949   OMPTeamsScope Scope(CGF, S);
4950   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
4951   CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
4952   CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn,
4953                                            CapturedVars);
4954 }
4955 
4956 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
4957   // Emit teams region as a standalone region.
4958   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4959     Action.Enter(CGF);
4960     OMPPrivateScope PrivateScope(CGF);
4961     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4962     CGF.EmitOMPPrivateClause(S, PrivateScope);
4963     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
4964     (void)PrivateScope.Privatize();
4965     CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt());
4966     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
4967   };
4968   emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen);
4969   emitPostUpdateForReductionClause(*this, S,
4970                                    [](CodeGenFunction &) { return nullptr; });
4971 }
4972 
4973 static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action,
4974                                   const OMPTargetTeamsDirective &S) {
4975   auto *CS = S.getCapturedStmt(OMPD_teams);
4976   Action.Enter(CGF);
4977   // Emit teams region as a standalone region.
4978   auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) {
4979     Action.Enter(CGF);
4980     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
4981     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
4982     CGF.EmitOMPPrivateClause(S, PrivateScope);
4983     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
4984     (void)PrivateScope.Privatize();
4985     if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
4986       CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S);
4987     CGF.EmitStmt(CS->getCapturedStmt());
4988     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
4989   };
4990   emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen);
4991   emitPostUpdateForReductionClause(CGF, S,
4992                                    [](CodeGenFunction &) { return nullptr; });
4993 }
4994 
4995 void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction(
4996     CodeGenModule &CGM, StringRef ParentName,
4997     const OMPTargetTeamsDirective &S) {
4998   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
4999     emitTargetTeamsRegion(CGF, Action, S);
5000   };
5001   llvm::Function *Fn;
5002   llvm::Constant *Addr;
5003   // Emit target region as a standalone region.
5004   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5005       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5006   assert(Fn && Addr && "Target device function emission failed.");
5007 }
5008 
5009 void CodeGenFunction::EmitOMPTargetTeamsDirective(
5010     const OMPTargetTeamsDirective &S) {
5011   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5012     emitTargetTeamsRegion(CGF, Action, S);
5013   };
5014   emitCommonOMPTargetDirective(*this, S, CodeGen);
5015 }
5016 
5017 static void
5018 emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action,
5019                                 const OMPTargetTeamsDistributeDirective &S) {
5020   Action.Enter(CGF);
5021   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5022     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
5023   };
5024 
5025   // Emit teams region as a standalone region.
5026   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5027                                             PrePostActionTy &Action) {
5028     Action.Enter(CGF);
5029     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5030     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5031     (void)PrivateScope.Privatize();
5032     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute,
5033                                                     CodeGenDistribute);
5034     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5035   };
5036   emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen);
5037   emitPostUpdateForReductionClause(CGF, S,
5038                                    [](CodeGenFunction &) { return nullptr; });
5039 }
5040 
5041 void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction(
5042     CodeGenModule &CGM, StringRef ParentName,
5043     const OMPTargetTeamsDistributeDirective &S) {
5044   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5045     emitTargetTeamsDistributeRegion(CGF, Action, S);
5046   };
5047   llvm::Function *Fn;
5048   llvm::Constant *Addr;
5049   // Emit target region as a standalone region.
5050   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5051       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5052   assert(Fn && Addr && "Target device function emission failed.");
5053 }
5054 
5055 void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective(
5056     const OMPTargetTeamsDistributeDirective &S) {
5057   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5058     emitTargetTeamsDistributeRegion(CGF, Action, S);
5059   };
5060   emitCommonOMPTargetDirective(*this, S, CodeGen);
5061 }
5062 
5063 static void emitTargetTeamsDistributeSimdRegion(
5064     CodeGenFunction &CGF, PrePostActionTy &Action,
5065     const OMPTargetTeamsDistributeSimdDirective &S) {
5066   Action.Enter(CGF);
5067   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5068     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
5069   };
5070 
5071   // Emit teams region as a standalone region.
5072   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5073                                             PrePostActionTy &Action) {
5074     Action.Enter(CGF);
5075     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5076     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5077     (void)PrivateScope.Privatize();
5078     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute,
5079                                                     CodeGenDistribute);
5080     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5081   };
5082   emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen);
5083   emitPostUpdateForReductionClause(CGF, S,
5084                                    [](CodeGenFunction &) { return nullptr; });
5085 }
5086 
5087 void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction(
5088     CodeGenModule &CGM, StringRef ParentName,
5089     const OMPTargetTeamsDistributeSimdDirective &S) {
5090   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5091     emitTargetTeamsDistributeSimdRegion(CGF, Action, S);
5092   };
5093   llvm::Function *Fn;
5094   llvm::Constant *Addr;
5095   // Emit target region as a standalone region.
5096   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5097       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5098   assert(Fn && Addr && "Target device function emission failed.");
5099 }
5100 
5101 void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective(
5102     const OMPTargetTeamsDistributeSimdDirective &S) {
5103   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5104     emitTargetTeamsDistributeSimdRegion(CGF, Action, S);
5105   };
5106   emitCommonOMPTargetDirective(*this, S, CodeGen);
5107 }
5108 
5109 void CodeGenFunction::EmitOMPTeamsDistributeDirective(
5110     const OMPTeamsDistributeDirective &S) {
5111 
5112   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5113     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
5114   };
5115 
5116   // Emit teams region as a standalone region.
5117   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5118                                             PrePostActionTy &Action) {
5119     Action.Enter(CGF);
5120     OMPPrivateScope PrivateScope(CGF);
5121     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5122     (void)PrivateScope.Privatize();
5123     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute,
5124                                                     CodeGenDistribute);
5125     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5126   };
5127   emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen);
5128   emitPostUpdateForReductionClause(*this, S,
5129                                    [](CodeGenFunction &) { return nullptr; });
5130 }
5131 
5132 void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective(
5133     const OMPTeamsDistributeSimdDirective &S) {
5134   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5135     CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc());
5136   };
5137 
5138   // Emit teams region as a standalone region.
5139   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5140                                             PrePostActionTy &Action) {
5141     Action.Enter(CGF);
5142     OMPPrivateScope PrivateScope(CGF);
5143     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5144     (void)PrivateScope.Privatize();
5145     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd,
5146                                                     CodeGenDistribute);
5147     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5148   };
5149   emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen);
5150   emitPostUpdateForReductionClause(*this, S,
5151                                    [](CodeGenFunction &) { return nullptr; });
5152 }
5153 
5154 void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective(
5155     const OMPTeamsDistributeParallelForDirective &S) {
5156   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5157     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
5158                               S.getDistInc());
5159   };
5160 
5161   // Emit teams region as a standalone region.
5162   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5163                                             PrePostActionTy &Action) {
5164     Action.Enter(CGF);
5165     OMPPrivateScope PrivateScope(CGF);
5166     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5167     (void)PrivateScope.Privatize();
5168     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute,
5169                                                     CodeGenDistribute);
5170     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5171   };
5172   emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen);
5173   emitPostUpdateForReductionClause(*this, S,
5174                                    [](CodeGenFunction &) { return nullptr; });
5175 }
5176 
5177 void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective(
5178     const OMPTeamsDistributeParallelForSimdDirective &S) {
5179   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5180     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
5181                               S.getDistInc());
5182   };
5183 
5184   // Emit teams region as a standalone region.
5185   auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5186                                             PrePostActionTy &Action) {
5187     Action.Enter(CGF);
5188     OMPPrivateScope PrivateScope(CGF);
5189     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5190     (void)PrivateScope.Privatize();
5191     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(
5192         CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false);
5193     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5194   };
5195   emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for_simd,
5196                               CodeGen);
5197   emitPostUpdateForReductionClause(*this, S,
5198                                    [](CodeGenFunction &) { return nullptr; });
5199 }
5200 
5201 static void emitTargetTeamsDistributeParallelForRegion(
5202     CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S,
5203     PrePostActionTy &Action) {
5204   Action.Enter(CGF);
5205   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5206     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
5207                               S.getDistInc());
5208   };
5209 
5210   // Emit teams region as a standalone region.
5211   auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5212                                                  PrePostActionTy &Action) {
5213     Action.Enter(CGF);
5214     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5215     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5216     (void)PrivateScope.Privatize();
5217     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(
5218         CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false);
5219     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5220   };
5221 
5222   emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for,
5223                               CodeGenTeams);
5224   emitPostUpdateForReductionClause(CGF, S,
5225                                    [](CodeGenFunction &) { return nullptr; });
5226 }
5227 
5228 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction(
5229     CodeGenModule &CGM, StringRef ParentName,
5230     const OMPTargetTeamsDistributeParallelForDirective &S) {
5231   // Emit SPMD target teams distribute parallel for region as a standalone
5232   // region.
5233   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5234     emitTargetTeamsDistributeParallelForRegion(CGF, S, Action);
5235   };
5236   llvm::Function *Fn;
5237   llvm::Constant *Addr;
5238   // Emit target region as a standalone region.
5239   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5240       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5241   assert(Fn && Addr && "Target device function emission failed.");
5242 }
5243 
5244 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective(
5245     const OMPTargetTeamsDistributeParallelForDirective &S) {
5246   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5247     emitTargetTeamsDistributeParallelForRegion(CGF, S, Action);
5248   };
5249   emitCommonOMPTargetDirective(*this, S, CodeGen);
5250 }
5251 
5252 static void emitTargetTeamsDistributeParallelForSimdRegion(
5253     CodeGenFunction &CGF,
5254     const OMPTargetTeamsDistributeParallelForSimdDirective &S,
5255     PrePostActionTy &Action) {
5256   Action.Enter(CGF);
5257   auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5258     CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined,
5259                               S.getDistInc());
5260   };
5261 
5262   // Emit teams region as a standalone region.
5263   auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF,
5264                                                  PrePostActionTy &Action) {
5265     Action.Enter(CGF);
5266     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5267     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5268     (void)PrivateScope.Privatize();
5269     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(
5270         CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false);
5271     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams);
5272   };
5273 
5274   emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd,
5275                               CodeGenTeams);
5276   emitPostUpdateForReductionClause(CGF, S,
5277                                    [](CodeGenFunction &) { return nullptr; });
5278 }
5279 
5280 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction(
5281     CodeGenModule &CGM, StringRef ParentName,
5282     const OMPTargetTeamsDistributeParallelForSimdDirective &S) {
5283   // Emit SPMD target teams distribute parallel for simd region as a standalone
5284   // region.
5285   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5286     emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action);
5287   };
5288   llvm::Function *Fn;
5289   llvm::Constant *Addr;
5290   // Emit target region as a standalone region.
5291   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5292       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5293   assert(Fn && Addr && "Target device function emission failed.");
5294 }
5295 
5296 void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective(
5297     const OMPTargetTeamsDistributeParallelForSimdDirective &S) {
5298   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5299     emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action);
5300   };
5301   emitCommonOMPTargetDirective(*this, S, CodeGen);
5302 }
5303 
5304 void CodeGenFunction::EmitOMPCancellationPointDirective(
5305     const OMPCancellationPointDirective &S) {
5306   CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(),
5307                                                    S.getCancelRegion());
5308 }
5309 
5310 void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
5311   const Expr *IfCond = nullptr;
5312   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
5313     if (C->getNameModifier() == OMPD_unknown ||
5314         C->getNameModifier() == OMPD_cancel) {
5315       IfCond = C->getCondition();
5316       break;
5317     }
5318   }
5319   if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) {
5320     // TODO: This check is necessary as we only generate `omp parallel` through
5321     // the OpenMPIRBuilder for now.
5322     if (S.getCancelRegion() == OMPD_parallel) {
5323       llvm::Value *IfCondition = nullptr;
5324       if (IfCond)
5325         IfCondition = EmitScalarExpr(IfCond,
5326                                      /*IgnoreResultAssign=*/true);
5327       return Builder.restoreIP(
5328           OMPBuilder->CreateCancel(Builder, IfCondition, S.getCancelRegion()));
5329     }
5330   }
5331 
5332   CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond,
5333                                         S.getCancelRegion());
5334 }
5335 
5336 CodeGenFunction::JumpDest
5337 CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
5338   if (Kind == OMPD_parallel || Kind == OMPD_task ||
5339       Kind == OMPD_target_parallel || Kind == OMPD_taskloop ||
5340       Kind == OMPD_master_taskloop || Kind == OMPD_parallel_master_taskloop)
5341     return ReturnBlock;
5342   assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
5343          Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for ||
5344          Kind == OMPD_distribute_parallel_for ||
5345          Kind == OMPD_target_parallel_for ||
5346          Kind == OMPD_teams_distribute_parallel_for ||
5347          Kind == OMPD_target_teams_distribute_parallel_for);
5348   return OMPCancelStack.getExitBlock();
5349 }
5350 
5351 void CodeGenFunction::EmitOMPUseDevicePtrClause(
5352     const OMPClause &NC, OMPPrivateScope &PrivateScope,
5353     const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) {
5354   const auto &C = cast<OMPUseDevicePtrClause>(NC);
5355   auto OrigVarIt = C.varlist_begin();
5356   auto InitIt = C.inits().begin();
5357   for (const Expr *PvtVarIt : C.private_copies()) {
5358     const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl());
5359     const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl());
5360     const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl());
5361 
5362     // In order to identify the right initializer we need to match the
5363     // declaration used by the mapping logic. In some cases we may get
5364     // OMPCapturedExprDecl that refers to the original declaration.
5365     const ValueDecl *MatchingVD = OrigVD;
5366     if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) {
5367       // OMPCapturedExprDecl are used to privative fields of the current
5368       // structure.
5369       const auto *ME = cast<MemberExpr>(OED->getInit());
5370       assert(isa<CXXThisExpr>(ME->getBase()) &&
5371              "Base should be the current struct!");
5372       MatchingVD = ME->getMemberDecl();
5373     }
5374 
5375     // If we don't have information about the current list item, move on to
5376     // the next one.
5377     auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD);
5378     if (InitAddrIt == CaptureDeviceAddrMap.end())
5379       continue;
5380 
5381     bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD,
5382                                                          InitAddrIt, InitVD,
5383                                                          PvtVD]() {
5384       // Initialize the temporary initialization variable with the address we
5385       // get from the runtime library. We have to cast the source address
5386       // because it is always a void *. References are materialized in the
5387       // privatization scope, so the initialization here disregards the fact
5388       // the original variable is a reference.
5389       QualType AddrQTy =
5390           getContext().getPointerType(OrigVD->getType().getNonReferenceType());
5391       llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy);
5392       Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy);
5393       setAddrOfLocalVar(InitVD, InitAddr);
5394 
5395       // Emit private declaration, it will be initialized by the value we
5396       // declaration we just added to the local declarations map.
5397       EmitDecl(*PvtVD);
5398 
5399       // The initialization variables reached its purpose in the emission
5400       // of the previous declaration, so we don't need it anymore.
5401       LocalDeclMap.erase(InitVD);
5402 
5403       // Return the address of the private variable.
5404       return GetAddrOfLocalVar(PvtVD);
5405     });
5406     assert(IsRegistered && "firstprivate var already registered as private");
5407     // Silence the warning about unused variable.
5408     (void)IsRegistered;
5409 
5410     ++OrigVarIt;
5411     ++InitIt;
5412   }
5413 }
5414 
5415 // Generate the instructions for '#pragma omp target data' directive.
5416 void CodeGenFunction::EmitOMPTargetDataDirective(
5417     const OMPTargetDataDirective &S) {
5418   CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true);
5419 
5420   // Create a pre/post action to signal the privatization of the device pointer.
5421   // This action can be replaced by the OpenMP runtime code generation to
5422   // deactivate privatization.
5423   bool PrivatizeDevicePointers = false;
5424   class DevicePointerPrivActionTy : public PrePostActionTy {
5425     bool &PrivatizeDevicePointers;
5426 
5427   public:
5428     explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers)
5429         : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {}
5430     void Enter(CodeGenFunction &CGF) override {
5431       PrivatizeDevicePointers = true;
5432     }
5433   };
5434   DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers);
5435 
5436   auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers](
5437                        CodeGenFunction &CGF, PrePostActionTy &Action) {
5438     auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5439       CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
5440     };
5441 
5442     // Codegen that selects whether to generate the privatization code or not.
5443     auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers,
5444                           &InnermostCodeGen](CodeGenFunction &CGF,
5445                                              PrePostActionTy &Action) {
5446       RegionCodeGenTy RCG(InnermostCodeGen);
5447       PrivatizeDevicePointers = false;
5448 
5449       // Call the pre-action to change the status of PrivatizeDevicePointers if
5450       // needed.
5451       Action.Enter(CGF);
5452 
5453       if (PrivatizeDevicePointers) {
5454         OMPPrivateScope PrivateScope(CGF);
5455         // Emit all instances of the use_device_ptr clause.
5456         for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>())
5457           CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope,
5458                                         Info.CaptureDeviceAddrMap);
5459         (void)PrivateScope.Privatize();
5460         RCG(CGF);
5461       } else {
5462         RCG(CGF);
5463       }
5464     };
5465 
5466     // Forward the provided action to the privatization codegen.
5467     RegionCodeGenTy PrivRCG(PrivCodeGen);
5468     PrivRCG.setAction(Action);
5469 
5470     // Notwithstanding the body of the region is emitted as inlined directive,
5471     // we don't use an inline scope as changes in the references inside the
5472     // region are expected to be visible outside, so we do not privative them.
5473     OMPLexicalScope Scope(CGF, S);
5474     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data,
5475                                                     PrivRCG);
5476   };
5477 
5478   RegionCodeGenTy RCG(CodeGen);
5479 
5480   // If we don't have target devices, don't bother emitting the data mapping
5481   // code.
5482   if (CGM.getLangOpts().OMPTargetTriples.empty()) {
5483     RCG(*this);
5484     return;
5485   }
5486 
5487   // Check if we have any if clause associated with the directive.
5488   const Expr *IfCond = nullptr;
5489   if (const auto *C = S.getSingleClause<OMPIfClause>())
5490     IfCond = C->getCondition();
5491 
5492   // Check if we have any device clause associated with the directive.
5493   const Expr *Device = nullptr;
5494   if (const auto *C = S.getSingleClause<OMPDeviceClause>())
5495     Device = C->getDevice();
5496 
5497   // Set the action to signal privatization of device pointers.
5498   RCG.setAction(PrivAction);
5499 
5500   // Emit region code.
5501   CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG,
5502                                              Info);
5503 }
5504 
5505 void CodeGenFunction::EmitOMPTargetEnterDataDirective(
5506     const OMPTargetEnterDataDirective &S) {
5507   // If we don't have target devices, don't bother emitting the data mapping
5508   // code.
5509   if (CGM.getLangOpts().OMPTargetTriples.empty())
5510     return;
5511 
5512   // Check if we have any if clause associated with the directive.
5513   const Expr *IfCond = nullptr;
5514   if (const auto *C = S.getSingleClause<OMPIfClause>())
5515     IfCond = C->getCondition();
5516 
5517   // Check if we have any device clause associated with the directive.
5518   const Expr *Device = nullptr;
5519   if (const auto *C = S.getSingleClause<OMPDeviceClause>())
5520     Device = C->getDevice();
5521 
5522   OMPLexicalScope Scope(*this, S, OMPD_task);
5523   CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
5524 }
5525 
5526 void CodeGenFunction::EmitOMPTargetExitDataDirective(
5527     const OMPTargetExitDataDirective &S) {
5528   // If we don't have target devices, don't bother emitting the data mapping
5529   // code.
5530   if (CGM.getLangOpts().OMPTargetTriples.empty())
5531     return;
5532 
5533   // Check if we have any if clause associated with the directive.
5534   const Expr *IfCond = nullptr;
5535   if (const auto *C = S.getSingleClause<OMPIfClause>())
5536     IfCond = C->getCondition();
5537 
5538   // Check if we have any device clause associated with the directive.
5539   const Expr *Device = nullptr;
5540   if (const auto *C = S.getSingleClause<OMPDeviceClause>())
5541     Device = C->getDevice();
5542 
5543   OMPLexicalScope Scope(*this, S, OMPD_task);
5544   CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
5545 }
5546 
5547 static void emitTargetParallelRegion(CodeGenFunction &CGF,
5548                                      const OMPTargetParallelDirective &S,
5549                                      PrePostActionTy &Action) {
5550   // Get the captured statement associated with the 'parallel' region.
5551   const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
5552   Action.Enter(CGF);
5553   auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) {
5554     Action.Enter(CGF);
5555     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
5556     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
5557     CGF.EmitOMPPrivateClause(S, PrivateScope);
5558     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
5559     (void)PrivateScope.Privatize();
5560     if (isOpenMPTargetExecutionDirective(S.getDirectiveKind()))
5561       CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S);
5562     // TODO: Add support for clauses.
5563     CGF.EmitStmt(CS->getCapturedStmt());
5564     CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel);
5565   };
5566   emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen,
5567                                  emitEmptyBoundParameters);
5568   emitPostUpdateForReductionClause(CGF, S,
5569                                    [](CodeGenFunction &) { return nullptr; });
5570 }
5571 
5572 void CodeGenFunction::EmitOMPTargetParallelDeviceFunction(
5573     CodeGenModule &CGM, StringRef ParentName,
5574     const OMPTargetParallelDirective &S) {
5575   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5576     emitTargetParallelRegion(CGF, S, Action);
5577   };
5578   llvm::Function *Fn;
5579   llvm::Constant *Addr;
5580   // Emit target region as a standalone region.
5581   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5582       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5583   assert(Fn && Addr && "Target device function emission failed.");
5584 }
5585 
5586 void CodeGenFunction::EmitOMPTargetParallelDirective(
5587     const OMPTargetParallelDirective &S) {
5588   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5589     emitTargetParallelRegion(CGF, S, Action);
5590   };
5591   emitCommonOMPTargetDirective(*this, S, CodeGen);
5592 }
5593 
5594 static void emitTargetParallelForRegion(CodeGenFunction &CGF,
5595                                         const OMPTargetParallelForDirective &S,
5596                                         PrePostActionTy &Action) {
5597   Action.Enter(CGF);
5598   // Emit directive as a combined directive that consists of two implicit
5599   // directives: 'parallel' with 'for' directive.
5600   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5601     Action.Enter(CGF);
5602     CodeGenFunction::OMPCancelStackRAII CancelRegion(
5603         CGF, OMPD_target_parallel_for, S.hasCancel());
5604     CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
5605                                emitDispatchForLoopBounds);
5606   };
5607   emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen,
5608                                  emitEmptyBoundParameters);
5609 }
5610 
5611 void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction(
5612     CodeGenModule &CGM, StringRef ParentName,
5613     const OMPTargetParallelForDirective &S) {
5614   // Emit SPMD target parallel for region as a standalone region.
5615   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5616     emitTargetParallelForRegion(CGF, S, Action);
5617   };
5618   llvm::Function *Fn;
5619   llvm::Constant *Addr;
5620   // Emit target region as a standalone region.
5621   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5622       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5623   assert(Fn && Addr && "Target device function emission failed.");
5624 }
5625 
5626 void CodeGenFunction::EmitOMPTargetParallelForDirective(
5627     const OMPTargetParallelForDirective &S) {
5628   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5629     emitTargetParallelForRegion(CGF, S, Action);
5630   };
5631   emitCommonOMPTargetDirective(*this, S, CodeGen);
5632 }
5633 
5634 static void
5635 emitTargetParallelForSimdRegion(CodeGenFunction &CGF,
5636                                 const OMPTargetParallelForSimdDirective &S,
5637                                 PrePostActionTy &Action) {
5638   Action.Enter(CGF);
5639   // Emit directive as a combined directive that consists of two implicit
5640   // directives: 'parallel' with 'for' directive.
5641   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5642     Action.Enter(CGF);
5643     CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds,
5644                                emitDispatchForLoopBounds);
5645   };
5646   emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen,
5647                                  emitEmptyBoundParameters);
5648 }
5649 
5650 void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction(
5651     CodeGenModule &CGM, StringRef ParentName,
5652     const OMPTargetParallelForSimdDirective &S) {
5653   // Emit SPMD target parallel for region as a standalone region.
5654   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5655     emitTargetParallelForSimdRegion(CGF, S, Action);
5656   };
5657   llvm::Function *Fn;
5658   llvm::Constant *Addr;
5659   // Emit target region as a standalone region.
5660   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
5661       S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen);
5662   assert(Fn && Addr && "Target device function emission failed.");
5663 }
5664 
5665 void CodeGenFunction::EmitOMPTargetParallelForSimdDirective(
5666     const OMPTargetParallelForSimdDirective &S) {
5667   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5668     emitTargetParallelForSimdRegion(CGF, S, Action);
5669   };
5670   emitCommonOMPTargetDirective(*this, S, CodeGen);
5671 }
5672 
5673 /// Emit a helper variable and return corresponding lvalue.
5674 static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
5675                      const ImplicitParamDecl *PVD,
5676                      CodeGenFunction::OMPPrivateScope &Privates) {
5677   const auto *VDecl = cast<VarDecl>(Helper->getDecl());
5678   Privates.addPrivate(VDecl,
5679                       [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); });
5680 }
5681 
5682 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
5683   assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
5684   // Emit outlined function for task construct.
5685   const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop);
5686   Address CapturedStruct = Address::invalid();
5687   {
5688     OMPLexicalScope Scope(*this, S, OMPD_taskloop, /*EmitPreInitStmt=*/false);
5689     CapturedStruct = GenerateCapturedStmtArgument(*CS);
5690   }
5691   QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
5692   const Expr *IfCond = nullptr;
5693   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
5694     if (C->getNameModifier() == OMPD_unknown ||
5695         C->getNameModifier() == OMPD_taskloop) {
5696       IfCond = C->getCondition();
5697       break;
5698     }
5699   }
5700 
5701   OMPTaskDataTy Data;
5702   // Check if taskloop must be emitted without taskgroup.
5703   Data.Nogroup = S.getSingleClause<OMPNogroupClause>();
5704   // TODO: Check if we should emit tied or untied task.
5705   Data.Tied = true;
5706   // Set scheduling for taskloop
5707   if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) {
5708     // grainsize clause
5709     Data.Schedule.setInt(/*IntVal=*/false);
5710     Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize()));
5711   } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) {
5712     // num_tasks clause
5713     Data.Schedule.setInt(/*IntVal=*/true);
5714     Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks()));
5715   }
5716 
5717   auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
5718     // if (PreCond) {
5719     //   for (IV in 0..LastIteration) BODY;
5720     //   <Final counter/linear vars updates>;
5721     // }
5722     //
5723 
5724     // Emit: if (PreCond) - begin.
5725     // If the condition constant folds and can be elided, avoid emitting the
5726     // whole loop.
5727     bool CondConstant;
5728     llvm::BasicBlock *ContBlock = nullptr;
5729     OMPLoopScope PreInitScope(CGF, S);
5730     if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
5731       if (!CondConstant)
5732         return;
5733     } else {
5734       llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
5735       ContBlock = CGF.createBasicBlock("taskloop.if.end");
5736       emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
5737                   CGF.getProfileCount(&S));
5738       CGF.EmitBlock(ThenBlock);
5739       CGF.incrementProfileCounter(&S);
5740     }
5741 
5742     (void)CGF.EmitOMPLinearClauseInit(S);
5743 
5744     OMPPrivateScope LoopScope(CGF);
5745     // Emit helper vars inits.
5746     enum { LowerBound = 5, UpperBound, Stride, LastIter };
5747     auto *I = CS->getCapturedDecl()->param_begin();
5748     auto *LBP = std::next(I, LowerBound);
5749     auto *UBP = std::next(I, UpperBound);
5750     auto *STP = std::next(I, Stride);
5751     auto *LIP = std::next(I, LastIter);
5752     mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
5753              LoopScope);
5754     mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
5755              LoopScope);
5756     mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
5757     mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
5758              LoopScope);
5759     CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
5760     CGF.EmitOMPLinearClause(S, LoopScope);
5761     bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
5762     (void)LoopScope.Privatize();
5763     // Emit the loop iteration variable.
5764     const Expr *IVExpr = S.getIterationVariable();
5765     const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
5766     CGF.EmitVarDecl(*IVDecl);
5767     CGF.EmitIgnoredExpr(S.getInit());
5768 
5769     // Emit the iterations count variable.
5770     // If it is not a variable, Sema decided to calculate iterations count on
5771     // each iteration (e.g., it is foldable into a constant).
5772     if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
5773       CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
5774       // Emit calculation of the iterations count.
5775       CGF.EmitIgnoredExpr(S.getCalcLastIteration());
5776     }
5777 
5778     {
5779       OMPLexicalScope Scope(CGF, S, OMPD_taskloop, /*EmitPreInitStmt=*/false);
5780       emitCommonSimdLoop(
5781           CGF, S,
5782           [&S](CodeGenFunction &CGF, PrePostActionTy &) {
5783             if (isOpenMPSimdDirective(S.getDirectiveKind()))
5784               CGF.EmitOMPSimdInit(S);
5785           },
5786           [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) {
5787             CGF.EmitOMPInnerLoop(
5788                 S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
5789                 [&S](CodeGenFunction &CGF) {
5790                   CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest());
5791                   CGF.EmitStopPoint(&S);
5792                 },
5793                 [](CodeGenFunction &) {});
5794           });
5795     }
5796     // Emit: if (PreCond) - end.
5797     if (ContBlock) {
5798       CGF.EmitBranch(ContBlock);
5799       CGF.EmitBlock(ContBlock, true);
5800     }
5801     // Emit final copy of the lastprivate variables if IsLastIter != 0.
5802     if (HasLastprivateClause) {
5803       CGF.EmitOMPLastprivateClauseFinal(
5804           S, isOpenMPSimdDirective(S.getDirectiveKind()),
5805           CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar(
5806               CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
5807               (*LIP)->getType(), S.getBeginLoc())));
5808     }
5809     CGF.EmitOMPLinearClauseFinal(S, [LIP, &S](CodeGenFunction &CGF) {
5810       return CGF.Builder.CreateIsNotNull(
5811           CGF.EmitLoadOfScalar(CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false,
5812                                (*LIP)->getType(), S.getBeginLoc()));
5813     });
5814   };
5815   auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
5816                     IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn,
5817                             const OMPTaskDataTy &Data) {
5818     auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond,
5819                       &Data](CodeGenFunction &CGF, PrePostActionTy &) {
5820       OMPLoopScope PreInitScope(CGF, S);
5821       CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S,
5822                                                   OutlinedFn, SharedsTy,
5823                                                   CapturedStruct, IfCond, Data);
5824     };
5825     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
5826                                                     CodeGen);
5827   };
5828   if (Data.Nogroup) {
5829     EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data);
5830   } else {
5831     CGM.getOpenMPRuntime().emitTaskgroupRegion(
5832         *this,
5833         [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF,
5834                                         PrePostActionTy &Action) {
5835           Action.Enter(CGF);
5836           CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen,
5837                                         Data);
5838         },
5839         S.getBeginLoc());
5840   }
5841 }
5842 
5843 void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
5844   auto LPCRegion =
5845       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5846   EmitOMPTaskLoopBasedDirective(S);
5847 }
5848 
5849 void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
5850     const OMPTaskLoopSimdDirective &S) {
5851   auto LPCRegion =
5852       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5853   OMPLexicalScope Scope(*this, S);
5854   EmitOMPTaskLoopBasedDirective(S);
5855 }
5856 
5857 void CodeGenFunction::EmitOMPMasterTaskLoopDirective(
5858     const OMPMasterTaskLoopDirective &S) {
5859   auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5860     Action.Enter(CGF);
5861     EmitOMPTaskLoopBasedDirective(S);
5862   };
5863   auto LPCRegion =
5864       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5865   OMPLexicalScope Scope(*this, S, llvm::None, /*EmitPreInitStmt=*/false);
5866   CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc());
5867 }
5868 
5869 void CodeGenFunction::EmitOMPMasterTaskLoopSimdDirective(
5870     const OMPMasterTaskLoopSimdDirective &S) {
5871   auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5872     Action.Enter(CGF);
5873     EmitOMPTaskLoopBasedDirective(S);
5874   };
5875   auto LPCRegion =
5876       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5877   OMPLexicalScope Scope(*this, S);
5878   CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc());
5879 }
5880 
5881 void CodeGenFunction::EmitOMPParallelMasterTaskLoopDirective(
5882     const OMPParallelMasterTaskLoopDirective &S) {
5883   auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5884     auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF,
5885                                   PrePostActionTy &Action) {
5886       Action.Enter(CGF);
5887       CGF.EmitOMPTaskLoopBasedDirective(S);
5888     };
5889     OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false);
5890     CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen,
5891                                             S.getBeginLoc());
5892   };
5893   auto LPCRegion =
5894       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5895   emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop, CodeGen,
5896                                  emitEmptyBoundParameters);
5897 }
5898 
5899 void CodeGenFunction::EmitOMPParallelMasterTaskLoopSimdDirective(
5900     const OMPParallelMasterTaskLoopSimdDirective &S) {
5901   auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) {
5902     auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF,
5903                                   PrePostActionTy &Action) {
5904       Action.Enter(CGF);
5905       CGF.EmitOMPTaskLoopBasedDirective(S);
5906     };
5907     OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false);
5908     CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen,
5909                                             S.getBeginLoc());
5910   };
5911   auto LPCRegion =
5912       CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S);
5913   emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop_simd, CodeGen,
5914                                  emitEmptyBoundParameters);
5915 }
5916 
5917 // Generate the instructions for '#pragma omp target update' directive.
5918 void CodeGenFunction::EmitOMPTargetUpdateDirective(
5919     const OMPTargetUpdateDirective &S) {
5920   // If we don't have target devices, don't bother emitting the data mapping
5921   // code.
5922   if (CGM.getLangOpts().OMPTargetTriples.empty())
5923     return;
5924 
5925   // Check if we have any if clause associated with the directive.
5926   const Expr *IfCond = nullptr;
5927   if (const auto *C = S.getSingleClause<OMPIfClause>())
5928     IfCond = C->getCondition();
5929 
5930   // Check if we have any device clause associated with the directive.
5931   const Expr *Device = nullptr;
5932   if (const auto *C = S.getSingleClause<OMPDeviceClause>())
5933     Device = C->getDevice();
5934 
5935   OMPLexicalScope Scope(*this, S, OMPD_task);
5936   CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device);
5937 }
5938 
5939 void CodeGenFunction::EmitSimpleOMPExecutableDirective(
5940     const OMPExecutableDirective &D) {
5941   if (!D.hasAssociatedStmt() || !D.getAssociatedStmt())
5942     return;
5943   auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) {
5944     OMPPrivateScope GlobalsScope(CGF);
5945     if (isOpenMPTaskingDirective(D.getDirectiveKind())) {
5946       // Capture global firstprivates to avoid crash.
5947       for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
5948         for (const Expr *Ref : C->varlists()) {
5949           const auto *DRE = cast<DeclRefExpr>(Ref->IgnoreParenImpCasts());
5950           if (!DRE)
5951             continue;
5952           const auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
5953           if (!VD || VD->hasLocalStorage())
5954             continue;
5955           if (!CGF.LocalDeclMap.count(VD)) {
5956             LValue GlobLVal = CGF.EmitLValue(Ref);
5957             GlobalsScope.addPrivate(
5958                 VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); });
5959           }
5960         }
5961       }
5962     }
5963     if (isOpenMPSimdDirective(D.getDirectiveKind())) {
5964       (void)GlobalsScope.Privatize();
5965       emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action);
5966     } else {
5967       if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) {
5968         for (const Expr *E : LD->counters()) {
5969           const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
5970           if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) {
5971             LValue GlobLVal = CGF.EmitLValue(E);
5972             GlobalsScope.addPrivate(
5973                 VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); });
5974           }
5975           if (isa<OMPCapturedExprDecl>(VD)) {
5976             // Emit only those that were not explicitly referenced in clauses.
5977             if (!CGF.LocalDeclMap.count(VD))
5978               CGF.EmitVarDecl(*VD);
5979           }
5980         }
5981         for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) {
5982           if (!C->getNumForLoops())
5983             continue;
5984           for (unsigned I = LD->getCollapsedNumber(),
5985                         E = C->getLoopNumIterations().size();
5986                I < E; ++I) {
5987             if (const auto *VD = dyn_cast<OMPCapturedExprDecl>(
5988                     cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) {
5989               // Emit only those that were not explicitly referenced in clauses.
5990               if (!CGF.LocalDeclMap.count(VD))
5991                 CGF.EmitVarDecl(*VD);
5992             }
5993           }
5994         }
5995       }
5996       (void)GlobalsScope.Privatize();
5997       CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt());
5998     }
5999   };
6000   {
6001     auto LPCRegion =
6002         CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, D);
6003     OMPSimdLexicalScope Scope(*this, D);
6004     CGM.getOpenMPRuntime().emitInlinedDirective(
6005         *this,
6006         isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd
6007                                                     : D.getDirectiveKind(),
6008         CodeGen);
6009   }
6010   // Check for outer lastprivate conditional update.
6011   checkForLastprivateConditionalUpdate(*this, D);
6012 }
6013