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