]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGDeclCXX.cpp
Bring LLVM libunwind snapshot into contrib/llvm/projects
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGDeclCXX.cpp
1 //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
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 dealing with code generation of C++ declarations
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "CodeGenFunction.h"
15 #include "CGCXXABI.h"
16 #include "CGObjCRuntime.h"
17 #include "CGOpenMPRuntime.h"
18 #include "clang/Frontend/CodeGenOptions.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/Support/Path.h"
22
23 using namespace clang;
24 using namespace CodeGen;
25
26 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
27                          llvm::Constant *DeclPtr) {
28   assert(D.hasGlobalStorage() && "VarDecl must have global storage!");
29   assert(!D.getType()->isReferenceType() && 
30          "Should not call EmitDeclInit on a reference!");
31   
32   ASTContext &Context = CGF.getContext();
33
34   CharUnits alignment = Context.getDeclAlign(&D);
35   QualType type = D.getType();
36   LValue lv = CGF.MakeAddrLValue(DeclPtr, type, alignment);
37
38   const Expr *Init = D.getInit();
39   switch (CGF.getEvaluationKind(type)) {
40   case TEK_Scalar: {
41     CodeGenModule &CGM = CGF.CGM;
42     if (lv.isObjCStrong())
43       CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init),
44                                                 DeclPtr, D.getTLSKind());
45     else if (lv.isObjCWeak())
46       CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init),
47                                               DeclPtr);
48     else
49       CGF.EmitScalarInit(Init, &D, lv, false);
50     return;
51   }
52   case TEK_Complex:
53     CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
54     return;
55   case TEK_Aggregate:
56     CGF.EmitAggExpr(Init, AggValueSlot::forLValue(lv,AggValueSlot::IsDestructed,
57                                           AggValueSlot::DoesNotNeedGCBarriers,
58                                                   AggValueSlot::IsNotAliased));
59     return;
60   }
61   llvm_unreachable("bad evaluation kind");
62 }
63
64 /// Emit code to cause the destruction of the given variable with
65 /// static storage duration.
66 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
67                             llvm::Constant *addr) {
68   CodeGenModule &CGM = CGF.CGM;
69
70   // FIXME:  __attribute__((cleanup)) ?
71   
72   QualType type = D.getType();
73   QualType::DestructionKind dtorKind = type.isDestructedType();
74
75   switch (dtorKind) {
76   case QualType::DK_none:
77     return;
78
79   case QualType::DK_cxx_destructor:
80     break;
81
82   case QualType::DK_objc_strong_lifetime:
83   case QualType::DK_objc_weak_lifetime:
84     // We don't care about releasing objects during process teardown.
85     assert(!D.getTLSKind() && "should have rejected this");
86     return;
87   }
88
89   llvm::Constant *function;
90   llvm::Constant *argument;
91
92   // Special-case non-array C++ destructors, where there's a function
93   // with the right signature that we can just call.
94   const CXXRecordDecl *record = nullptr;
95   if (dtorKind == QualType::DK_cxx_destructor &&
96       (record = type->getAsCXXRecordDecl())) {
97     assert(!record->hasTrivialDestructor());
98     CXXDestructorDecl *dtor = record->getDestructor();
99
100     function = CGM.getAddrOfCXXStructor(dtor, StructorType::Complete);
101     argument = llvm::ConstantExpr::getBitCast(
102         addr, CGF.getTypes().ConvertType(type)->getPointerTo());
103
104   // Otherwise, the standard logic requires a helper function.
105   } else {
106     function = CodeGenFunction(CGM)
107         .generateDestroyHelper(addr, type, CGF.getDestroyer(dtorKind),
108                                CGF.needsEHCleanup(dtorKind), &D);
109     argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
110   }
111
112   CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument);
113 }
114
115 /// Emit code to cause the variable at the given address to be considered as
116 /// constant from this point onwards.
117 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
118                               llvm::Constant *Addr) {
119   // Don't emit the intrinsic if we're not optimizing.
120   if (!CGF.CGM.getCodeGenOpts().OptimizationLevel)
121     return;
122
123   // Grab the llvm.invariant.start intrinsic.
124   llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
125   llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
126
127   // Emit a call with the size in bytes of the object.
128   CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType());
129   uint64_t Width = WidthChars.getQuantity();
130   llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width),
131                            llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
132   CGF.Builder.CreateCall(InvariantStart, Args);
133 }
134
135 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
136                                                llvm::Constant *DeclPtr,
137                                                bool PerformInit) {
138
139   const Expr *Init = D.getInit();
140   QualType T = D.getType();
141
142   if (!T->isReferenceType()) {
143     if (getLangOpts().OpenMP && D.hasAttr<OMPThreadPrivateDeclAttr>())
144       (void)CGM.getOpenMPRuntime().EmitOMPThreadPrivateVarDefinition(
145           &D, DeclPtr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
146           PerformInit, this);
147     if (PerformInit)
148       EmitDeclInit(*this, D, DeclPtr);
149     if (CGM.isTypeConstant(D.getType(), true))
150       EmitDeclInvariant(*this, D, DeclPtr);
151     else
152       EmitDeclDestroy(*this, D, DeclPtr);
153     return;
154   }
155
156   assert(PerformInit && "cannot have constant initializer which needs "
157          "destruction for reference");
158   unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
159   RValue RV = EmitReferenceBindingToExpr(Init);
160   EmitStoreOfScalar(RV.getScalarVal(), DeclPtr, false, Alignment, T);
161 }
162
163 /// Create a stub function, suitable for being passed to atexit,
164 /// which passes the given address to the given destructor function.
165 llvm::Constant *CodeGenFunction::createAtExitStub(const VarDecl &VD,
166                                                   llvm::Constant *dtor,
167                                                   llvm::Constant *addr) {
168   // Get the destructor function type, void(*)(void).
169   llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
170   SmallString<256> FnName;
171   {
172     llvm::raw_svector_ostream Out(FnName);
173     CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out);
174   }
175   llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(ty, FnName.str(),
176                                                               VD.getLocation());
177
178   CodeGenFunction CGF(CGM);
179
180   CGF.StartFunction(&VD, CGM.getContext().VoidTy, fn,
181                     CGM.getTypes().arrangeNullaryFunction(), FunctionArgList());
182
183   llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
184  
185  // Make sure the call and the callee agree on calling convention.
186   if (llvm::Function *dtorFn =
187         dyn_cast<llvm::Function>(dtor->stripPointerCasts()))
188     call->setCallingConv(dtorFn->getCallingConv());
189
190   CGF.FinishFunction();
191
192   return fn;
193 }
194
195 /// Register a global destructor using the C atexit runtime function.
196 void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD,
197                                                    llvm::Constant *dtor,
198                                                    llvm::Constant *addr) {
199   // Create a function which calls the destructor.
200   llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
201
202   // extern "C" int atexit(void (*f)(void));
203   llvm::FunctionType *atexitTy =
204     llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
205
206   llvm::Constant *atexit =
207     CGM.CreateRuntimeFunction(atexitTy, "atexit");
208   if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit))
209     atexitFn->setDoesNotThrow();
210
211   EmitNounwindRuntimeCall(atexit, dtorStub);
212 }
213
214 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D,
215                                          llvm::GlobalVariable *DeclPtr,
216                                          bool PerformInit) {
217   // If we've been asked to forbid guard variables, emit an error now.
218   // This diagnostic is hard-coded for Darwin's use case;  we can find
219   // better phrasing if someone else needs it.
220   if (CGM.getCodeGenOpts().ForbidGuardVariables)
221     CGM.Error(D.getLocation(),
222               "this initialization requires a guard variable, which "
223               "the kernel does not support");
224
225   CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
226 }
227
228 llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
229     llvm::FunctionType *FTy, const Twine &Name, SourceLocation Loc, bool TLS) {
230   llvm::Function *Fn =
231     llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage,
232                            Name, &getModule());
233   if (!getLangOpts().AppleKext && !TLS) {
234     // Set the section if needed.
235     if (const char *Section = getTarget().getStaticInitSectionSpecifier())
236       Fn->setSection(Section);
237   }
238
239   Fn->setCallingConv(getRuntimeCC());
240
241   if (!getLangOpts().Exceptions)
242     Fn->setDoesNotThrow();
243
244   if (!isInSanitizerBlacklist(Fn, Loc)) {
245     if (getLangOpts().Sanitize.has(SanitizerKind::Address))
246       Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
247     if (getLangOpts().Sanitize.has(SanitizerKind::Thread))
248       Fn->addFnAttr(llvm::Attribute::SanitizeThread);
249     if (getLangOpts().Sanitize.has(SanitizerKind::Memory))
250       Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
251   }
252
253   return Fn;
254 }
255
256 /// Create a global pointer to a function that will initialize a global
257 /// variable.  The user has requested that this pointer be emitted in a specific
258 /// section.
259 void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
260                                           llvm::GlobalVariable *GV,
261                                           llvm::Function *InitFunc,
262                                           InitSegAttr *ISA) {
263   llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
264       TheModule, InitFunc->getType(), /*isConstant=*/true,
265       llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
266   PtrArray->setSection(ISA->getSection());
267   addUsedGlobal(PtrArray);
268
269   // If the GV is already in a comdat group, then we have to join it.
270   llvm::Comdat *C = GV->getComdat();
271
272   // LinkOnce and Weak linkage are lowered down to a single-member comdat group.
273   // Make an explicit group so we can join it.
274   if (!C && (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())) {
275     C = TheModule.getOrInsertComdat(GV->getName());
276     GV->setComdat(C);
277   }
278   if (C)
279     PtrArray->setComdat(C);
280 }
281
282 void
283 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
284                                             llvm::GlobalVariable *Addr,
285                                             bool PerformInit) {
286   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
287   SmallString<256> FnName;
288   {
289     llvm::raw_svector_ostream Out(FnName);
290     getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out);
291   }
292
293   // Create a variable initialization function.
294   llvm::Function *Fn =
295       CreateGlobalInitOrDestructFunction(FTy, FnName.str(), D->getLocation());
296
297   auto *ISA = D->getAttr<InitSegAttr>();
298   CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr,
299                                                           PerformInit);
300
301   llvm::GlobalVariable *COMDATKey =
302       supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
303
304   if (D->getTLSKind()) {
305     // FIXME: Should we support init_priority for thread_local?
306     // FIXME: Ideally, initialization of instantiated thread_local static data
307     // members of class templates should not trigger initialization of other
308     // entities in the TU.
309     // FIXME: We only need to register one __cxa_thread_atexit function for the
310     // entire TU.
311     CXXThreadLocalInits.push_back(Fn);
312     CXXThreadLocalInitVars.push_back(Addr);
313   } else if (PerformInit && ISA) {
314     EmitPointerToInitFunc(D, Addr, Fn, ISA);
315     DelayedCXXInitPosition.erase(D);
316   } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
317     OrderGlobalInits Key(IPA->getPriority(), PrioritizedCXXGlobalInits.size());
318     PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
319     DelayedCXXInitPosition.erase(D);
320   } else if (isTemplateInstantiation(D->getTemplateSpecializationKind())) {
321     // C++ [basic.start.init]p2:
322     //   Definitions of explicitly specialized class template static data
323     //   members have ordered initialization. Other class template static data
324     //   members (i.e., implicitly or explicitly instantiated specializations)
325     //   have unordered initialization.
326     //
327     // As a consequence, we can put them into their own llvm.global_ctors entry.
328     //
329     // If the global is externally visible, put the initializer into a COMDAT
330     // group with the global being initialized.  On most platforms, this is a
331     // minor startup time optimization.  In the MS C++ ABI, there are no guard
332     // variables, so this COMDAT key is required for correctness.
333     AddGlobalCtor(Fn, 65535, COMDATKey);
334     DelayedCXXInitPosition.erase(D);
335   } else if (D->hasAttr<SelectAnyAttr>()) {
336     // SelectAny globals will be comdat-folded. Put the initializer into a
337     // COMDAT group associated with the global, so the initializers get folded
338     // too.
339     AddGlobalCtor(Fn, 65535, COMDATKey);
340     DelayedCXXInitPosition.erase(D);
341   } else {
342     llvm::DenseMap<const Decl *, unsigned>::iterator I =
343       DelayedCXXInitPosition.find(D);
344     if (I == DelayedCXXInitPosition.end()) {
345       CXXGlobalInits.push_back(Fn);
346     } else {
347       assert(CXXGlobalInits[I->second] == nullptr);
348       CXXGlobalInits[I->second] = Fn;
349       DelayedCXXInitPosition.erase(I);
350     }
351   }
352 }
353
354 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
355   getCXXABI().EmitThreadLocalInitFuncs(
356       *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
357
358   CXXThreadLocalInits.clear();
359   CXXThreadLocalInitVars.clear();
360   CXXThreadLocals.clear();
361 }
362
363 void
364 CodeGenModule::EmitCXXGlobalInitFunc() {
365   while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
366     CXXGlobalInits.pop_back();
367
368   if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
369     return;
370
371   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
372
373
374   // Create our global initialization function.
375   if (!PrioritizedCXXGlobalInits.empty()) {
376     SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
377     llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), 
378                          PrioritizedCXXGlobalInits.end());
379     // Iterate over "chunks" of ctors with same priority and emit each chunk
380     // into separate function. Note - everything is sorted first by priority,
381     // second - by lex order, so we emit ctor functions in proper order.
382     for (SmallVectorImpl<GlobalInitData >::iterator
383            I = PrioritizedCXXGlobalInits.begin(),
384            E = PrioritizedCXXGlobalInits.end(); I != E; ) {
385       SmallVectorImpl<GlobalInitData >::iterator
386         PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
387
388       LocalCXXGlobalInits.clear();
389       unsigned Priority = I->first.priority;
390       // Compute the function suffix from priority. Prepend with zeroes to make
391       // sure the function names are also ordered as priorities.
392       std::string PrioritySuffix = llvm::utostr(Priority);
393       // Priority is always <= 65535 (enforced by sema).
394       PrioritySuffix = std::string(6-PrioritySuffix.size(), '0')+PrioritySuffix;
395       llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
396           FTy, "_GLOBAL__I_" + PrioritySuffix);
397
398       for (; I < PrioE; ++I)
399         LocalCXXGlobalInits.push_back(I->second);
400
401       CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
402       AddGlobalCtor(Fn, Priority);
403     }
404   }
405
406   SmallString<128> FileName;
407   SourceManager &SM = Context.getSourceManager();
408   if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
409     // Include the filename in the symbol name. Including "sub_" matches gcc and
410     // makes sure these symbols appear lexicographically behind the symbols with
411     // priority emitted above.
412     FileName = llvm::sys::path::filename(MainFile->getName());
413   } else {
414     FileName = SmallString<128>("<null>");
415   }
416
417   for (size_t i = 0; i < FileName.size(); ++i) {
418     // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
419     // to be the set of C preprocessing numbers.
420     if (!isPreprocessingNumberBody(FileName[i]))
421       FileName[i] = '_';
422   }
423
424   llvm::Function *Fn = CreateGlobalInitOrDestructFunction(
425       FTy, llvm::Twine("_GLOBAL__sub_I_", FileName));
426
427   CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
428   AddGlobalCtor(Fn);
429
430   CXXGlobalInits.clear();
431   PrioritizedCXXGlobalInits.clear();
432 }
433
434 void CodeGenModule::EmitCXXGlobalDtorFunc() {
435   if (CXXGlobalDtors.empty())
436     return;
437
438   llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
439
440   // Create our global destructor function.
441   llvm::Function *Fn = CreateGlobalInitOrDestructFunction(FTy, "_GLOBAL__D_a");
442
443   CodeGenFunction(*this).GenerateCXXGlobalDtorsFunc(Fn, CXXGlobalDtors);
444   AddGlobalDtor(Fn);
445 }
446
447 /// Emit the code necessary to initialize the given global variable.
448 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
449                                                        const VarDecl *D,
450                                                  llvm::GlobalVariable *Addr,
451                                                        bool PerformInit) {
452   // Check if we need to emit debug info for variable initializer.
453   if (D->hasAttr<NoDebugAttr>())
454     DebugInfo = nullptr; // disable debug info indefinitely for this function
455
456   CurEHLocation = D->getLocStart();
457
458   StartFunction(GlobalDecl(D), getContext().VoidTy, Fn,
459                 getTypes().arrangeNullaryFunction(),
460                 FunctionArgList(), D->getLocation(),
461                 D->getInit()->getExprLoc());
462
463   // Use guarded initialization if the global variable is weak. This
464   // occurs for, e.g., instantiated static data members and
465   // definitions explicitly marked weak.
466   if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage()) {
467     EmitCXXGuardedInit(*D, Addr, PerformInit);
468   } else {
469     EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
470   }
471
472   FinishFunction();
473 }
474
475 void
476 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn,
477                                            ArrayRef<llvm::Function *> Decls,
478                                            llvm::GlobalVariable *Guard) {
479   {
480     ApplyDebugLocation NL(*this);
481     StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
482                   getTypes().arrangeNullaryFunction(), FunctionArgList());
483     // Emit an artificial location for this function.
484     ArtificialLocation AL(*this);
485
486     llvm::BasicBlock *ExitBlock = nullptr;
487     if (Guard) {
488       // If we have a guard variable, check whether we've already performed
489       // these initializations. This happens for TLS initialization functions.
490       llvm::Value *GuardVal = Builder.CreateLoad(Guard);
491       llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
492                                                  "guard.uninitialized");
493       // Mark as initialized before initializing anything else. If the
494       // initializers use previously-initialized thread_local vars, that's
495       // probably supposed to be OK, but the standard doesn't say.
496       Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
497       llvm::BasicBlock *InitBlock = createBasicBlock("init");
498       ExitBlock = createBasicBlock("exit");
499       Builder.CreateCondBr(Uninit, InitBlock, ExitBlock);
500       EmitBlock(InitBlock);
501     }
502
503     RunCleanupsScope Scope(*this);
504
505     // When building in Objective-C++ ARC mode, create an autorelease pool
506     // around the global initializers.
507     if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
508       llvm::Value *token = EmitObjCAutoreleasePoolPush();
509       EmitObjCAutoreleasePoolCleanup(token);
510     }
511
512     for (unsigned i = 0, e = Decls.size(); i != e; ++i)
513       if (Decls[i])
514         EmitRuntimeCall(Decls[i]);
515
516     Scope.ForceCleanup();
517
518     if (ExitBlock) {
519       Builder.CreateBr(ExitBlock);
520       EmitBlock(ExitBlock);
521     }
522   }
523
524   FinishFunction();
525 }
526
527 void CodeGenFunction::GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
528                   const std::vector<std::pair<llvm::WeakVH, llvm::Constant*> >
529                                                 &DtorsAndObjects) {
530   {
531     ApplyDebugLocation NL(*this);
532     StartFunction(GlobalDecl(), getContext().VoidTy, Fn,
533                   getTypes().arrangeNullaryFunction(), FunctionArgList());
534     // Emit an artificial location for this function.
535     ArtificialLocation AL(*this);
536
537     // Emit the dtors, in reverse order from construction.
538     for (unsigned i = 0, e = DtorsAndObjects.size(); i != e; ++i) {
539       llvm::Value *Callee = DtorsAndObjects[e - i - 1].first;
540       llvm::CallInst *CI = Builder.CreateCall(Callee,
541                                           DtorsAndObjects[e - i - 1].second);
542       // Make sure the call and the callee agree on calling convention.
543       if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
544         CI->setCallingConv(F->getCallingConv());
545     }
546   }
547
548   FinishFunction();
549 }
550
551 /// generateDestroyHelper - Generates a helper function which, when
552 /// invoked, destroys the given object.
553 llvm::Function *CodeGenFunction::generateDestroyHelper(
554     llvm::Constant *addr, QualType type, Destroyer *destroyer,
555     bool useEHCleanupForArray, const VarDecl *VD) {
556   FunctionArgList args;
557   ImplicitParamDecl dst(getContext(), nullptr, SourceLocation(), nullptr,
558                         getContext().VoidPtrTy);
559   args.push_back(&dst);
560
561   const CGFunctionInfo &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
562       getContext().VoidTy, args, FunctionType::ExtInfo(), /*variadic=*/false);
563   llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
564   llvm::Function *fn = CGM.CreateGlobalInitOrDestructFunction(
565       FTy, "__cxx_global_array_dtor", VD->getLocation());
566
567   CurEHLocation = VD->getLocStart();
568
569   StartFunction(VD, getContext().VoidTy, fn, FI, args);
570
571   emitDestroy(addr, type, destroyer, useEHCleanupForArray);
572   
573   FinishFunction();
574   
575   return fn;
576 }