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