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