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