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