1 //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit OpenMP nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGCleanup.h"
15 #include "CGOpenMPRuntime.h"
16 #include "CodeGenFunction.h"
17 #include "CodeGenModule.h"
18 #include "TargetInfo.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/StmtOpenMP.h"
21 #include "clang/AST/DeclOpenMP.h"
22 #include "llvm/IR/CallSite.h"
23 using namespace clang;
24 using namespace CodeGen;
25 
26 namespace {
27 /// Lexical scope for OpenMP executable constructs, that handles correct codegen
28 /// for captured expressions.
29 class OMPLexicalScope final : public CodeGenFunction::LexicalScope {
30   void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) {
31     for (const auto *C : S.clauses()) {
32       if (auto *CPI = OMPClauseWithPreInit::get(C)) {
33         if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) {
34           for (const auto *I : PreInit->decls()) {
35             if (!I->hasAttr<OMPCaptureNoInitAttr>())
36               CGF.EmitVarDecl(cast<VarDecl>(*I));
37             else {
38               CodeGenFunction::AutoVarEmission Emission =
39                   CGF.EmitAutoVarAlloca(cast<VarDecl>(*I));
40               CGF.EmitAutoVarCleanups(Emission);
41             }
42           }
43         }
44       }
45     }
46   }
47   CodeGenFunction::OMPPrivateScope InlinedShareds;
48 
49   static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) {
50     return CGF.LambdaCaptureFields.lookup(VD) ||
51            (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) ||
52            (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl));
53   }
54 
55 public:
56   OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S,
57                   bool AsInlined = false)
58       : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()),
59         InlinedShareds(CGF) {
60     emitPreInitStmt(CGF, S);
61     if (AsInlined) {
62       if (S.hasAssociatedStmt()) {
63         auto *CS = cast<CapturedStmt>(S.getAssociatedStmt());
64         for (auto &C : CS->captures()) {
65           if (C.capturesVariable() || C.capturesVariableByCopy()) {
66             auto *VD = C.getCapturedVar();
67             DeclRefExpr DRE(const_cast<VarDecl *>(VD),
68                             isCapturedVar(CGF, VD) ||
69                                 (CGF.CapturedStmtInfo &&
70                                  InlinedShareds.isGlobalVarCaptured(VD)),
71                             VD->getType().getNonReferenceType(), VK_LValue,
72                             SourceLocation());
73             InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address {
74               return CGF.EmitLValue(&DRE).getAddress();
75             });
76           }
77         }
78         (void)InlinedShareds.Privatize();
79       }
80     }
81   }
82 };
83 
84 /// Private scope for OpenMP loop-based directives, that supports capturing
85 /// of used expression from loop statement.
86 class OMPLoopScope : public CodeGenFunction::RunCleanupsScope {
87   void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) {
88     if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
89       if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) {
90         for (const auto *I : PreInits->decls())
91           CGF.EmitVarDecl(cast<VarDecl>(*I));
92       }
93     }
94   }
95 
96 public:
97   OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S)
98       : CodeGenFunction::RunCleanupsScope(CGF) {
99     emitPreInitStmt(CGF, S);
100   }
101 };
102 
103 } // namespace
104 
105 llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) {
106   auto &C = getContext();
107   llvm::Value *Size = nullptr;
108   auto SizeInChars = C.getTypeSizeInChars(Ty);
109   if (SizeInChars.isZero()) {
110     // getTypeSizeInChars() returns 0 for a VLA.
111     while (auto *VAT = C.getAsVariableArrayType(Ty)) {
112       llvm::Value *ArraySize;
113       std::tie(ArraySize, Ty) = getVLASize(VAT);
114       Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize;
115     }
116     SizeInChars = C.getTypeSizeInChars(Ty);
117     if (SizeInChars.isZero())
118       return llvm::ConstantInt::get(SizeTy, /*V=*/0);
119     Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars));
120   } else
121     Size = CGM.getSize(SizeInChars);
122   return Size;
123 }
124 
125 void CodeGenFunction::GenerateOpenMPCapturedVars(
126     const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) {
127   const RecordDecl *RD = S.getCapturedRecordDecl();
128   auto CurField = RD->field_begin();
129   auto CurCap = S.captures().begin();
130   for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
131                                                  E = S.capture_init_end();
132        I != E; ++I, ++CurField, ++CurCap) {
133     if (CurField->hasCapturedVLAType()) {
134       auto VAT = CurField->getCapturedVLAType();
135       auto *Val = VLASizeMap[VAT->getSizeExpr()];
136       CapturedVars.push_back(Val);
137     } else if (CurCap->capturesThis())
138       CapturedVars.push_back(CXXThisValue);
139     else if (CurCap->capturesVariableByCopy())
140       CapturedVars.push_back(
141           EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal());
142     else {
143       assert(CurCap->capturesVariable() && "Expected capture by reference.");
144       CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer());
145     }
146   }
147 }
148 
149 static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType,
150                                     StringRef Name, LValue AddrLV,
151                                     bool isReferenceType = false) {
152   ASTContext &Ctx = CGF.getContext();
153 
154   auto *CastedPtr = CGF.EmitScalarConversion(
155       AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(),
156       Ctx.getPointerType(DstType), SourceLocation());
157   auto TmpAddr =
158       CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType))
159           .getAddress();
160 
161   // If we are dealing with references we need to return the address of the
162   // reference instead of the reference of the value.
163   if (isReferenceType) {
164     QualType RefType = Ctx.getLValueReferenceType(DstType);
165     auto *RefVal = TmpAddr.getPointer();
166     TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref");
167     auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType);
168     CGF.EmitScalarInit(RefVal, TmpLVal);
169   }
170 
171   return TmpAddr;
172 }
173 
174 llvm::Function *
175 CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) {
176   assert(
177       CapturedStmtInfo &&
178       "CapturedStmtInfo should be set when generating the captured function");
179   const CapturedDecl *CD = S.getCapturedDecl();
180   const RecordDecl *RD = S.getCapturedRecordDecl();
181   assert(CD->hasBody() && "missing CapturedDecl body");
182 
183   // Build the argument list.
184   ASTContext &Ctx = CGM.getContext();
185   FunctionArgList Args;
186   Args.append(CD->param_begin(),
187               std::next(CD->param_begin(), CD->getContextParamPosition()));
188   auto I = S.captures().begin();
189   for (auto *FD : RD->fields()) {
190     QualType ArgType = FD->getType();
191     IdentifierInfo *II = nullptr;
192     VarDecl *CapVar = nullptr;
193 
194     // If this is a capture by copy and the type is not a pointer, the outlined
195     // function argument type should be uintptr and the value properly casted to
196     // uintptr. This is necessary given that the runtime library is only able to
197     // deal with pointers. We can pass in the same way the VLA type sizes to the
198     // outlined function.
199     if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) ||
200         I->capturesVariableArrayType())
201       ArgType = Ctx.getUIntPtrType();
202 
203     if (I->capturesVariable() || I->capturesVariableByCopy()) {
204       CapVar = I->getCapturedVar();
205       II = CapVar->getIdentifier();
206     } else if (I->capturesThis())
207       II = &getContext().Idents.get("this");
208     else {
209       assert(I->capturesVariableArrayType());
210       II = &getContext().Idents.get("vla");
211     }
212     if (ArgType->isVariablyModifiedType())
213       ArgType = getContext().getVariableArrayDecayedType(ArgType);
214     Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr,
215                                              FD->getLocation(), II, ArgType));
216     ++I;
217   }
218   Args.append(
219       std::next(CD->param_begin(), CD->getContextParamPosition() + 1),
220       CD->param_end());
221 
222   // Create the function declaration.
223   FunctionType::ExtInfo ExtInfo;
224   const CGFunctionInfo &FuncInfo =
225       CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
226   llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
227 
228   llvm::Function *F = llvm::Function::Create(
229       FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
230       CapturedStmtInfo->getHelperName(), &CGM.getModule());
231   CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
232   if (CD->isNothrow())
233     F->addFnAttr(llvm::Attribute::NoUnwind);
234 
235   // Generate the function.
236   StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
237                 CD->getBody()->getLocStart());
238   unsigned Cnt = CD->getContextParamPosition();
239   I = S.captures().begin();
240   for (auto *FD : RD->fields()) {
241     // If we are capturing a pointer by copy we don't need to do anything, just
242     // use the value that we get from the arguments.
243     if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) {
244       setAddrOfLocalVar(I->getCapturedVar(), GetAddrOfLocalVar(Args[Cnt]));
245       ++Cnt;
246       ++I;
247       continue;
248     }
249 
250     LValue ArgLVal =
251         MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(),
252                        AlignmentSource::Decl);
253     if (FD->hasCapturedVLAType()) {
254       LValue CastedArgLVal =
255           MakeAddrLValue(castValueFromUintptr(*this, FD->getType(),
256                                               Args[Cnt]->getName(), ArgLVal),
257                          FD->getType(), AlignmentSource::Decl);
258       auto *ExprArg =
259           EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal();
260       auto VAT = FD->getCapturedVLAType();
261       VLASizeMap[VAT->getSizeExpr()] = ExprArg;
262     } else if (I->capturesVariable()) {
263       auto *Var = I->getCapturedVar();
264       QualType VarTy = Var->getType();
265       Address ArgAddr = ArgLVal.getAddress();
266       if (!VarTy->isReferenceType()) {
267         ArgAddr = EmitLoadOfReference(
268             ArgAddr, ArgLVal.getType()->castAs<ReferenceType>());
269       }
270       setAddrOfLocalVar(
271           Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var)));
272     } else if (I->capturesVariableByCopy()) {
273       assert(!FD->getType()->isAnyPointerType() &&
274              "Not expecting a captured pointer.");
275       auto *Var = I->getCapturedVar();
276       QualType VarTy = Var->getType();
277       setAddrOfLocalVar(I->getCapturedVar(),
278                         castValueFromUintptr(*this, FD->getType(),
279                                              Args[Cnt]->getName(), ArgLVal,
280                                              VarTy->isReferenceType()));
281     } else {
282       // If 'this' is captured, load it into CXXThisValue.
283       assert(I->capturesThis());
284       CXXThisValue =
285           EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal();
286     }
287     ++Cnt;
288     ++I;
289   }
290 
291   PGO.assignRegionCounters(GlobalDecl(CD), F);
292   CapturedStmtInfo->EmitBody(*this, CD->getBody());
293   FinishFunction(CD->getBodyRBrace());
294 
295   return F;
296 }
297 
298 //===----------------------------------------------------------------------===//
299 //                              OpenMP Directive Emission
300 //===----------------------------------------------------------------------===//
301 void CodeGenFunction::EmitOMPAggregateAssign(
302     Address DestAddr, Address SrcAddr, QualType OriginalType,
303     const llvm::function_ref<void(Address, Address)> &CopyGen) {
304   // Perform element-by-element initialization.
305   QualType ElementTy;
306 
307   // Drill down to the base element type on both arrays.
308   auto ArrayTy = OriginalType->getAsArrayTypeUnsafe();
309   auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
310   SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
311 
312   auto SrcBegin = SrcAddr.getPointer();
313   auto DestBegin = DestAddr.getPointer();
314   // Cast from pointer to array type to pointer to single element.
315   auto DestEnd = Builder.CreateGEP(DestBegin, NumElements);
316   // The basic structure here is a while-do loop.
317   auto BodyBB = createBasicBlock("omp.arraycpy.body");
318   auto DoneBB = createBasicBlock("omp.arraycpy.done");
319   auto IsEmpty =
320       Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty");
321   Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
322 
323   // Enter the loop body, making that address the current address.
324   auto EntryBB = Builder.GetInsertBlock();
325   EmitBlock(BodyBB);
326 
327   CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy);
328 
329   llvm::PHINode *SrcElementPHI =
330     Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast");
331   SrcElementPHI->addIncoming(SrcBegin, EntryBB);
332   Address SrcElementCurrent =
333       Address(SrcElementPHI,
334               SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
335 
336   llvm::PHINode *DestElementPHI =
337     Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
338   DestElementPHI->addIncoming(DestBegin, EntryBB);
339   Address DestElementCurrent =
340     Address(DestElementPHI,
341             DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
342 
343   // Emit copy.
344   CopyGen(DestElementCurrent, SrcElementCurrent);
345 
346   // Shift the address forward by one element.
347   auto DestElementNext = Builder.CreateConstGEP1_32(
348       DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
349   auto SrcElementNext = Builder.CreateConstGEP1_32(
350       SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element");
351   // Check whether we've reached the end.
352   auto Done =
353       Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
354   Builder.CreateCondBr(Done, DoneBB, BodyBB);
355   DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock());
356   SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock());
357 
358   // Done.
359   EmitBlock(DoneBB, /*IsFinished=*/true);
360 }
361 
362 /// Check if the combiner is a call to UDR combiner and if it is so return the
363 /// UDR decl used for reduction.
364 static const OMPDeclareReductionDecl *
365 getReductionInit(const Expr *ReductionOp) {
366   if (auto *CE = dyn_cast<CallExpr>(ReductionOp))
367     if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee()))
368       if (auto *DRE =
369               dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts()))
370         if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl()))
371           return DRD;
372   return nullptr;
373 }
374 
375 static void emitInitWithReductionInitializer(CodeGenFunction &CGF,
376                                              const OMPDeclareReductionDecl *DRD,
377                                              const Expr *InitOp,
378                                              Address Private, Address Original,
379                                              QualType Ty) {
380   if (DRD->getInitializer()) {
381     std::pair<llvm::Function *, llvm::Function *> Reduction =
382         CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD);
383     auto *CE = cast<CallExpr>(InitOp);
384     auto *OVE = cast<OpaqueValueExpr>(CE->getCallee());
385     const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
386     const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
387     auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr());
388     auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr());
389     CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
390     PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()),
391                             [=]() -> Address { return Private; });
392     PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()),
393                             [=]() -> Address { return Original; });
394     (void)PrivateScope.Privatize();
395     RValue Func = RValue::get(Reduction.second);
396     CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func);
397     CGF.EmitIgnoredExpr(InitOp);
398   } else {
399     llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty);
400     auto *GV = new llvm::GlobalVariable(
401         CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
402         llvm::GlobalValue::PrivateLinkage, Init, ".init");
403     LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty);
404     RValue InitRVal;
405     switch (CGF.getEvaluationKind(Ty)) {
406     case TEK_Scalar:
407       InitRVal = CGF.EmitLoadOfLValue(LV, SourceLocation());
408       break;
409     case TEK_Complex:
410       InitRVal =
411           RValue::getComplex(CGF.EmitLoadOfComplex(LV, SourceLocation()));
412       break;
413     case TEK_Aggregate:
414       InitRVal = RValue::getAggregate(LV.getAddress());
415       break;
416     }
417     OpaqueValueExpr OVE(SourceLocation(), Ty, VK_RValue);
418     CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal);
419     CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(),
420                          /*IsInitializer=*/false);
421   }
422 }
423 
424 /// \brief Emit initialization of arrays of complex types.
425 /// \param DestAddr Address of the array.
426 /// \param Type Type of array.
427 /// \param Init Initial expression of array.
428 /// \param SrcAddr Address of the original array.
429 static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
430                                  QualType Type, const Expr *Init,
431                                  Address SrcAddr = Address::invalid()) {
432   auto *DRD = getReductionInit(Init);
433   // Perform element-by-element initialization.
434   QualType ElementTy;
435 
436   // Drill down to the base element type on both arrays.
437   auto ArrayTy = Type->getAsArrayTypeUnsafe();
438   auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
439   DestAddr =
440       CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType());
441   if (DRD)
442     SrcAddr =
443         CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
444 
445   llvm::Value *SrcBegin = nullptr;
446   if (DRD)
447     SrcBegin = SrcAddr.getPointer();
448   auto DestBegin = DestAddr.getPointer();
449   // Cast from pointer to array type to pointer to single element.
450   auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements);
451   // The basic structure here is a while-do loop.
452   auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body");
453   auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done");
454   auto IsEmpty =
455       CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty");
456   CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
457 
458   // Enter the loop body, making that address the current address.
459   auto EntryBB = CGF.Builder.GetInsertBlock();
460   CGF.EmitBlock(BodyBB);
461 
462   CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy);
463 
464   llvm::PHINode *SrcElementPHI = nullptr;
465   Address SrcElementCurrent = Address::invalid();
466   if (DRD) {
467     SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2,
468                                           "omp.arraycpy.srcElementPast");
469     SrcElementPHI->addIncoming(SrcBegin, EntryBB);
470     SrcElementCurrent =
471         Address(SrcElementPHI,
472                 SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize));
473   }
474   llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI(
475       DestBegin->getType(), 2, "omp.arraycpy.destElementPast");
476   DestElementPHI->addIncoming(DestBegin, EntryBB);
477   Address DestElementCurrent =
478       Address(DestElementPHI,
479               DestAddr.getAlignment().alignmentOfArrayElement(ElementSize));
480 
481   // Emit copy.
482   {
483     CodeGenFunction::RunCleanupsScope InitScope(CGF);
484     if (DRD && (DRD->getInitializer() || !Init)) {
485       emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent,
486                                        SrcElementCurrent, ElementTy);
487     } else
488       CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(),
489                            /*IsInitializer=*/false);
490   }
491 
492   if (DRD) {
493     // Shift the address forward by one element.
494     auto SrcElementNext = CGF.Builder.CreateConstGEP1_32(
495         SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
496     SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock());
497   }
498 
499   // Shift the address forward by one element.
500   auto DestElementNext = CGF.Builder.CreateConstGEP1_32(
501       DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element");
502   // Check whether we've reached the end.
503   auto Done =
504       CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done");
505   CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB);
506   DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock());
507 
508   // Done.
509   CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
510 }
511 
512 void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr,
513                                   Address SrcAddr, const VarDecl *DestVD,
514                                   const VarDecl *SrcVD, const Expr *Copy) {
515   if (OriginalType->isArrayType()) {
516     auto *BO = dyn_cast<BinaryOperator>(Copy);
517     if (BO && BO->getOpcode() == BO_Assign) {
518       // Perform simple memcpy for simple copying.
519       EmitAggregateAssign(DestAddr, SrcAddr, OriginalType);
520     } else {
521       // For arrays with complex element types perform element by element
522       // copying.
523       EmitOMPAggregateAssign(
524           DestAddr, SrcAddr, OriginalType,
525           [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) {
526             // Working with the single array element, so have to remap
527             // destination and source variables to corresponding array
528             // elements.
529             CodeGenFunction::OMPPrivateScope Remap(*this);
530             Remap.addPrivate(DestVD, [DestElement]() -> Address {
531               return DestElement;
532             });
533             Remap.addPrivate(
534                 SrcVD, [SrcElement]() -> Address { return SrcElement; });
535             (void)Remap.Privatize();
536             EmitIgnoredExpr(Copy);
537           });
538     }
539   } else {
540     // Remap pseudo source variable to private copy.
541     CodeGenFunction::OMPPrivateScope Remap(*this);
542     Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; });
543     Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; });
544     (void)Remap.Privatize();
545     // Emit copying of the whole variable.
546     EmitIgnoredExpr(Copy);
547   }
548 }
549 
550 bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
551                                                 OMPPrivateScope &PrivateScope) {
552   if (!HaveInsertPoint())
553     return false;
554   bool FirstprivateIsLastprivate = false;
555   llvm::DenseSet<const VarDecl *> Lastprivates;
556   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
557     for (const auto *D : C->varlists())
558       Lastprivates.insert(
559           cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl());
560   }
561   llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate;
562   for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) {
563     auto IRef = C->varlist_begin();
564     auto InitsRef = C->inits().begin();
565     for (auto IInit : C->private_copies()) {
566       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
567       FirstprivateIsLastprivate =
568           FirstprivateIsLastprivate ||
569           (Lastprivates.count(OrigVD->getCanonicalDecl()) > 0);
570       if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) {
571         auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
572         auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl());
573         bool IsRegistered;
574         DeclRefExpr DRE(
575             const_cast<VarDecl *>(OrigVD),
576             /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
577                 OrigVD) != nullptr,
578             (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
579         Address OriginalAddr = EmitLValue(&DRE).getAddress();
580         QualType Type = VD->getType();
581         if (Type->isArrayType()) {
582           // Emit VarDecl with copy init for arrays.
583           // Get the address of the original variable captured in current
584           // captured region.
585           IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
586             auto Emission = EmitAutoVarAlloca(*VD);
587             auto *Init = VD->getInit();
588             if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) {
589               // Perform simple memcpy.
590               EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr,
591                                   Type);
592             } else {
593               EmitOMPAggregateAssign(
594                   Emission.getAllocatedAddress(), OriginalAddr, Type,
595                   [this, VDInit, Init](Address DestElement,
596                                        Address SrcElement) {
597                     // Clean up any temporaries needed by the initialization.
598                     RunCleanupsScope InitScope(*this);
599                     // Emit initialization for single element.
600                     setAddrOfLocalVar(VDInit, SrcElement);
601                     EmitAnyExprToMem(Init, DestElement,
602                                      Init->getType().getQualifiers(),
603                                      /*IsInitializer*/ false);
604                     LocalDeclMap.erase(VDInit);
605                   });
606             }
607             EmitAutoVarCleanups(Emission);
608             return Emission.getAllocatedAddress();
609           });
610         } else {
611           IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
612             // Emit private VarDecl with copy init.
613             // Remap temp VDInit variable to the address of the original
614             // variable
615             // (for proper handling of captured global variables).
616             setAddrOfLocalVar(VDInit, OriginalAddr);
617             EmitDecl(*VD);
618             LocalDeclMap.erase(VDInit);
619             return GetAddrOfLocalVar(VD);
620           });
621         }
622         assert(IsRegistered &&
623                "firstprivate var already registered as private");
624         // Silence the warning about unused variable.
625         (void)IsRegistered;
626       }
627       ++IRef;
628       ++InitsRef;
629     }
630   }
631   return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty();
632 }
633 
634 void CodeGenFunction::EmitOMPPrivateClause(
635     const OMPExecutableDirective &D,
636     CodeGenFunction::OMPPrivateScope &PrivateScope) {
637   if (!HaveInsertPoint())
638     return;
639   llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
640   for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) {
641     auto IRef = C->varlist_begin();
642     for (auto IInit : C->private_copies()) {
643       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
644       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
645         auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
646         bool IsRegistered =
647             PrivateScope.addPrivate(OrigVD, [&]() -> Address {
648               // Emit private VarDecl with copy init.
649               EmitDecl(*VD);
650               return GetAddrOfLocalVar(VD);
651             });
652         assert(IsRegistered && "private var already registered as private");
653         // Silence the warning about unused variable.
654         (void)IsRegistered;
655       }
656       ++IRef;
657     }
658   }
659 }
660 
661 bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) {
662   if (!HaveInsertPoint())
663     return false;
664   // threadprivate_var1 = master_threadprivate_var1;
665   // operator=(threadprivate_var2, master_threadprivate_var2);
666   // ...
667   // __kmpc_barrier(&loc, global_tid);
668   llvm::DenseSet<const VarDecl *> CopiedVars;
669   llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr;
670   for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) {
671     auto IRef = C->varlist_begin();
672     auto ISrcRef = C->source_exprs().begin();
673     auto IDestRef = C->destination_exprs().begin();
674     for (auto *AssignOp : C->assignment_ops()) {
675       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
676       QualType Type = VD->getType();
677       if (CopiedVars.insert(VD->getCanonicalDecl()).second) {
678         // Get the address of the master variable. If we are emitting code with
679         // TLS support, the address is passed from the master as field in the
680         // captured declaration.
681         Address MasterAddr = Address::invalid();
682         if (getLangOpts().OpenMPUseTLS &&
683             getContext().getTargetInfo().isTLSSupported()) {
684           assert(CapturedStmtInfo->lookup(VD) &&
685                  "Copyin threadprivates should have been captured!");
686           DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(),
687                           VK_LValue, (*IRef)->getExprLoc());
688           MasterAddr = EmitLValue(&DRE).getAddress();
689           LocalDeclMap.erase(VD);
690         } else {
691           MasterAddr =
692             Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD)
693                                         : CGM.GetAddrOfGlobal(VD),
694                     getContext().getDeclAlign(VD));
695         }
696         // Get the address of the threadprivate variable.
697         Address PrivateAddr = EmitLValue(*IRef).getAddress();
698         if (CopiedVars.size() == 1) {
699           // At first check if current thread is a master thread. If it is, no
700           // need to copy data.
701           CopyBegin = createBasicBlock("copyin.not.master");
702           CopyEnd = createBasicBlock("copyin.not.master.end");
703           Builder.CreateCondBr(
704               Builder.CreateICmpNE(
705                   Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy),
706                   Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)),
707               CopyBegin, CopyEnd);
708           EmitBlock(CopyBegin);
709         }
710         auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
711         auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
712         EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp);
713       }
714       ++IRef;
715       ++ISrcRef;
716       ++IDestRef;
717     }
718   }
719   if (CopyEnd) {
720     // Exit out of copying procedure for non-master thread.
721     EmitBlock(CopyEnd, /*IsFinished=*/true);
722     return true;
723   }
724   return false;
725 }
726 
727 bool CodeGenFunction::EmitOMPLastprivateClauseInit(
728     const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) {
729   if (!HaveInsertPoint())
730     return false;
731   bool HasAtLeastOneLastprivate = false;
732   llvm::DenseSet<const VarDecl *> SIMDLCVs;
733   if (isOpenMPSimdDirective(D.getDirectiveKind())) {
734     auto *LoopDirective = cast<OMPLoopDirective>(&D);
735     for (auto *C : LoopDirective->counters()) {
736       SIMDLCVs.insert(
737           cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
738     }
739   }
740   llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
741   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
742     HasAtLeastOneLastprivate = true;
743     auto IRef = C->varlist_begin();
744     auto IDestRef = C->destination_exprs().begin();
745     for (auto *IInit : C->private_copies()) {
746       // Keep the address of the original variable for future update at the end
747       // of the loop.
748       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
749       if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) {
750         auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
751         PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address {
752           DeclRefExpr DRE(
753               const_cast<VarDecl *>(OrigVD),
754               /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup(
755                   OrigVD) != nullptr,
756               (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc());
757           return EmitLValue(&DRE).getAddress();
758         });
759         // Check if the variable is also a firstprivate: in this case IInit is
760         // not generated. Initialization of this variable will happen in codegen
761         // for 'firstprivate' clause.
762         if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) {
763           auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl());
764           bool IsRegistered =
765               PrivateScope.addPrivate(OrigVD, [&]() -> Address {
766                 // Emit private VarDecl with copy init.
767                 EmitDecl(*VD);
768                 return GetAddrOfLocalVar(VD);
769               });
770           assert(IsRegistered &&
771                  "lastprivate var already registered as private");
772           (void)IsRegistered;
773         }
774       }
775       ++IRef;
776       ++IDestRef;
777     }
778   }
779   return HasAtLeastOneLastprivate;
780 }
781 
782 void CodeGenFunction::EmitOMPLastprivateClauseFinal(
783     const OMPExecutableDirective &D, bool NoFinals,
784     llvm::Value *IsLastIterCond) {
785   if (!HaveInsertPoint())
786     return;
787   // Emit following code:
788   // if (<IsLastIterCond>) {
789   //   orig_var1 = private_orig_var1;
790   //   ...
791   //   orig_varn = private_orig_varn;
792   // }
793   llvm::BasicBlock *ThenBB = nullptr;
794   llvm::BasicBlock *DoneBB = nullptr;
795   if (IsLastIterCond) {
796     ThenBB = createBasicBlock(".omp.lastprivate.then");
797     DoneBB = createBasicBlock(".omp.lastprivate.done");
798     Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB);
799     EmitBlock(ThenBB);
800   }
801   llvm::DenseSet<const VarDecl *> AlreadyEmittedVars;
802   llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates;
803   if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) {
804     auto IC = LoopDirective->counters().begin();
805     for (auto F : LoopDirective->finals()) {
806       auto *D =
807           cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl();
808       if (NoFinals)
809         AlreadyEmittedVars.insert(D);
810       else
811         LoopCountersAndUpdates[D] = F;
812       ++IC;
813     }
814   }
815   for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) {
816     auto IRef = C->varlist_begin();
817     auto ISrcRef = C->source_exprs().begin();
818     auto IDestRef = C->destination_exprs().begin();
819     for (auto *AssignOp : C->assignment_ops()) {
820       auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
821       QualType Type = PrivateVD->getType();
822       auto *CanonicalVD = PrivateVD->getCanonicalDecl();
823       if (AlreadyEmittedVars.insert(CanonicalVD).second) {
824         // If lastprivate variable is a loop control variable for loop-based
825         // directive, update its value before copyin back to original
826         // variable.
827         if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD))
828           EmitIgnoredExpr(FinalExpr);
829         auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl());
830         auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl());
831         // Get the address of the original variable.
832         Address OriginalAddr = GetAddrOfLocalVar(DestVD);
833         // Get the address of the private variable.
834         Address PrivateAddr = GetAddrOfLocalVar(PrivateVD);
835         if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>())
836           PrivateAddr =
837               Address(Builder.CreateLoad(PrivateAddr),
838                       getNaturalTypeAlignment(RefTy->getPointeeType()));
839         EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp);
840       }
841       ++IRef;
842       ++ISrcRef;
843       ++IDestRef;
844     }
845     if (auto *PostUpdate = C->getPostUpdateExpr())
846       EmitIgnoredExpr(PostUpdate);
847   }
848   if (IsLastIterCond)
849     EmitBlock(DoneBB, /*IsFinished=*/true);
850 }
851 
852 static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
853                           LValue BaseLV, llvm::Value *Addr) {
854   Address Tmp = Address::invalid();
855   Address TopTmp = Address::invalid();
856   Address MostTopTmp = Address::invalid();
857   BaseTy = BaseTy.getNonReferenceType();
858   while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
859          !CGF.getContext().hasSameType(BaseTy, ElTy)) {
860     Tmp = CGF.CreateMemTemp(BaseTy);
861     if (TopTmp.isValid())
862       CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp);
863     else
864       MostTopTmp = Tmp;
865     TopTmp = Tmp;
866     BaseTy = BaseTy->getPointeeType();
867   }
868   llvm::Type *Ty = BaseLV.getPointer()->getType();
869   if (Tmp.isValid())
870     Ty = Tmp.getElementType();
871   Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty);
872   if (Tmp.isValid()) {
873     CGF.Builder.CreateStore(Addr, Tmp);
874     return MostTopTmp;
875   }
876   return Address(Addr, BaseLV.getAlignment());
877 }
878 
879 static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
880                           LValue BaseLV) {
881   BaseTy = BaseTy.getNonReferenceType();
882   while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) &&
883          !CGF.getContext().hasSameType(BaseTy, ElTy)) {
884     if (auto *PtrTy = BaseTy->getAs<PointerType>())
885       BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy);
886     else {
887       BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(),
888                                              BaseTy->castAs<ReferenceType>());
889     }
890     BaseTy = BaseTy->getPointeeType();
891   }
892   return CGF.MakeAddrLValue(
893       Address(
894           CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
895               BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()),
896           BaseLV.getAlignment()),
897       BaseLV.getType(), BaseLV.getAlignmentSource());
898 }
899 
900 void CodeGenFunction::EmitOMPReductionClauseInit(
901     const OMPExecutableDirective &D,
902     CodeGenFunction::OMPPrivateScope &PrivateScope) {
903   if (!HaveInsertPoint())
904     return;
905   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
906     auto ILHS = C->lhs_exprs().begin();
907     auto IRHS = C->rhs_exprs().begin();
908     auto IPriv = C->privates().begin();
909     auto IRed = C->reduction_ops().begin();
910     for (auto IRef : C->varlists()) {
911       auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl());
912       auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl());
913       auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl());
914       auto *DRD = getReductionInit(*IRed);
915       if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) {
916         auto *Base = OASE->getBase()->IgnoreParenImpCasts();
917         while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
918           Base = TempOASE->getBase()->IgnoreParenImpCasts();
919         while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
920           Base = TempASE->getBase()->IgnoreParenImpCasts();
921         auto *DE = cast<DeclRefExpr>(Base);
922         auto *OrigVD = cast<VarDecl>(DE->getDecl());
923         auto OASELValueLB = EmitOMPArraySectionExpr(OASE);
924         auto OASELValueUB =
925             EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false);
926         auto OriginalBaseLValue = EmitLValue(DE);
927         LValue BaseLValue =
928             loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(),
929                         OriginalBaseLValue);
930         // Store the address of the original variable associated with the LHS
931         // implicit variable.
932         PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address {
933           return OASELValueLB.getAddress();
934         });
935         // Emit reduction copy.
936         bool IsRegistered = PrivateScope.addPrivate(
937             OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB,
938                      OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address {
939               // Emit VarDecl with copy init for arrays.
940               // Get the address of the original variable captured in current
941               // captured region.
942               auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(),
943                                                  OASELValueLB.getPointer());
944               Size = Builder.CreateNUWAdd(
945                   Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1));
946               CodeGenFunction::OpaqueValueMapping OpaqueMap(
947                   *this, cast<OpaqueValueExpr>(
948                              getContext()
949                                  .getAsVariableArrayType(PrivateVD->getType())
950                                  ->getSizeExpr()),
951                   RValue::get(Size));
952               EmitVariablyModifiedType(PrivateVD->getType());
953               auto Emission = EmitAutoVarAlloca(*PrivateVD);
954               auto Addr = Emission.getAllocatedAddress();
955               auto *Init = PrivateVD->getInit();
956               EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
957                                    DRD ? *IRed : Init,
958                                    OASELValueLB.getAddress());
959               EmitAutoVarCleanups(Emission);
960               // Emit private VarDecl with reduction init.
961               auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
962                                                    OASELValueLB.getPointer());
963               auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
964               return castToBase(*this, OrigVD->getType(),
965                                 OASELValueLB.getType(), OriginalBaseLValue,
966                                 Ptr);
967             });
968         assert(IsRegistered && "private var already registered as private");
969         // Silence the warning about unused variable.
970         (void)IsRegistered;
971         PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
972           return GetAddrOfLocalVar(PrivateVD);
973         });
974       } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) {
975         auto *Base = ASE->getBase()->IgnoreParenImpCasts();
976         while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
977           Base = TempASE->getBase()->IgnoreParenImpCasts();
978         auto *DE = cast<DeclRefExpr>(Base);
979         auto *OrigVD = cast<VarDecl>(DE->getDecl());
980         auto ASELValue = EmitLValue(ASE);
981         auto OriginalBaseLValue = EmitLValue(DE);
982         LValue BaseLValue = loadToBegin(
983             *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue);
984         // Store the address of the original variable associated with the LHS
985         // implicit variable.
986         PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address {
987           return ASELValue.getAddress();
988         });
989         // Emit reduction copy.
990         bool IsRegistered = PrivateScope.addPrivate(
991             OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue,
992                      OriginalBaseLValue, DRD, IRed]() -> Address {
993               // Emit private VarDecl with reduction init.
994               AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
995               auto Addr = Emission.getAllocatedAddress();
996               if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
997                 emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
998                                                  ASELValue.getAddress(),
999                                                  ASELValue.getType());
1000               } else
1001                 EmitAutoVarInit(Emission);
1002               EmitAutoVarCleanups(Emission);
1003               auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(),
1004                                                    ASELValue.getPointer());
1005               auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset);
1006               return castToBase(*this, OrigVD->getType(), ASELValue.getType(),
1007                                 OriginalBaseLValue, Ptr);
1008             });
1009         assert(IsRegistered && "private var already registered as private");
1010         // Silence the warning about unused variable.
1011         (void)IsRegistered;
1012         PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1013           return Builder.CreateElementBitCast(
1014               GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()),
1015               "rhs.begin");
1016         });
1017       } else {
1018         auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl());
1019         QualType Type = PrivateVD->getType();
1020         if (getContext().getAsArrayType(Type)) {
1021           // Store the address of the original variable associated with the LHS
1022           // implicit variable.
1023           DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1024                           CapturedStmtInfo->lookup(OrigVD) != nullptr,
1025                           IRef->getType(), VK_LValue, IRef->getExprLoc());
1026           Address OriginalAddr = EmitLValue(&DRE).getAddress();
1027           PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr,
1028                                           LHSVD]() -> Address {
1029             OriginalAddr = Builder.CreateElementBitCast(
1030                 OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin");
1031             return OriginalAddr;
1032           });
1033           bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address {
1034             if (Type->isVariablyModifiedType()) {
1035               CodeGenFunction::OpaqueValueMapping OpaqueMap(
1036                   *this, cast<OpaqueValueExpr>(
1037                              getContext()
1038                                  .getAsVariableArrayType(PrivateVD->getType())
1039                                  ->getSizeExpr()),
1040                   RValue::get(
1041                       getTypeSize(OrigVD->getType().getNonReferenceType())));
1042               EmitVariablyModifiedType(Type);
1043             }
1044             auto Emission = EmitAutoVarAlloca(*PrivateVD);
1045             auto Addr = Emission.getAllocatedAddress();
1046             auto *Init = PrivateVD->getInit();
1047             EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(),
1048                                  DRD ? *IRed : Init, OriginalAddr);
1049             EmitAutoVarCleanups(Emission);
1050             return Emission.getAllocatedAddress();
1051           });
1052           assert(IsRegistered && "private var already registered as private");
1053           // Silence the warning about unused variable.
1054           (void)IsRegistered;
1055           PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address {
1056             return Builder.CreateElementBitCast(
1057                 GetAddrOfLocalVar(PrivateVD),
1058                 ConvertTypeForMem(RHSVD->getType()), "rhs.begin");
1059           });
1060         } else {
1061           // Store the address of the original variable associated with the LHS
1062           // implicit variable.
1063           Address OriginalAddr = Address::invalid();
1064           PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef,
1065                                           &OriginalAddr]() -> Address {
1066             DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1067                             CapturedStmtInfo->lookup(OrigVD) != nullptr,
1068                             IRef->getType(), VK_LValue, IRef->getExprLoc());
1069             OriginalAddr = EmitLValue(&DRE).getAddress();
1070             return OriginalAddr;
1071           });
1072           // Emit reduction copy.
1073           bool IsRegistered = PrivateScope.addPrivate(
1074               OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address {
1075                 // Emit private VarDecl with reduction init.
1076                 AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD);
1077                 auto Addr = Emission.getAllocatedAddress();
1078                 if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) {
1079                   emitInitWithReductionInitializer(*this, DRD, *IRed, Addr,
1080                                                    OriginalAddr,
1081                                                    PrivateVD->getType());
1082                 } else
1083                   EmitAutoVarInit(Emission);
1084                 EmitAutoVarCleanups(Emission);
1085                 return Addr;
1086               });
1087           assert(IsRegistered && "private var already registered as private");
1088           // Silence the warning about unused variable.
1089           (void)IsRegistered;
1090           PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address {
1091             return GetAddrOfLocalVar(PrivateVD);
1092           });
1093         }
1094       }
1095       ++ILHS;
1096       ++IRHS;
1097       ++IPriv;
1098       ++IRed;
1099     }
1100   }
1101 }
1102 
1103 void CodeGenFunction::EmitOMPReductionClauseFinal(
1104     const OMPExecutableDirective &D) {
1105   if (!HaveInsertPoint())
1106     return;
1107   llvm::SmallVector<const Expr *, 8> Privates;
1108   llvm::SmallVector<const Expr *, 8> LHSExprs;
1109   llvm::SmallVector<const Expr *, 8> RHSExprs;
1110   llvm::SmallVector<const Expr *, 8> ReductionOps;
1111   bool HasAtLeastOneReduction = false;
1112   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1113     HasAtLeastOneReduction = true;
1114     Privates.append(C->privates().begin(), C->privates().end());
1115     LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end());
1116     RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end());
1117     ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end());
1118   }
1119   if (HasAtLeastOneReduction) {
1120     // Emit nowait reduction if nowait clause is present or directive is a
1121     // parallel directive (it always has implicit barrier).
1122     CGM.getOpenMPRuntime().emitReduction(
1123         *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps,
1124         D.getSingleClause<OMPNowaitClause>() ||
1125             isOpenMPParallelDirective(D.getDirectiveKind()) ||
1126             D.getDirectiveKind() == OMPD_simd,
1127         D.getDirectiveKind() == OMPD_simd);
1128   }
1129 }
1130 
1131 static void emitPostUpdateForReductionClause(
1132     CodeGenFunction &CGF, const OMPExecutableDirective &D,
1133     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1134   if (!CGF.HaveInsertPoint())
1135     return;
1136   llvm::BasicBlock *DoneBB = nullptr;
1137   for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) {
1138     if (auto *PostUpdate = C->getPostUpdateExpr()) {
1139       if (!DoneBB) {
1140         if (auto *Cond = CondGen(CGF)) {
1141           // If the first post-update expression is found, emit conditional
1142           // block if it was requested.
1143           auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu");
1144           DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done");
1145           CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1146           CGF.EmitBlock(ThenBB);
1147         }
1148       }
1149       CGF.EmitIgnoredExpr(PostUpdate);
1150     }
1151   }
1152   if (DoneBB)
1153     CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
1154 }
1155 
1156 static void emitCommonOMPParallelDirective(CodeGenFunction &CGF,
1157                                            const OMPExecutableDirective &S,
1158                                            OpenMPDirectiveKind InnermostKind,
1159                                            const RegionCodeGenTy &CodeGen) {
1160   auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
1161   auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
1162       emitParallelOrTeamsOutlinedFunction(S,
1163           *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
1164   if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) {
1165     CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
1166     auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
1167                                          /*IgnoreResultAssign*/ true);
1168     CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1169         CGF, NumThreads, NumThreadsClause->getLocStart());
1170   }
1171   if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
1172     CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);
1173     CGF.CGM.getOpenMPRuntime().emitProcBindClause(
1174         CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart());
1175   }
1176   const Expr *IfCond = nullptr;
1177   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
1178     if (C->getNameModifier() == OMPD_unknown ||
1179         C->getNameModifier() == OMPD_parallel) {
1180       IfCond = C->getCondition();
1181       break;
1182     }
1183   }
1184 
1185   OMPLexicalScope Scope(CGF, S);
1186   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
1187   CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
1188   CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn,
1189                                               CapturedVars, IfCond);
1190 }
1191 
1192 void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
1193   // Emit parallel region as a standalone region.
1194   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1195     OMPPrivateScope PrivateScope(CGF);
1196     bool Copyins = CGF.EmitOMPCopyinClause(S);
1197     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
1198     if (Copyins) {
1199       // Emit implicit barrier to synchronize threads and avoid data races on
1200       // propagation master's thread values of threadprivate variables to local
1201       // instances of that variables of all other implicit threads.
1202       CGF.CGM.getOpenMPRuntime().emitBarrierCall(
1203           CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1204           /*ForceSimpleCall=*/true);
1205     }
1206     CGF.EmitOMPPrivateClause(S, PrivateScope);
1207     CGF.EmitOMPReductionClauseInit(S, PrivateScope);
1208     (void)PrivateScope.Privatize();
1209     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
1210     CGF.EmitOMPReductionClauseFinal(S);
1211   };
1212   emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen);
1213   emitPostUpdateForReductionClause(
1214       *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
1215 }
1216 
1217 void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D,
1218                                       JumpDest LoopExit) {
1219   RunCleanupsScope BodyScope(*this);
1220   // Update counters values on current iteration.
1221   for (auto I : D.updates()) {
1222     EmitIgnoredExpr(I);
1223   }
1224   // Update the linear variables.
1225   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1226     for (auto *U : C->updates())
1227       EmitIgnoredExpr(U);
1228   }
1229 
1230   // On a continue in the body, jump to the end.
1231   auto Continue = getJumpDestInCurrentScope("omp.body.continue");
1232   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1233   // Emit loop body.
1234   EmitStmt(D.getBody());
1235   // The end (updates/cleanups).
1236   EmitBlock(Continue.getBlock());
1237   BreakContinueStack.pop_back();
1238 }
1239 
1240 void CodeGenFunction::EmitOMPInnerLoop(
1241     const Stmt &S, bool RequiresCleanup, const Expr *LoopCond,
1242     const Expr *IncExpr,
1243     const llvm::function_ref<void(CodeGenFunction &)> &BodyGen,
1244     const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) {
1245   auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end");
1246 
1247   // Start the loop with a block that tests the condition.
1248   auto CondBlock = createBasicBlock("omp.inner.for.cond");
1249   EmitBlock(CondBlock);
1250   LoopStack.push(CondBlock);
1251 
1252   // If there are any cleanups between here and the loop-exit scope,
1253   // create a block to stage a loop exit along.
1254   auto ExitBlock = LoopExit.getBlock();
1255   if (RequiresCleanup)
1256     ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup");
1257 
1258   auto LoopBody = createBasicBlock("omp.inner.for.body");
1259 
1260   // Emit condition.
1261   EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S));
1262   if (ExitBlock != LoopExit.getBlock()) {
1263     EmitBlock(ExitBlock);
1264     EmitBranchThroughCleanup(LoopExit);
1265   }
1266 
1267   EmitBlock(LoopBody);
1268   incrementProfileCounter(&S);
1269 
1270   // Create a block for the increment.
1271   auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc");
1272   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1273 
1274   BodyGen(*this);
1275 
1276   // Emit "IV = IV + 1" and a back-edge to the condition block.
1277   EmitBlock(Continue.getBlock());
1278   EmitIgnoredExpr(IncExpr);
1279   PostIncGen(*this);
1280   BreakContinueStack.pop_back();
1281   EmitBranch(CondBlock);
1282   LoopStack.pop();
1283   // Emit the fall-through block.
1284   EmitBlock(LoopExit.getBlock());
1285 }
1286 
1287 void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) {
1288   if (!HaveInsertPoint())
1289     return;
1290   // Emit inits for the linear variables.
1291   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1292     for (auto *Init : C->inits()) {
1293       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl());
1294       if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) {
1295         AutoVarEmission Emission = EmitAutoVarAlloca(*VD);
1296         auto *OrigVD = cast<VarDecl>(Ref->getDecl());
1297         DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1298                         CapturedStmtInfo->lookup(OrigVD) != nullptr,
1299                         VD->getInit()->getType(), VK_LValue,
1300                         VD->getInit()->getExprLoc());
1301         EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(),
1302                                                 VD->getType()),
1303                        /*capturedByInit=*/false);
1304         EmitAutoVarCleanups(Emission);
1305       } else
1306         EmitVarDecl(*VD);
1307     }
1308     // Emit the linear steps for the linear clauses.
1309     // If a step is not constant, it is pre-calculated before the loop.
1310     if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep()))
1311       if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) {
1312         EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl()));
1313         // Emit calculation of the linear step.
1314         EmitIgnoredExpr(CS);
1315       }
1316   }
1317 }
1318 
1319 void CodeGenFunction::EmitOMPLinearClauseFinal(
1320     const OMPLoopDirective &D,
1321     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1322   if (!HaveInsertPoint())
1323     return;
1324   llvm::BasicBlock *DoneBB = nullptr;
1325   // Emit the final values of the linear variables.
1326   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1327     auto IC = C->varlist_begin();
1328     for (auto *F : C->finals()) {
1329       if (!DoneBB) {
1330         if (auto *Cond = CondGen(*this)) {
1331           // If the first post-update expression is found, emit conditional
1332           // block if it was requested.
1333           auto *ThenBB = createBasicBlock(".omp.linear.pu");
1334           DoneBB = createBasicBlock(".omp.linear.pu.done");
1335           Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1336           EmitBlock(ThenBB);
1337         }
1338       }
1339       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl());
1340       DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD),
1341                       CapturedStmtInfo->lookup(OrigVD) != nullptr,
1342                       (*IC)->getType(), VK_LValue, (*IC)->getExprLoc());
1343       Address OrigAddr = EmitLValue(&DRE).getAddress();
1344       CodeGenFunction::OMPPrivateScope VarScope(*this);
1345       VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; });
1346       (void)VarScope.Privatize();
1347       EmitIgnoredExpr(F);
1348       ++IC;
1349     }
1350     if (auto *PostUpdate = C->getPostUpdateExpr())
1351       EmitIgnoredExpr(PostUpdate);
1352   }
1353   if (DoneBB)
1354     EmitBlock(DoneBB, /*IsFinished=*/true);
1355 }
1356 
1357 static void emitAlignedClause(CodeGenFunction &CGF,
1358                               const OMPExecutableDirective &D) {
1359   if (!CGF.HaveInsertPoint())
1360     return;
1361   for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) {
1362     unsigned ClauseAlignment = 0;
1363     if (auto AlignmentExpr = Clause->getAlignment()) {
1364       auto AlignmentCI =
1365           cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
1366       ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
1367     }
1368     for (auto E : Clause->varlists()) {
1369       unsigned Alignment = ClauseAlignment;
1370       if (Alignment == 0) {
1371         // OpenMP [2.8.1, Description]
1372         // If no optional parameter is specified, implementation-defined default
1373         // alignments for SIMD instructions on the target platforms are assumed.
1374         Alignment =
1375             CGF.getContext()
1376                 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign(
1377                     E->getType()->getPointeeType()))
1378                 .getQuantity();
1379       }
1380       assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
1381              "alignment is not power of 2");
1382       if (Alignment != 0) {
1383         llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
1384         CGF.EmitAlignmentAssumption(PtrValue, Alignment);
1385       }
1386     }
1387   }
1388 }
1389 
1390 void CodeGenFunction::EmitOMPPrivateLoopCounters(
1391     const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) {
1392   if (!HaveInsertPoint())
1393     return;
1394   auto I = S.private_counters().begin();
1395   for (auto *E : S.counters()) {
1396     auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1397     auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl());
1398     (void)LoopScope.addPrivate(VD, [&]() -> Address {
1399       // Emit var without initialization.
1400       if (!LocalDeclMap.count(PrivateVD)) {
1401         auto VarEmission = EmitAutoVarAlloca(*PrivateVD);
1402         EmitAutoVarCleanups(VarEmission);
1403       }
1404       DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1405                       /*RefersToEnclosingVariableOrCapture=*/false,
1406                       (*I)->getType(), VK_LValue, (*I)->getExprLoc());
1407       return EmitLValue(&DRE).getAddress();
1408     });
1409     if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) ||
1410         VD->hasGlobalStorage()) {
1411       (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address {
1412         DeclRefExpr DRE(const_cast<VarDecl *>(VD),
1413                         LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD),
1414                         E->getType(), VK_LValue, E->getExprLoc());
1415         return EmitLValue(&DRE).getAddress();
1416       });
1417     }
1418     ++I;
1419   }
1420 }
1421 
1422 static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S,
1423                         const Expr *Cond, llvm::BasicBlock *TrueBlock,
1424                         llvm::BasicBlock *FalseBlock, uint64_t TrueCount) {
1425   if (!CGF.HaveInsertPoint())
1426     return;
1427   {
1428     CodeGenFunction::OMPPrivateScope PreCondScope(CGF);
1429     CGF.EmitOMPPrivateLoopCounters(S, PreCondScope);
1430     (void)PreCondScope.Privatize();
1431     // Get initial values of real counters.
1432     for (auto I : S.inits()) {
1433       CGF.EmitIgnoredExpr(I);
1434     }
1435   }
1436   // Check that loop is executed at least one time.
1437   CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount);
1438 }
1439 
1440 void CodeGenFunction::EmitOMPLinearClause(
1441     const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) {
1442   if (!HaveInsertPoint())
1443     return;
1444   llvm::DenseSet<const VarDecl *> SIMDLCVs;
1445   if (isOpenMPSimdDirective(D.getDirectiveKind())) {
1446     auto *LoopDirective = cast<OMPLoopDirective>(&D);
1447     for (auto *C : LoopDirective->counters()) {
1448       SIMDLCVs.insert(
1449           cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl());
1450     }
1451   }
1452   for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) {
1453     auto CurPrivate = C->privates().begin();
1454     for (auto *E : C->varlists()) {
1455       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
1456       auto *PrivateVD =
1457           cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl());
1458       if (!SIMDLCVs.count(VD->getCanonicalDecl())) {
1459         bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address {
1460           // Emit private VarDecl with copy init.
1461           EmitVarDecl(*PrivateVD);
1462           return GetAddrOfLocalVar(PrivateVD);
1463         });
1464         assert(IsRegistered && "linear var already registered as private");
1465         // Silence the warning about unused variable.
1466         (void)IsRegistered;
1467       } else
1468         EmitVarDecl(*PrivateVD);
1469       ++CurPrivate;
1470     }
1471   }
1472 }
1473 
1474 static void emitSimdlenSafelenClause(CodeGenFunction &CGF,
1475                                      const OMPExecutableDirective &D,
1476                                      bool IsMonotonic) {
1477   if (!CGF.HaveInsertPoint())
1478     return;
1479   if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) {
1480     RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(),
1481                                  /*ignoreResult=*/true);
1482     llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1483     CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1484     // In presence of finite 'safelen', it may be unsafe to mark all
1485     // the memory instructions parallel, because loop-carried
1486     // dependences of 'safelen' iterations are possible.
1487     if (!IsMonotonic)
1488       CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>());
1489   } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) {
1490     RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(),
1491                                  /*ignoreResult=*/true);
1492     llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
1493     CGF.LoopStack.setVectorizeWidth(Val->getZExtValue());
1494     // In presence of finite 'safelen', it may be unsafe to mark all
1495     // the memory instructions parallel, because loop-carried
1496     // dependences of 'safelen' iterations are possible.
1497     CGF.LoopStack.setParallel(false);
1498   }
1499 }
1500 
1501 void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D,
1502                                       bool IsMonotonic) {
1503   // Walk clauses and process safelen/lastprivate.
1504   LoopStack.setParallel(!IsMonotonic);
1505   LoopStack.setVectorizeEnable(true);
1506   emitSimdlenSafelenClause(*this, D, IsMonotonic);
1507 }
1508 
1509 void CodeGenFunction::EmitOMPSimdFinal(
1510     const OMPLoopDirective &D,
1511     const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) {
1512   if (!HaveInsertPoint())
1513     return;
1514   llvm::BasicBlock *DoneBB = nullptr;
1515   auto IC = D.counters().begin();
1516   auto IPC = D.private_counters().begin();
1517   for (auto F : D.finals()) {
1518     auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl());
1519     auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl());
1520     auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD);
1521     if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) ||
1522         OrigVD->hasGlobalStorage() || CED) {
1523       if (!DoneBB) {
1524         if (auto *Cond = CondGen(*this)) {
1525           // If the first post-update expression is found, emit conditional
1526           // block if it was requested.
1527           auto *ThenBB = createBasicBlock(".omp.final.then");
1528           DoneBB = createBasicBlock(".omp.final.done");
1529           Builder.CreateCondBr(Cond, ThenBB, DoneBB);
1530           EmitBlock(ThenBB);
1531         }
1532       }
1533       Address OrigAddr = Address::invalid();
1534       if (CED)
1535         OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress();
1536       else {
1537         DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD),
1538                         /*RefersToEnclosingVariableOrCapture=*/false,
1539                         (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc());
1540         OrigAddr = EmitLValue(&DRE).getAddress();
1541       }
1542       OMPPrivateScope VarScope(*this);
1543       VarScope.addPrivate(OrigVD,
1544                           [OrigAddr]() -> Address { return OrigAddr; });
1545       (void)VarScope.Privatize();
1546       EmitIgnoredExpr(F);
1547     }
1548     ++IC;
1549     ++IPC;
1550   }
1551   if (DoneBB)
1552     EmitBlock(DoneBB, /*IsFinished=*/true);
1553 }
1554 
1555 void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
1556   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
1557     OMPLoopScope PreInitScope(CGF, S);
1558     // if (PreCond) {
1559     //   for (IV in 0..LastIteration) BODY;
1560     //   <Final counter/linear vars updates>;
1561     // }
1562     //
1563 
1564     // Emit: if (PreCond) - begin.
1565     // If the condition constant folds and can be elided, avoid emitting the
1566     // whole loop.
1567     bool CondConstant;
1568     llvm::BasicBlock *ContBlock = nullptr;
1569     if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1570       if (!CondConstant)
1571         return;
1572     } else {
1573       auto *ThenBlock = CGF.createBasicBlock("simd.if.then");
1574       ContBlock = CGF.createBasicBlock("simd.if.end");
1575       emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
1576                   CGF.getProfileCount(&S));
1577       CGF.EmitBlock(ThenBlock);
1578       CGF.incrementProfileCounter(&S);
1579     }
1580 
1581     // Emit the loop iteration variable.
1582     const Expr *IVExpr = S.getIterationVariable();
1583     const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
1584     CGF.EmitVarDecl(*IVDecl);
1585     CGF.EmitIgnoredExpr(S.getInit());
1586 
1587     // Emit the iterations count variable.
1588     // If it is not a variable, Sema decided to calculate iterations count on
1589     // each iteration (e.g., it is foldable into a constant).
1590     if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1591       CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1592       // Emit calculation of the iterations count.
1593       CGF.EmitIgnoredExpr(S.getCalcLastIteration());
1594     }
1595 
1596     CGF.EmitOMPSimdInit(S);
1597 
1598     emitAlignedClause(CGF, S);
1599     CGF.EmitOMPLinearClauseInit(S);
1600     {
1601       OMPPrivateScope LoopScope(CGF);
1602       CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
1603       CGF.EmitOMPLinearClause(S, LoopScope);
1604       CGF.EmitOMPPrivateClause(S, LoopScope);
1605       CGF.EmitOMPReductionClauseInit(S, LoopScope);
1606       bool HasLastprivateClause =
1607           CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
1608       (void)LoopScope.Privatize();
1609       CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1610                            S.getInc(),
1611                            [&S](CodeGenFunction &CGF) {
1612                              CGF.EmitOMPLoopBody(S, JumpDest());
1613                              CGF.EmitStopPoint(&S);
1614                            },
1615                            [](CodeGenFunction &) {});
1616       CGF.EmitOMPSimdFinal(
1617           S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
1618       // Emit final copy of the lastprivate variables at the end of loops.
1619       if (HasLastprivateClause)
1620         CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true);
1621       CGF.EmitOMPReductionClauseFinal(S);
1622       emitPostUpdateForReductionClause(
1623           CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
1624     }
1625     CGF.EmitOMPLinearClauseFinal(
1626         S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; });
1627     // Emit: if (PreCond) - end.
1628     if (ContBlock) {
1629       CGF.EmitBranch(ContBlock);
1630       CGF.EmitBlock(ContBlock, true);
1631     }
1632   };
1633   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
1634   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
1635 }
1636 
1637 void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic,
1638     const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1639     Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1640   auto &RT = CGM.getOpenMPRuntime();
1641 
1642   const Expr *IVExpr = S.getIterationVariable();
1643   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1644   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1645 
1646   auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end");
1647 
1648   // Start the loop with a block that tests the condition.
1649   auto CondBlock = createBasicBlock("omp.dispatch.cond");
1650   EmitBlock(CondBlock);
1651   LoopStack.push(CondBlock);
1652 
1653   llvm::Value *BoolCondVal = nullptr;
1654   if (!DynamicOrOrdered) {
1655     // UB = min(UB, GlobalUB)
1656     EmitIgnoredExpr(S.getEnsureUpperBound());
1657     // IV = LB
1658     EmitIgnoredExpr(S.getInit());
1659     // IV < UB
1660     BoolCondVal = EvaluateExprAsBool(S.getCond());
1661   } else {
1662     BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, IL,
1663                                  LB, UB, ST);
1664   }
1665 
1666   // If there are any cleanups between here and the loop-exit scope,
1667   // create a block to stage a loop exit along.
1668   auto ExitBlock = LoopExit.getBlock();
1669   if (LoopScope.requiresCleanups())
1670     ExitBlock = createBasicBlock("omp.dispatch.cleanup");
1671 
1672   auto LoopBody = createBasicBlock("omp.dispatch.body");
1673   Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
1674   if (ExitBlock != LoopExit.getBlock()) {
1675     EmitBlock(ExitBlock);
1676     EmitBranchThroughCleanup(LoopExit);
1677   }
1678   EmitBlock(LoopBody);
1679 
1680   // Emit "IV = LB" (in case of static schedule, we have already calculated new
1681   // LB for loop condition and emitted it above).
1682   if (DynamicOrOrdered)
1683     EmitIgnoredExpr(S.getInit());
1684 
1685   // Create a block for the increment.
1686   auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc");
1687   BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1688 
1689   // Generate !llvm.loop.parallel metadata for loads and stores for loops
1690   // with dynamic/guided scheduling and without ordered clause.
1691   if (!isOpenMPSimdDirective(S.getDirectiveKind()))
1692     LoopStack.setParallel(!IsMonotonic);
1693   else
1694     EmitOMPSimdInit(S, IsMonotonic);
1695 
1696   SourceLocation Loc = S.getLocStart();
1697   EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(),
1698                    [&S, LoopExit](CodeGenFunction &CGF) {
1699                      CGF.EmitOMPLoopBody(S, LoopExit);
1700                      CGF.EmitStopPoint(&S);
1701                    },
1702                    [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) {
1703                      if (Ordered) {
1704                        CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(
1705                            CGF, Loc, IVSize, IVSigned);
1706                      }
1707                    });
1708 
1709   EmitBlock(Continue.getBlock());
1710   BreakContinueStack.pop_back();
1711   if (!DynamicOrOrdered) {
1712     // Emit "LB = LB + Stride", "UB = UB + Stride".
1713     EmitIgnoredExpr(S.getNextLowerBound());
1714     EmitIgnoredExpr(S.getNextUpperBound());
1715   }
1716 
1717   EmitBranch(CondBlock);
1718   LoopStack.pop();
1719   // Emit the fall-through block.
1720   EmitBlock(LoopExit.getBlock());
1721 
1722   // Tell the runtime we are done.
1723   if (!DynamicOrOrdered)
1724     RT.emitForStaticFinish(*this, S.getLocEnd());
1725 
1726 }
1727 
1728 void CodeGenFunction::EmitOMPForOuterLoop(
1729     OpenMPScheduleClauseKind ScheduleKind, bool IsMonotonic,
1730     const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered,
1731     Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1732   auto &RT = CGM.getOpenMPRuntime();
1733 
1734   // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime).
1735   const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind);
1736 
1737   assert((Ordered ||
1738           !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) &&
1739          "static non-chunked schedule does not need outer loop");
1740 
1741   // Emit outer loop.
1742   //
1743   // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1744   // When schedule(dynamic,chunk_size) is specified, the iterations are
1745   // distributed to threads in the team in chunks as the threads request them.
1746   // Each thread executes a chunk of iterations, then requests another chunk,
1747   // until no chunks remain to be distributed. Each chunk contains chunk_size
1748   // iterations, except for the last chunk to be distributed, which may have
1749   // fewer iterations. When no chunk_size is specified, it defaults to 1.
1750   //
1751   // When schedule(guided,chunk_size) is specified, the iterations are assigned
1752   // to threads in the team in chunks as the executing threads request them.
1753   // Each thread executes a chunk of iterations, then requests another chunk,
1754   // until no chunks remain to be assigned. For a chunk_size of 1, the size of
1755   // each chunk is proportional to the number of unassigned iterations divided
1756   // by the number of threads in the team, decreasing to 1. For a chunk_size
1757   // with value k (greater than 1), the size of each chunk is determined in the
1758   // same way, with the restriction that the chunks do not contain fewer than k
1759   // iterations (except for the last chunk to be assigned, which may have fewer
1760   // than k iterations).
1761   //
1762   // When schedule(auto) is specified, the decision regarding scheduling is
1763   // delegated to the compiler and/or runtime system. The programmer gives the
1764   // implementation the freedom to choose any possible mapping of iterations to
1765   // threads in the team.
1766   //
1767   // When schedule(runtime) is specified, the decision regarding scheduling is
1768   // deferred until run time, and the schedule and chunk size are taken from the
1769   // run-sched-var ICV. If the ICV is set to auto, the schedule is
1770   // implementation defined
1771   //
1772   // while(__kmpc_dispatch_next(&LB, &UB)) {
1773   //   idx = LB;
1774   //   while (idx <= UB) { BODY; ++idx;
1775   //   __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only.
1776   //   } // inner loop
1777   // }
1778   //
1779   // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1780   // When schedule(static, chunk_size) is specified, iterations are divided into
1781   // chunks of size chunk_size, and the chunks are assigned to the threads in
1782   // the team in a round-robin fashion in the order of the thread number.
1783   //
1784   // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) {
1785   //   while (idx <= UB) { BODY; ++idx; } // inner loop
1786   //   LB = LB + ST;
1787   //   UB = UB + ST;
1788   // }
1789   //
1790 
1791   const Expr *IVExpr = S.getIterationVariable();
1792   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1793   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1794 
1795   if (DynamicOrOrdered) {
1796     llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration());
1797     RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind,
1798                            IVSize, IVSigned, Ordered, UBVal, Chunk);
1799   } else {
1800     RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned,
1801                          Ordered, IL, LB, UB, ST, Chunk);
1802   }
1803 
1804   EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, Ordered, LB, UB,
1805                    ST, IL, Chunk);
1806 }
1807 
1808 void CodeGenFunction::EmitOMPDistributeOuterLoop(
1809     OpenMPDistScheduleClauseKind ScheduleKind,
1810     const OMPDistributeDirective &S, OMPPrivateScope &LoopScope,
1811     Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) {
1812 
1813   auto &RT = CGM.getOpenMPRuntime();
1814 
1815   // Emit outer loop.
1816   // Same behavior as a OMPForOuterLoop, except that schedule cannot be
1817   // dynamic
1818   //
1819 
1820   const Expr *IVExpr = S.getIterationVariable();
1821   const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1822   const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1823 
1824   RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
1825                               IVSize, IVSigned, /* Ordered = */ false,
1826                               IL, LB, UB, ST, Chunk);
1827 
1828   EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false,
1829                    S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk);
1830 }
1831 
1832 /// \brief Emit a helper variable and return corresponding lvalue.
1833 static LValue EmitOMPHelperVar(CodeGenFunction &CGF,
1834                                const DeclRefExpr *Helper) {
1835   auto VDecl = cast<VarDecl>(Helper->getDecl());
1836   CGF.EmitVarDecl(*VDecl);
1837   return CGF.EmitLValue(Helper);
1838 }
1839 
1840 namespace {
1841   struct ScheduleKindModifiersTy {
1842     OpenMPScheduleClauseKind Kind;
1843     OpenMPScheduleClauseModifier M1;
1844     OpenMPScheduleClauseModifier M2;
1845     ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind,
1846                             OpenMPScheduleClauseModifier M1,
1847                             OpenMPScheduleClauseModifier M2)
1848         : Kind(Kind), M1(M1), M2(M2) {}
1849   };
1850 } // namespace
1851 
1852 bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) {
1853   // Emit the loop iteration variable.
1854   auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
1855   auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
1856   EmitVarDecl(*IVDecl);
1857 
1858   // Emit the iterations count variable.
1859   // If it is not a variable, Sema decided to calculate iterations count on each
1860   // iteration (e.g., it is foldable into a constant).
1861   if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
1862     EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
1863     // Emit calculation of the iterations count.
1864     EmitIgnoredExpr(S.getCalcLastIteration());
1865   }
1866 
1867   auto &RT = CGM.getOpenMPRuntime();
1868 
1869   bool HasLastprivateClause;
1870   // Check pre-condition.
1871   {
1872     OMPLoopScope PreInitScope(*this, S);
1873     // Skip the entire loop if we don't meet the precondition.
1874     // If the condition constant folds and can be elided, avoid emitting the
1875     // whole loop.
1876     bool CondConstant;
1877     llvm::BasicBlock *ContBlock = nullptr;
1878     if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
1879       if (!CondConstant)
1880         return false;
1881     } else {
1882       auto *ThenBlock = createBasicBlock("omp.precond.then");
1883       ContBlock = createBasicBlock("omp.precond.end");
1884       emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
1885                   getProfileCount(&S));
1886       EmitBlock(ThenBlock);
1887       incrementProfileCounter(&S);
1888     }
1889 
1890     llvm::DenseSet<const Expr *> EmittedFinals;
1891     emitAlignedClause(*this, S);
1892     EmitOMPLinearClauseInit(S);
1893     // Emit helper vars inits.
1894     LValue LB =
1895         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
1896     LValue UB =
1897         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
1898     LValue ST =
1899         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
1900     LValue IL =
1901         EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
1902 
1903     // Emit 'then' code.
1904     {
1905       OMPPrivateScope LoopScope(*this);
1906       if (EmitOMPFirstprivateClause(S, LoopScope)) {
1907         // Emit implicit barrier to synchronize threads and avoid data races on
1908         // initialization of firstprivate variables and post-update of
1909         // lastprivate variables.
1910         CGM.getOpenMPRuntime().emitBarrierCall(
1911             *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
1912             /*ForceSimpleCall=*/true);
1913       }
1914       EmitOMPPrivateClause(S, LoopScope);
1915       HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope);
1916       EmitOMPReductionClauseInit(S, LoopScope);
1917       EmitOMPPrivateLoopCounters(S, LoopScope);
1918       EmitOMPLinearClause(S, LoopScope);
1919       (void)LoopScope.Privatize();
1920 
1921       // Detect the loop schedule kind and chunk.
1922       llvm::Value *Chunk = nullptr;
1923       OpenMPScheduleClauseKind ScheduleKind = OMPC_SCHEDULE_unknown;
1924       OpenMPScheduleClauseModifier M1 = OMPC_SCHEDULE_MODIFIER_unknown;
1925       OpenMPScheduleClauseModifier M2 = OMPC_SCHEDULE_MODIFIER_unknown;
1926       if (auto *C = S.getSingleClause<OMPScheduleClause>()) {
1927         ScheduleKind = C->getScheduleKind();
1928         M1 = C->getFirstScheduleModifier();
1929         M2 = C->getSecondScheduleModifier();
1930         if (const auto *Ch = C->getChunkSize()) {
1931           Chunk = EmitScalarExpr(Ch);
1932           Chunk = EmitScalarConversion(Chunk, Ch->getType(),
1933                                        S.getIterationVariable()->getType(),
1934                                        S.getLocStart());
1935         }
1936       }
1937       const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
1938       const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
1939       const bool Ordered = S.getSingleClause<OMPOrderedClause>() != nullptr;
1940       // OpenMP 4.5, 2.7.1 Loop Construct, Description.
1941       // If the static schedule kind is specified or if the ordered clause is
1942       // specified, and if no monotonic modifier is specified, the effect will
1943       // be as if the monotonic modifier was specified.
1944       if (RT.isStaticNonchunked(ScheduleKind,
1945                                 /* Chunked */ Chunk != nullptr) &&
1946           !Ordered) {
1947         if (isOpenMPSimdDirective(S.getDirectiveKind()))
1948           EmitOMPSimdInit(S, /*IsMonotonic=*/true);
1949         // OpenMP [2.7.1, Loop Construct, Description, table 2-1]
1950         // When no chunk_size is specified, the iteration space is divided into
1951         // chunks that are approximately equal in size, and at most one chunk is
1952         // distributed to each thread. Note that the size of the chunks is
1953         // unspecified in this case.
1954         RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind,
1955                              IVSize, IVSigned, Ordered,
1956                              IL.getAddress(), LB.getAddress(),
1957                              UB.getAddress(), ST.getAddress());
1958         auto LoopExit =
1959             getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
1960         // UB = min(UB, GlobalUB);
1961         EmitIgnoredExpr(S.getEnsureUpperBound());
1962         // IV = LB;
1963         EmitIgnoredExpr(S.getInit());
1964         // while (idx <= UB) { BODY; ++idx; }
1965         EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
1966                          S.getInc(),
1967                          [&S, LoopExit](CodeGenFunction &CGF) {
1968                            CGF.EmitOMPLoopBody(S, LoopExit);
1969                            CGF.EmitStopPoint(&S);
1970                          },
1971                          [](CodeGenFunction &) {});
1972         EmitBlock(LoopExit.getBlock());
1973         // Tell the runtime we are done.
1974         RT.emitForStaticFinish(*this, S.getLocStart());
1975       } else {
1976         const bool IsMonotonic = Ordered ||
1977                                  ScheduleKind == OMPC_SCHEDULE_static ||
1978                                  ScheduleKind == OMPC_SCHEDULE_unknown ||
1979                                  M1 == OMPC_SCHEDULE_MODIFIER_monotonic ||
1980                                  M2 == OMPC_SCHEDULE_MODIFIER_monotonic;
1981         // Emit the outer loop, which requests its work chunk [LB..UB] from
1982         // runtime and runs the inner loop to process it.
1983         EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered,
1984                             LB.getAddress(), UB.getAddress(), ST.getAddress(),
1985                             IL.getAddress(), Chunk);
1986       }
1987       if (isOpenMPSimdDirective(S.getDirectiveKind())) {
1988         EmitOMPSimdFinal(S,
1989                          [&](CodeGenFunction &CGF) -> llvm::Value * {
1990                            return CGF.Builder.CreateIsNotNull(
1991                                CGF.EmitLoadOfScalar(IL, S.getLocStart()));
1992                          });
1993       }
1994       EmitOMPReductionClauseFinal(S);
1995       // Emit post-update of the reduction variables if IsLastIter != 0.
1996       emitPostUpdateForReductionClause(
1997           *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
1998             return CGF.Builder.CreateIsNotNull(
1999                 CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2000           });
2001       // Emit final copy of the lastprivate variables if IsLastIter != 0.
2002       if (HasLastprivateClause)
2003         EmitOMPLastprivateClauseFinal(
2004             S, isOpenMPSimdDirective(S.getDirectiveKind()),
2005             Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart())));
2006     }
2007     EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2008       return CGF.Builder.CreateIsNotNull(
2009           CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2010     });
2011     // We're now done with the loop, so jump to the continuation block.
2012     if (ContBlock) {
2013       EmitBranch(ContBlock);
2014       EmitBlock(ContBlock, true);
2015     }
2016   }
2017   return HasLastprivateClause;
2018 }
2019 
2020 void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) {
2021   bool HasLastprivates = false;
2022   auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2023                                           PrePostActionTy &) {
2024     HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
2025   };
2026   {
2027     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2028     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen,
2029                                                 S.hasCancel());
2030   }
2031 
2032   // Emit an implicit barrier at the end.
2033   if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
2034     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2035   }
2036 }
2037 
2038 void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) {
2039   bool HasLastprivates = false;
2040   auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF,
2041                                           PrePostActionTy &) {
2042     HasLastprivates = CGF.EmitOMPWorksharingLoop(S);
2043   };
2044   {
2045     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2046     CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen);
2047   }
2048 
2049   // Emit an implicit barrier at the end.
2050   if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) {
2051     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for);
2052   }
2053 }
2054 
2055 static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
2056                                 const Twine &Name,
2057                                 llvm::Value *Init = nullptr) {
2058   auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty);
2059   if (Init)
2060     CGF.EmitScalarInit(Init, LVal);
2061   return LVal;
2062 }
2063 
2064 void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
2065   auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt();
2066   auto *CS = dyn_cast<CompoundStmt>(Stmt);
2067   bool HasLastprivates = false;
2068   auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF,
2069                                                     PrePostActionTy &) {
2070     auto &C = CGF.CGM.getContext();
2071     auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
2072     // Emit helper vars inits.
2073     LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.",
2074                                   CGF.Builder.getInt32(0));
2075     auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1)
2076                                       : CGF.Builder.getInt32(0);
2077     LValue UB =
2078         createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal);
2079     LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.",
2080                                   CGF.Builder.getInt32(1));
2081     LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.",
2082                                   CGF.Builder.getInt32(0));
2083     // Loop counter.
2084     LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv.");
2085     OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2086     CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV);
2087     OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue);
2088     CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB);
2089     // Generate condition for loop.
2090     BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue,
2091                         OK_Ordinary, S.getLocStart(),
2092                         /*fpContractable=*/false);
2093     // Increment for loop counter.
2094     UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary,
2095                       S.getLocStart());
2096     auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) {
2097       // Iterate through all sections and emit a switch construct:
2098       // switch (IV) {
2099       //   case 0:
2100       //     <SectionStmt[0]>;
2101       //     break;
2102       // ...
2103       //   case <NumSection> - 1:
2104       //     <SectionStmt[<NumSection> - 1]>;
2105       //     break;
2106       // }
2107       // .omp.sections.exit:
2108       auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit");
2109       auto *SwitchStmt = CGF.Builder.CreateSwitch(
2110           CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB,
2111           CS == nullptr ? 1 : CS->size());
2112       if (CS) {
2113         unsigned CaseNumber = 0;
2114         for (auto *SubStmt : CS->children()) {
2115           auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2116           CGF.EmitBlock(CaseBB);
2117           SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB);
2118           CGF.EmitStmt(SubStmt);
2119           CGF.EmitBranch(ExitBB);
2120           ++CaseNumber;
2121         }
2122       } else {
2123         auto CaseBB = CGF.createBasicBlock(".omp.sections.case");
2124         CGF.EmitBlock(CaseBB);
2125         SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB);
2126         CGF.EmitStmt(Stmt);
2127         CGF.EmitBranch(ExitBB);
2128       }
2129       CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
2130     };
2131 
2132     CodeGenFunction::OMPPrivateScope LoopScope(CGF);
2133     if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) {
2134       // Emit implicit barrier to synchronize threads and avoid data races on
2135       // initialization of firstprivate variables and post-update of lastprivate
2136       // variables.
2137       CGF.CGM.getOpenMPRuntime().emitBarrierCall(
2138           CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false,
2139           /*ForceSimpleCall=*/true);
2140     }
2141     CGF.EmitOMPPrivateClause(S, LoopScope);
2142     HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope);
2143     CGF.EmitOMPReductionClauseInit(S, LoopScope);
2144     (void)LoopScope.Privatize();
2145 
2146     // Emit static non-chunked loop.
2147     CGF.CGM.getOpenMPRuntime().emitForStaticInit(
2148         CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32,
2149         /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(),
2150         UB.getAddress(), ST.getAddress());
2151     // UB = min(UB, GlobalUB);
2152     auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart());
2153     auto *MinUBGlobalUB = CGF.Builder.CreateSelect(
2154         CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal);
2155     CGF.EmitStoreOfScalar(MinUBGlobalUB, UB);
2156     // IV = LB;
2157     CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV);
2158     // while (idx <= UB) { BODY; ++idx; }
2159     CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen,
2160                          [](CodeGenFunction &) {});
2161     // Tell the runtime we are done.
2162     CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart());
2163     CGF.EmitOMPReductionClauseFinal(S);
2164     // Emit post-update of the reduction variables if IsLastIter != 0.
2165     emitPostUpdateForReductionClause(
2166         CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * {
2167           return CGF.Builder.CreateIsNotNull(
2168               CGF.EmitLoadOfScalar(IL, S.getLocStart()));
2169         });
2170 
2171     // Emit final copy of the lastprivate variables if IsLastIter != 0.
2172     if (HasLastprivates)
2173       CGF.EmitOMPLastprivateClauseFinal(
2174           S, /*NoFinals=*/false,
2175           CGF.Builder.CreateIsNotNull(
2176               CGF.EmitLoadOfScalar(IL, S.getLocStart())));
2177   };
2178 
2179   bool HasCancel = false;
2180   if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S))
2181     HasCancel = OSD->hasCancel();
2182   else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S))
2183     HasCancel = OPSD->hasCancel();
2184   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen,
2185                                               HasCancel);
2186   // Emit barrier for lastprivates only if 'sections' directive has 'nowait'
2187   // clause. Otherwise the barrier will be generated by the codegen for the
2188   // directive.
2189   if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) {
2190     // Emit implicit barrier to synchronize threads and avoid data races on
2191     // initialization of firstprivate variables.
2192     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2193                                            OMPD_unknown);
2194   }
2195 }
2196 
2197 void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
2198   {
2199     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2200     EmitSections(S);
2201   }
2202   // Emit an implicit barrier at the end.
2203   if (!S.getSingleClause<OMPNowaitClause>()) {
2204     CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(),
2205                                            OMPD_sections);
2206   }
2207 }
2208 
2209 void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
2210   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2211     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2212   };
2213   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2214   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen,
2215                                               S.hasCancel());
2216 }
2217 
2218 void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) {
2219   llvm::SmallVector<const Expr *, 8> CopyprivateVars;
2220   llvm::SmallVector<const Expr *, 8> DestExprs;
2221   llvm::SmallVector<const Expr *, 8> SrcExprs;
2222   llvm::SmallVector<const Expr *, 8> AssignmentOps;
2223   // Check if there are any 'copyprivate' clauses associated with this
2224   // 'single' construct.
2225   // Build a list of copyprivate variables along with helper expressions
2226   // (<source>, <destination>, <destination>=<source> expressions)
2227   for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) {
2228     CopyprivateVars.append(C->varlists().begin(), C->varlists().end());
2229     DestExprs.append(C->destination_exprs().begin(),
2230                      C->destination_exprs().end());
2231     SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end());
2232     AssignmentOps.append(C->assignment_ops().begin(),
2233                          C->assignment_ops().end());
2234   }
2235   // Emit code for 'single' region along with 'copyprivate' clauses
2236   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2237     Action.Enter(CGF);
2238     OMPPrivateScope SingleScope(CGF);
2239     (void)CGF.EmitOMPFirstprivateClause(S, SingleScope);
2240     CGF.EmitOMPPrivateClause(S, SingleScope);
2241     (void)SingleScope.Privatize();
2242     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2243   };
2244   {
2245     OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2246     CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(),
2247                                             CopyprivateVars, DestExprs,
2248                                             SrcExprs, AssignmentOps);
2249   }
2250   // Emit an implicit barrier at the end (to avoid data race on firstprivate
2251   // init or if no 'nowait' clause was specified and no 'copyprivate' clause).
2252   if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) {
2253     CGM.getOpenMPRuntime().emitBarrierCall(
2254         *this, S.getLocStart(),
2255         S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single);
2256   }
2257 }
2258 
2259 void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
2260   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2261     Action.Enter(CGF);
2262     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2263   };
2264   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2265   CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart());
2266 }
2267 
2268 void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
2269   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2270     Action.Enter(CGF);
2271     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2272   };
2273   Expr *Hint = nullptr;
2274   if (auto *HintClause = S.getSingleClause<OMPHintClause>())
2275     Hint = HintClause->getHint();
2276   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2277   CGM.getOpenMPRuntime().emitCriticalRegion(*this,
2278                                             S.getDirectiveName().getAsString(),
2279                                             CodeGen, S.getLocStart(), Hint);
2280 }
2281 
2282 void CodeGenFunction::EmitOMPParallelForDirective(
2283     const OMPParallelForDirective &S) {
2284   // Emit directive as a combined directive that consists of two implicit
2285   // directives: 'parallel' with 'for' directive.
2286   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2287     CGF.EmitOMPWorksharingLoop(S);
2288   };
2289   emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen);
2290 }
2291 
2292 void CodeGenFunction::EmitOMPParallelForSimdDirective(
2293     const OMPParallelForSimdDirective &S) {
2294   // Emit directive as a combined directive that consists of two implicit
2295   // directives: 'parallel' with 'for' directive.
2296   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2297     CGF.EmitOMPWorksharingLoop(S);
2298   };
2299   emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen);
2300 }
2301 
2302 void CodeGenFunction::EmitOMPParallelSectionsDirective(
2303     const OMPParallelSectionsDirective &S) {
2304   // Emit directive as a combined directive that consists of two implicit
2305   // directives: 'parallel' with 'sections' directive.
2306   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2307     CGF.EmitSections(S);
2308   };
2309   emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen);
2310 }
2311 
2312 void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S,
2313                                                 const RegionCodeGenTy &BodyGen,
2314                                                 const TaskGenTy &TaskGen,
2315                                                 bool Tied) {
2316   // Emit outlined function for task construct.
2317   auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2318   auto *I = CS->getCapturedDecl()->param_begin();
2319   auto *PartId = std::next(I);
2320   auto *TaskT = std::next(I, 4);
2321   // The first function argument for tasks is a thread id, the second one is a
2322   // part id (0 for tied tasks, >=0 for untied task).
2323   llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
2324   // Get list of private variables.
2325   OMPPrivateDataTy Data;
2326   Data.Tied = Tied;
2327   for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) {
2328     auto IRef = C->varlist_begin();
2329     for (auto *IInit : C->private_copies()) {
2330       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2331       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2332         Data.PrivateVars.push_back(*IRef);
2333         Data.PrivateCopies.push_back(IInit);
2334       }
2335       ++IRef;
2336     }
2337   }
2338   EmittedAsPrivate.clear();
2339   // Get list of firstprivate variables.
2340   for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) {
2341     auto IRef = C->varlist_begin();
2342     auto IElemInitRef = C->inits().begin();
2343     for (auto *IInit : C->private_copies()) {
2344       auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl());
2345       if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) {
2346         Data.FirstprivateVars.push_back(*IRef);
2347         Data.FirstprivateCopies.push_back(IInit);
2348         Data.FirstprivateInits.push_back(*IElemInitRef);
2349       }
2350       ++IRef;
2351       ++IElemInitRef;
2352     }
2353   }
2354   // Build list of dependences.
2355   for (const auto *C : S.getClausesOfKind<OMPDependClause>())
2356     for (auto *IRef : C->varlists())
2357       Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef));
2358   auto &&CodeGen = [PartId, &S, &Data, CS, &BodyGen](CodeGenFunction &CGF,
2359                                                      PrePostActionTy &Action) {
2360     // Set proper addresses for generated private copies.
2361     OMPPrivateScope Scope(CGF);
2362     if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty()) {
2363       auto *CopyFn = CGF.Builder.CreateLoad(
2364           CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)));
2365       auto *PrivatesPtr = CGF.Builder.CreateLoad(
2366           CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)));
2367       // Map privates.
2368       llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs;
2369       llvm::SmallVector<llvm::Value *, 16> CallArgs;
2370       CallArgs.push_back(PrivatesPtr);
2371       for (auto *E : Data.PrivateVars) {
2372         auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2373         Address PrivatePtr = CGF.CreateMemTemp(
2374             CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr");
2375         PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2376         CallArgs.push_back(PrivatePtr.getPointer());
2377       }
2378       for (auto *E : Data.FirstprivateVars) {
2379         auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl());
2380         Address PrivatePtr =
2381             CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()),
2382                               ".firstpriv.ptr.addr");
2383         PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr));
2384         CallArgs.push_back(PrivatePtr.getPointer());
2385       }
2386       CGF.EmitRuntimeCall(CopyFn, CallArgs);
2387       for (auto &&Pair : PrivatePtrs) {
2388         Address Replacement(CGF.Builder.CreateLoad(Pair.second),
2389                             CGF.getContext().getDeclAlign(Pair.first));
2390         Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; });
2391       }
2392     }
2393     (void)Scope.Privatize();
2394 
2395     Action.Enter(CGF);
2396     BodyGen(CGF);
2397   };
2398   auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction(
2399       S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied,
2400       Data.NumberOfParts);
2401   OMPLexicalScope Scope(*this, S);
2402   TaskGen(*this, OutlinedFn, Data);
2403 }
2404 
2405 void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
2406   // Emit outlined function for task construct.
2407   auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2408   auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
2409   // Check if we should emit tied or untied task.
2410   bool Tied = !S.getSingleClause<OMPUntiedClause>();
2411   // Check if the task is final
2412   llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
2413   if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
2414     // If the condition constant folds and can be elided, try to avoid emitting
2415     // the condition and the dead arm of the if/else.
2416     auto *Cond = Clause->getCondition();
2417     bool CondConstant;
2418     if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
2419       Final.setInt(CondConstant);
2420     else
2421       Final.setPointer(EvaluateExprAsBool(Cond));
2422   } else {
2423     // By default the task is not final.
2424     Final.setInt(/*IntVal=*/false);
2425   }
2426   auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
2427   const Expr *IfCond = nullptr;
2428   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
2429     if (C->getNameModifier() == OMPD_unknown ||
2430         C->getNameModifier() == OMPD_task) {
2431       IfCond = C->getCondition();
2432       break;
2433     }
2434   }
2435 
2436   auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
2437     CGF.EmitStmt(CS->getCapturedStmt());
2438   };
2439   auto &&TaskGen = [&S, &Final, SharedsTy, CapturedStruct,
2440                     IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
2441                             const OMPPrivateDataTy &Data) {
2442     CGF.CGM.getOpenMPRuntime().emitTaskCall(
2443         CGF, S.getLocStart(), S, Data.Tied, Final, Data.NumberOfParts,
2444         OutlinedFn, SharedsTy, CapturedStruct, IfCond, Data.PrivateVars,
2445         Data.PrivateCopies, Data.FirstprivateVars, Data.FirstprivateCopies,
2446         Data.FirstprivateInits, Data.Dependences);
2447   };
2448   EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Tied);
2449 }
2450 
2451 void CodeGenFunction::EmitOMPTaskyieldDirective(
2452     const OMPTaskyieldDirective &S) {
2453   CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart());
2454 }
2455 
2456 void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) {
2457   CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier);
2458 }
2459 
2460 void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) {
2461   CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart());
2462 }
2463 
2464 void CodeGenFunction::EmitOMPTaskgroupDirective(
2465     const OMPTaskgroupDirective &S) {
2466   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
2467     Action.Enter(CGF);
2468     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2469   };
2470   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2471   CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart());
2472 }
2473 
2474 void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) {
2475   CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> {
2476     if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) {
2477       return llvm::makeArrayRef(FlushClause->varlist_begin(),
2478                                 FlushClause->varlist_end());
2479     }
2480     return llvm::None;
2481   }(), S.getLocStart());
2482 }
2483 
2484 void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) {
2485   // Emit the loop iteration variable.
2486   auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable());
2487   auto IVDecl = cast<VarDecl>(IVExpr->getDecl());
2488   EmitVarDecl(*IVDecl);
2489 
2490   // Emit the iterations count variable.
2491   // If it is not a variable, Sema decided to calculate iterations count on each
2492   // iteration (e.g., it is foldable into a constant).
2493   if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
2494     EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
2495     // Emit calculation of the iterations count.
2496     EmitIgnoredExpr(S.getCalcLastIteration());
2497   }
2498 
2499   auto &RT = CGM.getOpenMPRuntime();
2500 
2501   // Check pre-condition.
2502   {
2503     OMPLoopScope PreInitScope(*this, S);
2504     // Skip the entire loop if we don't meet the precondition.
2505     // If the condition constant folds and can be elided, avoid emitting the
2506     // whole loop.
2507     bool CondConstant;
2508     llvm::BasicBlock *ContBlock = nullptr;
2509     if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
2510       if (!CondConstant)
2511         return;
2512     } else {
2513       auto *ThenBlock = createBasicBlock("omp.precond.then");
2514       ContBlock = createBasicBlock("omp.precond.end");
2515       emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
2516                   getProfileCount(&S));
2517       EmitBlock(ThenBlock);
2518       incrementProfileCounter(&S);
2519     }
2520 
2521     // Emit 'then' code.
2522     {
2523       // Emit helper vars inits.
2524       LValue LB =
2525           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable()));
2526       LValue UB =
2527           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable()));
2528       LValue ST =
2529           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable()));
2530       LValue IL =
2531           EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable()));
2532 
2533       OMPPrivateScope LoopScope(*this);
2534       EmitOMPPrivateLoopCounters(S, LoopScope);
2535       (void)LoopScope.Privatize();
2536 
2537       // Detect the distribute schedule kind and chunk.
2538       llvm::Value *Chunk = nullptr;
2539       OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown;
2540       if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) {
2541         ScheduleKind = C->getDistScheduleKind();
2542         if (const auto *Ch = C->getChunkSize()) {
2543           Chunk = EmitScalarExpr(Ch);
2544           Chunk = EmitScalarConversion(Chunk, Ch->getType(),
2545           S.getIterationVariable()->getType(),
2546           S.getLocStart());
2547         }
2548       }
2549       const unsigned IVSize = getContext().getTypeSize(IVExpr->getType());
2550       const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation();
2551 
2552       // OpenMP [2.10.8, distribute Construct, Description]
2553       // If dist_schedule is specified, kind must be static. If specified,
2554       // iterations are divided into chunks of size chunk_size, chunks are
2555       // assigned to the teams of the league in a round-robin fashion in the
2556       // order of the team number. When no chunk_size is specified, the
2557       // iteration space is divided into chunks that are approximately equal
2558       // in size, and at most one chunk is distributed to each team of the
2559       // league. The size of the chunks is unspecified in this case.
2560       if (RT.isStaticNonchunked(ScheduleKind,
2561                                 /* Chunked */ Chunk != nullptr)) {
2562         RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind,
2563                              IVSize, IVSigned, /* Ordered = */ false,
2564                              IL.getAddress(), LB.getAddress(),
2565                              UB.getAddress(), ST.getAddress());
2566         auto LoopExit =
2567             getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit"));
2568         // UB = min(UB, GlobalUB);
2569         EmitIgnoredExpr(S.getEnsureUpperBound());
2570         // IV = LB;
2571         EmitIgnoredExpr(S.getInit());
2572         // while (idx <= UB) { BODY; ++idx; }
2573         EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
2574                          S.getInc(),
2575                          [&S, LoopExit](CodeGenFunction &CGF) {
2576                            CGF.EmitOMPLoopBody(S, LoopExit);
2577                            CGF.EmitStopPoint(&S);
2578                          },
2579                          [](CodeGenFunction &) {});
2580         EmitBlock(LoopExit.getBlock());
2581         // Tell the runtime we are done.
2582         RT.emitForStaticFinish(*this, S.getLocStart());
2583       } else {
2584         // Emit the outer loop, which requests its work chunk [LB..UB] from
2585         // runtime and runs the inner loop to process it.
2586         EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope,
2587                             LB.getAddress(), UB.getAddress(), ST.getAddress(),
2588                             IL.getAddress(), Chunk);
2589       }
2590     }
2591 
2592     // We're now done with the loop, so jump to the continuation block.
2593     if (ContBlock) {
2594       EmitBranch(ContBlock);
2595       EmitBlock(ContBlock, true);
2596     }
2597   }
2598 }
2599 
2600 void CodeGenFunction::EmitOMPDistributeDirective(
2601     const OMPDistributeDirective &S) {
2602   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
2603     CGF.EmitOMPDistributeLoop(S);
2604   };
2605   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2606   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen,
2607                                               false);
2608 }
2609 
2610 static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
2611                                                    const CapturedStmt *S) {
2612   CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
2613   CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
2614   CGF.CapturedStmtInfo = &CapStmtInfo;
2615   auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S);
2616   Fn->addFnAttr(llvm::Attribute::NoInline);
2617   return Fn;
2618 }
2619 
2620 void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
2621   if (!S.getAssociatedStmt())
2622     return;
2623   auto *C = S.getSingleClause<OMPSIMDClause>();
2624   auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF,
2625                                  PrePostActionTy &Action) {
2626     if (C) {
2627       auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
2628       llvm::SmallVector<llvm::Value *, 16> CapturedVars;
2629       CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
2630       auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS);
2631       CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars);
2632     } else {
2633       Action.Enter(CGF);
2634       CGF.EmitStmt(
2635           cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
2636     }
2637   };
2638   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
2639   CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C);
2640 }
2641 
2642 static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val,
2643                                          QualType SrcType, QualType DestType,
2644                                          SourceLocation Loc) {
2645   assert(CGF.hasScalarEvaluationKind(DestType) &&
2646          "DestType must have scalar evaluation kind.");
2647   assert(!Val.isAggregate() && "Must be a scalar or complex.");
2648   return Val.isScalar()
2649              ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType,
2650                                         Loc)
2651              : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType,
2652                                                  DestType, Loc);
2653 }
2654 
2655 static CodeGenFunction::ComplexPairTy
2656 convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType,
2657                       QualType DestType, SourceLocation Loc) {
2658   assert(CGF.getEvaluationKind(DestType) == TEK_Complex &&
2659          "DestType must have complex evaluation kind.");
2660   CodeGenFunction::ComplexPairTy ComplexVal;
2661   if (Val.isScalar()) {
2662     // Convert the input element to the element type of the complex.
2663     auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
2664     auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType,
2665                                               DestElementType, Loc);
2666     ComplexVal = CodeGenFunction::ComplexPairTy(
2667         ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType()));
2668   } else {
2669     assert(Val.isComplex() && "Must be a scalar or complex.");
2670     auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType();
2671     auto DestElementType = DestType->castAs<ComplexType>()->getElementType();
2672     ComplexVal.first = CGF.EmitScalarConversion(
2673         Val.getComplexVal().first, SrcElementType, DestElementType, Loc);
2674     ComplexVal.second = CGF.EmitScalarConversion(
2675         Val.getComplexVal().second, SrcElementType, DestElementType, Loc);
2676   }
2677   return ComplexVal;
2678 }
2679 
2680 static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst,
2681                                   LValue LVal, RValue RVal) {
2682   if (LVal.isGlobalReg()) {
2683     CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal);
2684   } else {
2685     CGF.EmitAtomicStore(RVal, LVal,
2686                         IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2687                                  : llvm::AtomicOrdering::Monotonic,
2688                         LVal.isVolatile(), /*IsInit=*/false);
2689   }
2690 }
2691 
2692 void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal,
2693                                          QualType RValTy, SourceLocation Loc) {
2694   switch (getEvaluationKind(LVal.getType())) {
2695   case TEK_Scalar:
2696     EmitStoreThroughLValue(RValue::get(convertToScalarValue(
2697                                *this, RVal, RValTy, LVal.getType(), Loc)),
2698                            LVal);
2699     break;
2700   case TEK_Complex:
2701     EmitStoreOfComplex(
2702         convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal,
2703         /*isInit=*/false);
2704     break;
2705   case TEK_Aggregate:
2706     llvm_unreachable("Must be a scalar or complex.");
2707   }
2708 }
2709 
2710 static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst,
2711                                   const Expr *X, const Expr *V,
2712                                   SourceLocation Loc) {
2713   // v = x;
2714   assert(V->isLValue() && "V of 'omp atomic read' is not lvalue");
2715   assert(X->isLValue() && "X of 'omp atomic read' is not lvalue");
2716   LValue XLValue = CGF.EmitLValue(X);
2717   LValue VLValue = CGF.EmitLValue(V);
2718   RValue Res = XLValue.isGlobalReg()
2719                    ? CGF.EmitLoadOfLValue(XLValue, Loc)
2720                    : CGF.EmitAtomicLoad(
2721                          XLValue, Loc,
2722                          IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2723                                   : llvm::AtomicOrdering::Monotonic,
2724                          XLValue.isVolatile());
2725   // OpenMP, 2.12.6, atomic Construct
2726   // Any atomic construct with a seq_cst clause forces the atomically
2727   // performed operation to include an implicit flush operation without a
2728   // list.
2729   if (IsSeqCst)
2730     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2731   CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc);
2732 }
2733 
2734 static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst,
2735                                    const Expr *X, const Expr *E,
2736                                    SourceLocation Loc) {
2737   // x = expr;
2738   assert(X->isLValue() && "X of 'omp atomic write' is not lvalue");
2739   emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E));
2740   // OpenMP, 2.12.6, atomic Construct
2741   // Any atomic construct with a seq_cst clause forces the atomically
2742   // performed operation to include an implicit flush operation without a
2743   // list.
2744   if (IsSeqCst)
2745     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2746 }
2747 
2748 static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X,
2749                                                 RValue Update,
2750                                                 BinaryOperatorKind BO,
2751                                                 llvm::AtomicOrdering AO,
2752                                                 bool IsXLHSInRHSPart) {
2753   auto &Context = CGF.CGM.getContext();
2754   // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x'
2755   // expression is simple and atomic is allowed for the given type for the
2756   // target platform.
2757   if (BO == BO_Comma || !Update.isScalar() ||
2758       !Update.getScalarVal()->getType()->isIntegerTy() ||
2759       !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) &&
2760                         (Update.getScalarVal()->getType() !=
2761                          X.getAddress().getElementType())) ||
2762       !X.getAddress().getElementType()->isIntegerTy() ||
2763       !Context.getTargetInfo().hasBuiltinAtomic(
2764           Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment())))
2765     return std::make_pair(false, RValue::get(nullptr));
2766 
2767   llvm::AtomicRMWInst::BinOp RMWOp;
2768   switch (BO) {
2769   case BO_Add:
2770     RMWOp = llvm::AtomicRMWInst::Add;
2771     break;
2772   case BO_Sub:
2773     if (!IsXLHSInRHSPart)
2774       return std::make_pair(false, RValue::get(nullptr));
2775     RMWOp = llvm::AtomicRMWInst::Sub;
2776     break;
2777   case BO_And:
2778     RMWOp = llvm::AtomicRMWInst::And;
2779     break;
2780   case BO_Or:
2781     RMWOp = llvm::AtomicRMWInst::Or;
2782     break;
2783   case BO_Xor:
2784     RMWOp = llvm::AtomicRMWInst::Xor;
2785     break;
2786   case BO_LT:
2787     RMWOp = X.getType()->hasSignedIntegerRepresentation()
2788                 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min
2789                                    : llvm::AtomicRMWInst::Max)
2790                 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin
2791                                    : llvm::AtomicRMWInst::UMax);
2792     break;
2793   case BO_GT:
2794     RMWOp = X.getType()->hasSignedIntegerRepresentation()
2795                 ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max
2796                                    : llvm::AtomicRMWInst::Min)
2797                 : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax
2798                                    : llvm::AtomicRMWInst::UMin);
2799     break;
2800   case BO_Assign:
2801     RMWOp = llvm::AtomicRMWInst::Xchg;
2802     break;
2803   case BO_Mul:
2804   case BO_Div:
2805   case BO_Rem:
2806   case BO_Shl:
2807   case BO_Shr:
2808   case BO_LAnd:
2809   case BO_LOr:
2810     return std::make_pair(false, RValue::get(nullptr));
2811   case BO_PtrMemD:
2812   case BO_PtrMemI:
2813   case BO_LE:
2814   case BO_GE:
2815   case BO_EQ:
2816   case BO_NE:
2817   case BO_AddAssign:
2818   case BO_SubAssign:
2819   case BO_AndAssign:
2820   case BO_OrAssign:
2821   case BO_XorAssign:
2822   case BO_MulAssign:
2823   case BO_DivAssign:
2824   case BO_RemAssign:
2825   case BO_ShlAssign:
2826   case BO_ShrAssign:
2827   case BO_Comma:
2828     llvm_unreachable("Unsupported atomic update operation");
2829   }
2830   auto *UpdateVal = Update.getScalarVal();
2831   if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) {
2832     UpdateVal = CGF.Builder.CreateIntCast(
2833         IC, X.getAddress().getElementType(),
2834         X.getType()->hasSignedIntegerRepresentation());
2835   }
2836   auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO);
2837   return std::make_pair(true, RValue::get(Res));
2838 }
2839 
2840 std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr(
2841     LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart,
2842     llvm::AtomicOrdering AO, SourceLocation Loc,
2843     const llvm::function_ref<RValue(RValue)> &CommonGen) {
2844   // Update expressions are allowed to have the following forms:
2845   // x binop= expr; -> xrval + expr;
2846   // x++, ++x -> xrval + 1;
2847   // x--, --x -> xrval - 1;
2848   // x = x binop expr; -> xrval binop expr
2849   // x = expr Op x; - > expr binop xrval;
2850   auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart);
2851   if (!Res.first) {
2852     if (X.isGlobalReg()) {
2853       // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop
2854       // 'xrval'.
2855       EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X);
2856     } else {
2857       // Perform compare-and-swap procedure.
2858       EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified());
2859     }
2860   }
2861   return Res;
2862 }
2863 
2864 static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst,
2865                                     const Expr *X, const Expr *E,
2866                                     const Expr *UE, bool IsXLHSInRHSPart,
2867                                     SourceLocation Loc) {
2868   assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
2869          "Update expr in 'atomic update' must be a binary operator.");
2870   auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
2871   // Update expressions are allowed to have the following forms:
2872   // x binop= expr; -> xrval + expr;
2873   // x++, ++x -> xrval + 1;
2874   // x--, --x -> xrval - 1;
2875   // x = x binop expr; -> xrval binop expr
2876   // x = expr Op x; - > expr binop xrval;
2877   assert(X->isLValue() && "X of 'omp atomic update' is not lvalue");
2878   LValue XLValue = CGF.EmitLValue(X);
2879   RValue ExprRValue = CGF.EmitAnyExpr(E);
2880   auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2881                      : llvm::AtomicOrdering::Monotonic;
2882   auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
2883   auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
2884   auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
2885   auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
2886   auto Gen =
2887       [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue {
2888         CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2889         CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
2890         return CGF.EmitAnyExpr(UE);
2891       };
2892   (void)CGF.EmitOMPAtomicSimpleUpdateExpr(
2893       XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
2894   // OpenMP, 2.12.6, atomic Construct
2895   // Any atomic construct with a seq_cst clause forces the atomically
2896   // performed operation to include an implicit flush operation without a
2897   // list.
2898   if (IsSeqCst)
2899     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2900 }
2901 
2902 static RValue convertToType(CodeGenFunction &CGF, RValue Value,
2903                             QualType SourceType, QualType ResType,
2904                             SourceLocation Loc) {
2905   switch (CGF.getEvaluationKind(ResType)) {
2906   case TEK_Scalar:
2907     return RValue::get(
2908         convertToScalarValue(CGF, Value, SourceType, ResType, Loc));
2909   case TEK_Complex: {
2910     auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc);
2911     return RValue::getComplex(Res.first, Res.second);
2912   }
2913   case TEK_Aggregate:
2914     break;
2915   }
2916   llvm_unreachable("Must be a scalar or complex.");
2917 }
2918 
2919 static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst,
2920                                      bool IsPostfixUpdate, const Expr *V,
2921                                      const Expr *X, const Expr *E,
2922                                      const Expr *UE, bool IsXLHSInRHSPart,
2923                                      SourceLocation Loc) {
2924   assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue");
2925   assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue");
2926   RValue NewVVal;
2927   LValue VLValue = CGF.EmitLValue(V);
2928   LValue XLValue = CGF.EmitLValue(X);
2929   RValue ExprRValue = CGF.EmitAnyExpr(E);
2930   auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent
2931                      : llvm::AtomicOrdering::Monotonic;
2932   QualType NewVValType;
2933   if (UE) {
2934     // 'x' is updated with some additional value.
2935     assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) &&
2936            "Update expr in 'atomic capture' must be a binary operator.");
2937     auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts());
2938     // Update expressions are allowed to have the following forms:
2939     // x binop= expr; -> xrval + expr;
2940     // x++, ++x -> xrval + 1;
2941     // x--, --x -> xrval - 1;
2942     // x = x binop expr; -> xrval binop expr
2943     // x = expr Op x; - > expr binop xrval;
2944     auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts());
2945     auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts());
2946     auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS;
2947     NewVValType = XRValExpr->getType();
2948     auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS;
2949     auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr,
2950                   IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue {
2951       CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2952       CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue);
2953       RValue Res = CGF.EmitAnyExpr(UE);
2954       NewVVal = IsPostfixUpdate ? XRValue : Res;
2955       return Res;
2956     };
2957     auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
2958         XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen);
2959     if (Res.first) {
2960       // 'atomicrmw' instruction was generated.
2961       if (IsPostfixUpdate) {
2962         // Use old value from 'atomicrmw'.
2963         NewVVal = Res.second;
2964       } else {
2965         // 'atomicrmw' does not provide new value, so evaluate it using old
2966         // value of 'x'.
2967         CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue);
2968         CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second);
2969         NewVVal = CGF.EmitAnyExpr(UE);
2970       }
2971     }
2972   } else {
2973     // 'x' is simply rewritten with some 'expr'.
2974     NewVValType = X->getType().getNonReferenceType();
2975     ExprRValue = convertToType(CGF, ExprRValue, E->getType(),
2976                                X->getType().getNonReferenceType(), Loc);
2977     auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue {
2978       NewVVal = XRValue;
2979       return ExprRValue;
2980     };
2981     // Try to perform atomicrmw xchg, otherwise simple exchange.
2982     auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr(
2983         XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO,
2984         Loc, Gen);
2985     if (Res.first) {
2986       // 'atomicrmw' instruction was generated.
2987       NewVVal = IsPostfixUpdate ? Res.second : ExprRValue;
2988     }
2989   }
2990   // Emit post-update store to 'v' of old/new 'x' value.
2991   CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc);
2992   // OpenMP, 2.12.6, atomic Construct
2993   // Any atomic construct with a seq_cst clause forces the atomically
2994   // performed operation to include an implicit flush operation without a
2995   // list.
2996   if (IsSeqCst)
2997     CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc);
2998 }
2999 
3000 static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind,
3001                               bool IsSeqCst, bool IsPostfixUpdate,
3002                               const Expr *X, const Expr *V, const Expr *E,
3003                               const Expr *UE, bool IsXLHSInRHSPart,
3004                               SourceLocation Loc) {
3005   switch (Kind) {
3006   case OMPC_read:
3007     EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc);
3008     break;
3009   case OMPC_write:
3010     EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc);
3011     break;
3012   case OMPC_unknown:
3013   case OMPC_update:
3014     EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc);
3015     break;
3016   case OMPC_capture:
3017     EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE,
3018                              IsXLHSInRHSPart, Loc);
3019     break;
3020   case OMPC_if:
3021   case OMPC_final:
3022   case OMPC_num_threads:
3023   case OMPC_private:
3024   case OMPC_firstprivate:
3025   case OMPC_lastprivate:
3026   case OMPC_reduction:
3027   case OMPC_safelen:
3028   case OMPC_simdlen:
3029   case OMPC_collapse:
3030   case OMPC_default:
3031   case OMPC_seq_cst:
3032   case OMPC_shared:
3033   case OMPC_linear:
3034   case OMPC_aligned:
3035   case OMPC_copyin:
3036   case OMPC_copyprivate:
3037   case OMPC_flush:
3038   case OMPC_proc_bind:
3039   case OMPC_schedule:
3040   case OMPC_ordered:
3041   case OMPC_nowait:
3042   case OMPC_untied:
3043   case OMPC_threadprivate:
3044   case OMPC_depend:
3045   case OMPC_mergeable:
3046   case OMPC_device:
3047   case OMPC_threads:
3048   case OMPC_simd:
3049   case OMPC_map:
3050   case OMPC_num_teams:
3051   case OMPC_thread_limit:
3052   case OMPC_priority:
3053   case OMPC_grainsize:
3054   case OMPC_nogroup:
3055   case OMPC_num_tasks:
3056   case OMPC_hint:
3057   case OMPC_dist_schedule:
3058   case OMPC_defaultmap:
3059   case OMPC_uniform:
3060     llvm_unreachable("Clause is not allowed in 'omp atomic'.");
3061   }
3062 }
3063 
3064 void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) {
3065   bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>();
3066   OpenMPClauseKind Kind = OMPC_unknown;
3067   for (auto *C : S.clauses()) {
3068     // Find first clause (skip seq_cst clause, if it is first).
3069     if (C->getClauseKind() != OMPC_seq_cst) {
3070       Kind = C->getClauseKind();
3071       break;
3072     }
3073   }
3074 
3075   const auto *CS =
3076       S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true);
3077   if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) {
3078     enterFullExpression(EWC);
3079   }
3080   // Processing for statements under 'atomic capture'.
3081   if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) {
3082     for (const auto *C : Compound->body()) {
3083       if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) {
3084         enterFullExpression(EWC);
3085       }
3086     }
3087   }
3088 
3089   auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF,
3090                                             PrePostActionTy &) {
3091     CGF.EmitStopPoint(CS);
3092     EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(),
3093                       S.getV(), S.getExpr(), S.getUpdateExpr(),
3094                       S.isXLHSInRHSPart(), S.getLocStart());
3095   };
3096   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
3097   CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen);
3098 }
3099 
3100 std::pair<llvm::Function * /*OutlinedFn*/, llvm::Constant * /*OutlinedFnID*/>
3101 CodeGenFunction::EmitOMPTargetDirectiveOutlinedFunction(
3102     CodeGenModule &CGM, const OMPTargetDirective &S, StringRef ParentName,
3103     bool IsOffloadEntry) {
3104   llvm::Function *OutlinedFn = nullptr;
3105   llvm::Constant *OutlinedFnID = nullptr;
3106   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
3107     OMPPrivateScope PrivateScope(CGF);
3108     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3109     CGF.EmitOMPPrivateClause(S, PrivateScope);
3110     (void)PrivateScope.Privatize();
3111 
3112     Action.Enter(CGF);
3113     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3114   };
3115   // Emit target region as a standalone region.
3116   CGM.getOpenMPRuntime().emitTargetOutlinedFunction(
3117       S, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry, CodeGen);
3118   return std::make_pair(OutlinedFn, OutlinedFnID);
3119 }
3120 
3121 void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) {
3122   const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt());
3123 
3124   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3125   GenerateOpenMPCapturedVars(CS, CapturedVars);
3126 
3127   llvm::Function *Fn = nullptr;
3128   llvm::Constant *FnID = nullptr;
3129 
3130   // Check if we have any if clause associated with the directive.
3131   const Expr *IfCond = nullptr;
3132 
3133   if (auto *C = S.getSingleClause<OMPIfClause>()) {
3134     IfCond = C->getCondition();
3135   }
3136 
3137   // Check if we have any device clause associated with the directive.
3138   const Expr *Device = nullptr;
3139   if (auto *C = S.getSingleClause<OMPDeviceClause>()) {
3140     Device = C->getDevice();
3141   }
3142 
3143   // Check if we have an if clause whose conditional always evaluates to false
3144   // or if we do not have any targets specified. If so the target region is not
3145   // an offload entry point.
3146   bool IsOffloadEntry = true;
3147   if (IfCond) {
3148     bool Val;
3149     if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val)
3150       IsOffloadEntry = false;
3151   }
3152   if (CGM.getLangOpts().OMPTargetTriples.empty())
3153     IsOffloadEntry = false;
3154 
3155   assert(CurFuncDecl && "No parent declaration for target region!");
3156   StringRef ParentName;
3157   // In case we have Ctors/Dtors we use the complete type variant to produce
3158   // the mangling of the device outlined kernel.
3159   if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl))
3160     ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete));
3161   else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl))
3162     ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete));
3163   else
3164     ParentName =
3165         CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl)));
3166 
3167   std::tie(Fn, FnID) = EmitOMPTargetDirectiveOutlinedFunction(
3168       CGM, S, ParentName, IsOffloadEntry);
3169   OMPLexicalScope Scope(*this, S);
3170   CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device,
3171                                         CapturedVars);
3172 }
3173 
3174 static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF,
3175                                         const OMPExecutableDirective &S,
3176                                         OpenMPDirectiveKind InnermostKind,
3177                                         const RegionCodeGenTy &CodeGen) {
3178   auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3179   auto OutlinedFn = CGF.CGM.getOpenMPRuntime().
3180       emitParallelOrTeamsOutlinedFunction(S,
3181           *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen);
3182 
3183   const OMPTeamsDirective &TD = *dyn_cast<OMPTeamsDirective>(&S);
3184   const OMPNumTeamsClause *NT = TD.getSingleClause<OMPNumTeamsClause>();
3185   const OMPThreadLimitClause *TL = TD.getSingleClause<OMPThreadLimitClause>();
3186   if (NT || TL) {
3187     Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr;
3188     Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr;
3189 
3190     CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit,
3191                                                   S.getLocStart());
3192   }
3193 
3194   OMPLexicalScope Scope(CGF, S);
3195   llvm::SmallVector<llvm::Value *, 16> CapturedVars;
3196   CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars);
3197   CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn,
3198                                            CapturedVars);
3199 }
3200 
3201 void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) {
3202   // Emit parallel region as a standalone region.
3203   auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3204     OMPPrivateScope PrivateScope(CGF);
3205     (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope);
3206     CGF.EmitOMPPrivateClause(S, PrivateScope);
3207     (void)PrivateScope.Privatize();
3208     CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3209   };
3210   emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen);
3211 }
3212 
3213 void CodeGenFunction::EmitOMPCancellationPointDirective(
3214     const OMPCancellationPointDirective &S) {
3215   CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(),
3216                                                    S.getCancelRegion());
3217 }
3218 
3219 void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
3220   const Expr *IfCond = nullptr;
3221   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3222     if (C->getNameModifier() == OMPD_unknown ||
3223         C->getNameModifier() == OMPD_cancel) {
3224       IfCond = C->getCondition();
3225       break;
3226     }
3227   }
3228   CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond,
3229                                         S.getCancelRegion());
3230 }
3231 
3232 CodeGenFunction::JumpDest
3233 CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) {
3234   if (Kind == OMPD_parallel || Kind == OMPD_task)
3235     return ReturnBlock;
3236   assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections ||
3237          Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for);
3238   return BreakContinueStack.back().BreakBlock;
3239 }
3240 
3241 // Generate the instructions for '#pragma omp target data' directive.
3242 void CodeGenFunction::EmitOMPTargetDataDirective(
3243     const OMPTargetDataDirective &S) {
3244   // emit the code inside the construct for now
3245   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
3246   CGM.getOpenMPRuntime().emitInlinedDirective(
3247       *this, OMPD_target_data, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3248         CGF.EmitStmt(
3249             cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3250       });
3251 }
3252 
3253 void CodeGenFunction::EmitOMPTargetEnterDataDirective(
3254     const OMPTargetEnterDataDirective &S) {
3255   // TODO: codegen for target enter data.
3256 }
3257 
3258 void CodeGenFunction::EmitOMPTargetExitDataDirective(
3259     const OMPTargetExitDataDirective &S) {
3260   // TODO: codegen for target exit data.
3261 }
3262 
3263 void CodeGenFunction::EmitOMPTargetParallelDirective(
3264     const OMPTargetParallelDirective &S) {
3265   // TODO: codegen for target parallel.
3266 }
3267 
3268 void CodeGenFunction::EmitOMPTargetParallelForDirective(
3269     const OMPTargetParallelForDirective &S) {
3270   // TODO: codegen for target parallel for.
3271 }
3272 
3273 /// Emit a helper variable and return corresponding lvalue.
3274 static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper,
3275                      const ImplicitParamDecl *PVD,
3276                      CodeGenFunction::OMPPrivateScope &Privates) {
3277   auto *VDecl = cast<VarDecl>(Helper->getDecl());
3278   Privates.addPrivate(
3279       VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); });
3280 }
3281 
3282 void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) {
3283   assert(isOpenMPTaskLoopDirective(S.getDirectiveKind()));
3284   // Emit outlined function for task construct.
3285   auto CS = cast<CapturedStmt>(S.getAssociatedStmt());
3286   auto CapturedStruct = GenerateCapturedStmtArgument(*CS);
3287   auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl());
3288   const Expr *IfCond = nullptr;
3289   for (const auto *C : S.getClausesOfKind<OMPIfClause>()) {
3290     if (C->getNameModifier() == OMPD_unknown ||
3291         C->getNameModifier() == OMPD_taskloop) {
3292       IfCond = C->getCondition();
3293       break;
3294     }
3295   }
3296   bool Nogroup = S.getSingleClause<OMPNogroupClause>();
3297   // TODO: Check if we should emit tied or untied task.
3298   // Check if the task is final
3299   llvm::PointerIntPair<llvm::Value *, 1, bool> Final;
3300   if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) {
3301     // If the condition constant folds and can be elided, try to avoid emitting
3302     // the condition and the dead arm of the if/else.
3303     auto *Cond = Clause->getCondition();
3304     bool CondConstant;
3305     if (ConstantFoldsToSimpleInteger(Cond, CondConstant))
3306       Final.setInt(CondConstant);
3307     else
3308       Final.setPointer(EvaluateExprAsBool(Cond));
3309   } else {
3310     // By default the task is not final.
3311     Final.setInt(/*IntVal=*/false);
3312   }
3313 
3314   auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) {
3315     // if (PreCond) {
3316     //   for (IV in 0..LastIteration) BODY;
3317     //   <Final counter/linear vars updates>;
3318     // }
3319     //
3320 
3321     // Emit: if (PreCond) - begin.
3322     // If the condition constant folds and can be elided, avoid emitting the
3323     // whole loop.
3324     bool CondConstant;
3325     llvm::BasicBlock *ContBlock = nullptr;
3326     OMPLoopScope PreInitScope(CGF, S);
3327     if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
3328       if (!CondConstant)
3329         return;
3330     } else {
3331       auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then");
3332       ContBlock = CGF.createBasicBlock("taskloop.if.end");
3333       emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock,
3334                   CGF.getProfileCount(&S));
3335       CGF.EmitBlock(ThenBlock);
3336       CGF.incrementProfileCounter(&S);
3337     }
3338 
3339     OMPPrivateScope LoopScope(CGF);
3340     // Emit helper vars inits.
3341     enum { LowerBound = 5, UpperBound, Stride, LastIter };
3342     auto *I = CS->getCapturedDecl()->param_begin();
3343     auto *LBP = std::next(I, LowerBound);
3344     auto *UBP = std::next(I, UpperBound);
3345     auto *STP = std::next(I, Stride);
3346     auto *LIP = std::next(I, LastIter);
3347     mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP,
3348              LoopScope);
3349     mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP,
3350              LoopScope);
3351     mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope);
3352     mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP,
3353              LoopScope);
3354     CGF.EmitOMPPrivateLoopCounters(S, LoopScope);
3355     (void)LoopScope.Privatize();
3356     // Emit the loop iteration variable.
3357     const Expr *IVExpr = S.getIterationVariable();
3358     const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl());
3359     CGF.EmitVarDecl(*IVDecl);
3360     CGF.EmitIgnoredExpr(S.getInit());
3361 
3362     // Emit the iterations count variable.
3363     // If it is not a variable, Sema decided to calculate iterations count on
3364     // each iteration (e.g., it is foldable into a constant).
3365     if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) {
3366       CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl()));
3367       // Emit calculation of the iterations count.
3368       CGF.EmitIgnoredExpr(S.getCalcLastIteration());
3369     }
3370 
3371     CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(),
3372                          S.getInc(),
3373                          [&S](CodeGenFunction &CGF) {
3374                            CGF.EmitOMPLoopBody(S, JumpDest());
3375                            CGF.EmitStopPoint(&S);
3376                          },
3377                          [](CodeGenFunction &) {});
3378     // Emit: if (PreCond) - end.
3379     if (ContBlock) {
3380       CGF.EmitBranch(ContBlock);
3381       CGF.EmitBlock(ContBlock, true);
3382     }
3383   };
3384   auto &&TaskGen = [&S, SharedsTy, CapturedStruct, IfCond, &Final,
3385                     Nogroup](CodeGenFunction &CGF, llvm::Value *OutlinedFn,
3386                              const OMPPrivateDataTy &Data) {
3387     auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) {
3388       OMPLoopScope PreInitScope(CGF, S);
3389       CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(
3390           CGF, S.getLocStart(), S, Data.Tied, Final, Nogroup,
3391           Data.NumberOfParts, OutlinedFn, SharedsTy, CapturedStruct, IfCond,
3392           Data.PrivateVars, Data.PrivateCopies, Data.FirstprivateVars,
3393           Data.FirstprivateCopies, Data.FirstprivateInits);
3394     };
3395     CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop,
3396                                                     CodeGen);
3397   };
3398   EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, /*Tied=*/true);
3399 }
3400 
3401 void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) {
3402   EmitOMPTaskLoopBasedDirective(S);
3403 }
3404 
3405 void CodeGenFunction::EmitOMPTaskLoopSimdDirective(
3406     const OMPTaskLoopSimdDirective &S) {
3407   // emit the code inside the construct for now
3408   OMPLexicalScope Scope(*this, S, /*AsInlined=*/true);
3409   CGM.getOpenMPRuntime().emitInlinedDirective(
3410       *this, OMPD_taskloop_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) {
3411         OMPLoopScope PreInitScope(CGF, S);
3412         CGF.EmitStmt(
3413             cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
3414       });
3415 }
3416