CGDecl.cpp revision 60d3365b46eb826fba44483583c0051ac5c41fe3
1//===--- CGDecl.cpp - Emit LLVM Code for 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 to emit Decl nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
15#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
17#include "CGBlocks.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/CharUnits.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Frontend/CodeGenOptions.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Type.h"
29using namespace clang;
30using namespace CodeGen;
31
32
33void CodeGenFunction::EmitDecl(const Decl &D) {
34  switch (D.getKind()) {
35  case Decl::TranslationUnit:
36  case Decl::Namespace:
37  case Decl::UnresolvedUsingTypename:
38  case Decl::ClassTemplateSpecialization:
39  case Decl::ClassTemplatePartialSpecialization:
40  case Decl::TemplateTypeParm:
41  case Decl::UnresolvedUsingValue:
42  case Decl::NonTypeTemplateParm:
43  case Decl::CXXMethod:
44  case Decl::CXXConstructor:
45  case Decl::CXXDestructor:
46  case Decl::CXXConversion:
47  case Decl::Field:
48  case Decl::IndirectField:
49  case Decl::ObjCIvar:
50  case Decl::ObjCAtDefsField:
51  case Decl::ParmVar:
52  case Decl::ImplicitParam:
53  case Decl::ClassTemplate:
54  case Decl::FunctionTemplate:
55  case Decl::TemplateTemplateParm:
56  case Decl::ObjCMethod:
57  case Decl::ObjCCategory:
58  case Decl::ObjCProtocol:
59  case Decl::ObjCInterface:
60  case Decl::ObjCCategoryImpl:
61  case Decl::ObjCImplementation:
62  case Decl::ObjCProperty:
63  case Decl::ObjCCompatibleAlias:
64  case Decl::AccessSpec:
65  case Decl::LinkageSpec:
66  case Decl::ObjCPropertyImpl:
67  case Decl::ObjCClass:
68  case Decl::ObjCForwardProtocol:
69  case Decl::FileScopeAsm:
70  case Decl::Friend:
71  case Decl::FriendTemplate:
72  case Decl::Block:
73    assert(0 && "Declaration not should not be in declstmts!");
74  case Decl::Function:  // void X();
75  case Decl::Record:    // struct/union/class X;
76  case Decl::Enum:      // enum X;
77  case Decl::EnumConstant: // enum ? { X = ? }
78  case Decl::CXXRecord: // struct/union/class X; [C++]
79  case Decl::Using:          // using X; [C++]
80  case Decl::UsingShadow:
81  case Decl::UsingDirective: // using namespace X; [C++]
82  case Decl::NamespaceAlias:
83  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
84  case Decl::Label:        // __label__ x;
85    // None of these decls require codegen support.
86    return;
87
88  case Decl::Var: {
89    const VarDecl &VD = cast<VarDecl>(D);
90    assert(VD.isLocalVarDecl() &&
91           "Should not see file-scope variables inside a function!");
92    return EmitVarDecl(VD);
93  }
94
95  case Decl::Typedef: {   // typedef int X;
96    const TypedefDecl &TD = cast<TypedefDecl>(D);
97    QualType Ty = TD.getUnderlyingType();
98
99    if (Ty->isVariablyModifiedType())
100      EmitVLASize(Ty);
101  }
102  }
103}
104
105/// EmitVarDecl - This method handles emission of any variable declaration
106/// inside a function, including static vars etc.
107void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
108  switch (D.getStorageClass()) {
109  case SC_None:
110  case SC_Auto:
111  case SC_Register:
112    return EmitAutoVarDecl(D);
113  case SC_Static: {
114    llvm::GlobalValue::LinkageTypes Linkage =
115      llvm::GlobalValue::InternalLinkage;
116
117    // If the function definition has some sort of weak linkage, its
118    // static variables should also be weak so that they get properly
119    // uniqued.  We can't do this in C, though, because there's no
120    // standard way to agree on which variables are the same (i.e.
121    // there's no mangling).
122    if (getContext().getLangOptions().CPlusPlus)
123      if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
124        Linkage = CurFn->getLinkage();
125
126    return EmitStaticVarDecl(D, Linkage);
127  }
128  case SC_Extern:
129  case SC_PrivateExtern:
130    // Don't emit it now, allow it to be emitted lazily on its first use.
131    return;
132  }
133
134  assert(0 && "Unknown storage class");
135}
136
137static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
138                                     const char *Separator) {
139  CodeGenModule &CGM = CGF.CGM;
140  if (CGF.getContext().getLangOptions().CPlusPlus) {
141    llvm::StringRef Name = CGM.getMangledName(&D);
142    return Name.str();
143  }
144
145  std::string ContextName;
146  if (!CGF.CurFuncDecl) {
147    // Better be in a block declared in global scope.
148    const NamedDecl *ND = cast<NamedDecl>(&D);
149    const DeclContext *DC = ND->getDeclContext();
150    if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
151      MangleBuffer Name;
152      CGM.getBlockMangledName(GlobalDecl(), Name, BD);
153      ContextName = Name.getString();
154    }
155    else
156      assert(0 && "Unknown context for block static var decl");
157  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
158    llvm::StringRef Name = CGM.getMangledName(FD);
159    ContextName = Name.str();
160  } else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
161    ContextName = CGF.CurFn->getName();
162  else
163    assert(0 && "Unknown context for static var decl");
164
165  return ContextName + Separator + D.getNameAsString();
166}
167
168llvm::GlobalVariable *
169CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
170                                     const char *Separator,
171                                     llvm::GlobalValue::LinkageTypes Linkage) {
172  QualType Ty = D.getType();
173  assert(Ty->isConstantSizeType() && "VLAs can't be static");
174
175  std::string Name = GetStaticDeclName(*this, D, Separator);
176
177  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
178  llvm::GlobalVariable *GV =
179    new llvm::GlobalVariable(CGM.getModule(), LTy,
180                             Ty.isConstant(getContext()), Linkage,
181                             CGM.EmitNullConstant(D.getType()), Name, 0,
182                             D.isThreadSpecified(), Ty.getAddressSpace());
183  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
184  if (Linkage != llvm::GlobalValue::InternalLinkage)
185    GV->setVisibility(CurFn->getVisibility());
186  return GV;
187}
188
189/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
190/// global variable that has already been created for it.  If the initializer
191/// has a different type than GV does, this may free GV and return a different
192/// one.  Otherwise it just returns GV.
193llvm::GlobalVariable *
194CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
195                                               llvm::GlobalVariable *GV) {
196  llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
197
198  // If constant emission failed, then this should be a C++ static
199  // initializer.
200  if (!Init) {
201    if (!getContext().getLangOptions().CPlusPlus)
202      CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
203    else if (Builder.GetInsertBlock()) {
204      // Since we have a static initializer, this global variable can't
205      // be constant.
206      GV->setConstant(false);
207
208      EmitCXXGuardedInit(D, GV);
209    }
210    return GV;
211  }
212
213  // The initializer may differ in type from the global. Rewrite
214  // the global to match the initializer.  (We have to do this
215  // because some types, like unions, can't be completely represented
216  // in the LLVM type system.)
217  if (GV->getType()->getElementType() != Init->getType()) {
218    llvm::GlobalVariable *OldGV = GV;
219
220    GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
221                                  OldGV->isConstant(),
222                                  OldGV->getLinkage(), Init, "",
223                                  /*InsertBefore*/ OldGV,
224                                  D.isThreadSpecified(),
225                                  D.getType().getAddressSpace());
226    GV->setVisibility(OldGV->getVisibility());
227
228    // Steal the name of the old global
229    GV->takeName(OldGV);
230
231    // Replace all uses of the old global with the new global
232    llvm::Constant *NewPtrForOldDecl =
233    llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
234    OldGV->replaceAllUsesWith(NewPtrForOldDecl);
235
236    // Erase the old global, since it is no longer used.
237    OldGV->eraseFromParent();
238  }
239
240  GV->setInitializer(Init);
241  return GV;
242}
243
244void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
245                                      llvm::GlobalValue::LinkageTypes Linkage) {
246  llvm::Value *&DMEntry = LocalDeclMap[&D];
247  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
248
249  llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
250
251  // Store into LocalDeclMap before generating initializer to handle
252  // circular references.
253  DMEntry = GV;
254
255  // We can't have a VLA here, but we can have a pointer to a VLA,
256  // even though that doesn't really make any sense.
257  // Make sure to evaluate VLA bounds now so that we have them for later.
258  if (D.getType()->isVariablyModifiedType())
259    EmitVLASize(D.getType());
260
261  // Local static block variables must be treated as globals as they may be
262  // referenced in their RHS initializer block-literal expresion.
263  CGM.setStaticLocalDeclAddress(&D, GV);
264
265  // If this value has an initializer, emit it.
266  if (D.getInit())
267    GV = AddInitializerToStaticVarDecl(D, GV);
268
269  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
270
271  // FIXME: Merge attribute handling.
272  if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
273    SourceManager &SM = CGM.getContext().getSourceManager();
274    llvm::Constant *Ann =
275      CGM.EmitAnnotateAttr(GV, AA,
276                           SM.getInstantiationLineNumber(D.getLocation()));
277    CGM.AddAnnotation(Ann);
278  }
279
280  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
281    GV->setSection(SA->getName());
282
283  if (D.hasAttr<UsedAttr>())
284    CGM.AddUsedGlobal(GV);
285
286  // We may have to cast the constant because of the initializer
287  // mismatch above.
288  //
289  // FIXME: It is really dangerous to store this in the map; if anyone
290  // RAUW's the GV uses of this constant will be invalid.
291  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
292  const llvm::Type *LPtrTy = LTy->getPointerTo(D.getType().getAddressSpace());
293  DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
294
295  // Emit global variable debug descriptor for static vars.
296  CGDebugInfo *DI = getDebugInfo();
297  if (DI) {
298    DI->setLocation(D.getLocation());
299    DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
300  }
301}
302
303unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
304  assert(ByRefValueInfo.count(VD) && "Did not find value!");
305
306  return ByRefValueInfo.find(VD)->second.second;
307}
308
309llvm::Value *CodeGenFunction::BuildBlockByrefAddress(llvm::Value *BaseAddr,
310                                                     const VarDecl *V) {
311  llvm::Value *Loc = Builder.CreateStructGEP(BaseAddr, 1, "forwarding");
312  Loc = Builder.CreateLoad(Loc);
313  Loc = Builder.CreateStructGEP(Loc, getByRefValueLLVMField(V),
314                                V->getNameAsString());
315  return Loc;
316}
317
318/// BuildByRefType - This routine changes a __block variable declared as T x
319///   into:
320///
321///      struct {
322///        void *__isa;
323///        void *__forwarding;
324///        int32_t __flags;
325///        int32_t __size;
326///        void *__copy_helper;       // only if needed
327///        void *__destroy_helper;    // only if needed
328///        char padding[X];           // only if needed
329///        T x;
330///      } x
331///
332const llvm::Type *CodeGenFunction::BuildByRefType(const VarDecl *D) {
333  std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
334  if (Info.first)
335    return Info.first;
336
337  QualType Ty = D->getType();
338
339  std::vector<const llvm::Type *> Types;
340
341  llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(getLLVMContext());
342
343  // void *__isa;
344  Types.push_back(Int8PtrTy);
345
346  // void *__forwarding;
347  Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
348
349  // int32_t __flags;
350  Types.push_back(Int32Ty);
351
352  // int32_t __size;
353  Types.push_back(Int32Ty);
354
355  bool HasCopyAndDispose = getContext().BlockRequiresCopying(Ty);
356  if (HasCopyAndDispose) {
357    /// void *__copy_helper;
358    Types.push_back(Int8PtrTy);
359
360    /// void *__destroy_helper;
361    Types.push_back(Int8PtrTy);
362  }
363
364  bool Packed = false;
365  CharUnits Align = getContext().getDeclAlign(D);
366  if (Align > getContext().toCharUnitsFromBits(Target.getPointerAlign(0))) {
367    // We have to insert padding.
368
369    // The struct above has 2 32-bit integers.
370    unsigned CurrentOffsetInBytes = 4 * 2;
371
372    // And either 2 or 4 pointers.
373    CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
374      CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
375
376    // Align the offset.
377    unsigned AlignedOffsetInBytes =
378      llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
379
380    unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
381    if (NumPaddingBytes > 0) {
382      const llvm::Type *Ty = llvm::Type::getInt8Ty(getLLVMContext());
383      // FIXME: We need a sema error for alignment larger than the minimum of
384      // the maximal stack alignmint and the alignment of malloc on the system.
385      if (NumPaddingBytes > 1)
386        Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
387
388      Types.push_back(Ty);
389
390      // We want a packed struct.
391      Packed = true;
392    }
393  }
394
395  // T x;
396  Types.push_back(ConvertTypeForMem(Ty));
397
398  const llvm::Type *T = llvm::StructType::get(getLLVMContext(), Types, Packed);
399
400  cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
401  CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
402                              ByRefTypeHolder.get());
403
404  Info.first = ByRefTypeHolder.get();
405
406  Info.second = Types.size() - 1;
407
408  return Info.first;
409}
410
411namespace {
412  struct CallArrayDtor : EHScopeStack::Cleanup {
413    CallArrayDtor(const CXXDestructorDecl *Dtor,
414                  const ConstantArrayType *Type,
415                  llvm::Value *Loc)
416      : Dtor(Dtor), Type(Type), Loc(Loc) {}
417
418    const CXXDestructorDecl *Dtor;
419    const ConstantArrayType *Type;
420    llvm::Value *Loc;
421
422    void Emit(CodeGenFunction &CGF, bool IsForEH) {
423      QualType BaseElementTy = CGF.getContext().getBaseElementType(Type);
424      const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy);
425      BasePtr = llvm::PointerType::getUnqual(BasePtr);
426      llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr);
427      CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr);
428    }
429  };
430
431  struct CallVarDtor : EHScopeStack::Cleanup {
432    CallVarDtor(const CXXDestructorDecl *Dtor,
433                llvm::Value *NRVOFlag,
434                llvm::Value *Loc)
435      : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {}
436
437    const CXXDestructorDecl *Dtor;
438    llvm::Value *NRVOFlag;
439    llvm::Value *Loc;
440
441    void Emit(CodeGenFunction &CGF, bool IsForEH) {
442      // Along the exceptions path we always execute the dtor.
443      bool NRVO = !IsForEH && NRVOFlag;
444
445      llvm::BasicBlock *SkipDtorBB = 0;
446      if (NRVO) {
447        // If we exited via NRVO, we skip the destructor call.
448        llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
449        SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
450        llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
451        CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
452        CGF.EmitBlock(RunDtorBB);
453      }
454
455      CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
456                                /*ForVirtualBase=*/false, Loc);
457
458      if (NRVO) CGF.EmitBlock(SkipDtorBB);
459    }
460  };
461}
462
463namespace {
464  struct CallStackRestore : EHScopeStack::Cleanup {
465    llvm::Value *Stack;
466    CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
467    void Emit(CodeGenFunction &CGF, bool IsForEH) {
468      llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp");
469      llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
470      CGF.Builder.CreateCall(F, V);
471    }
472  };
473
474  struct CallCleanupFunction : EHScopeStack::Cleanup {
475    llvm::Constant *CleanupFn;
476    const CGFunctionInfo &FnInfo;
477    const VarDecl &Var;
478
479    CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
480                        const VarDecl *Var)
481      : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
482
483    void Emit(CodeGenFunction &CGF, bool IsForEH) {
484      DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
485                      SourceLocation());
486      // Compute the address of the local variable, in case it's a byref
487      // or something.
488      llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
489
490      // In some cases, the type of the function argument will be different from
491      // the type of the pointer. An example of this is
492      // void f(void* arg);
493      // __attribute__((cleanup(f))) void *g;
494      //
495      // To fix this we insert a bitcast here.
496      QualType ArgTy = FnInfo.arg_begin()->type;
497      llvm::Value *Arg =
498        CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
499
500      CallArgList Args;
501      Args.push_back(std::make_pair(RValue::get(Arg),
502                            CGF.getContext().getPointerType(Var.getType())));
503      CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
504    }
505  };
506
507  struct CallBlockRelease : EHScopeStack::Cleanup {
508    llvm::Value *Addr;
509    CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
510
511    void Emit(CodeGenFunction &CGF, bool IsForEH) {
512      CGF.BuildBlockRelease(Addr, BLOCK_FIELD_IS_BYREF);
513    }
514  };
515}
516
517
518/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
519/// non-zero parts of the specified initializer with equal or fewer than
520/// NumStores scalar stores.
521static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
522                                                unsigned &NumStores) {
523  // Zero and Undef never requires any extra stores.
524  if (isa<llvm::ConstantAggregateZero>(Init) ||
525      isa<llvm::ConstantPointerNull>(Init) ||
526      isa<llvm::UndefValue>(Init))
527    return true;
528  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
529      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
530      isa<llvm::ConstantExpr>(Init))
531    return Init->isNullValue() || NumStores--;
532
533  // See if we can emit each element.
534  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
535    for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
536      llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
537      if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
538        return false;
539    }
540    return true;
541  }
542
543  // Anything else is hard and scary.
544  return false;
545}
546
547/// emitStoresForInitAfterMemset - For inits that
548/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
549/// stores that would be required.
550static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
551                                         bool isVolatile, CGBuilderTy &Builder) {
552  // Zero doesn't require any stores.
553  if (isa<llvm::ConstantAggregateZero>(Init) ||
554      isa<llvm::ConstantPointerNull>(Init) ||
555      isa<llvm::UndefValue>(Init))
556    return;
557
558  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
559      isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
560      isa<llvm::ConstantExpr>(Init)) {
561    if (!Init->isNullValue())
562      Builder.CreateStore(Init, Loc, isVolatile);
563    return;
564  }
565
566  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
567         "Unknown value type!");
568
569  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
570    llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
571    if (Elt->isNullValue()) continue;
572
573    // Otherwise, get a pointer to the element and emit it.
574    emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
575                                 isVolatile, Builder);
576  }
577}
578
579
580/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
581/// plus some stores to initialize a local variable instead of using a memcpy
582/// from a constant global.  It is beneficial to use memset if the global is all
583/// zeros, or mostly zeros and large.
584static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
585                                                  uint64_t GlobalSize) {
586  // If a global is all zeros, always use a memset.
587  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
588
589
590  // If a non-zero global is <= 32 bytes, always use a memcpy.  If it is large,
591  // do it if it will require 6 or fewer scalar stores.
592  // TODO: Should budget depends on the size?  Avoiding a large global warrants
593  // plopping in more stores.
594  unsigned StoreBudget = 6;
595  uint64_t SizeLimit = 32;
596
597  return GlobalSize > SizeLimit &&
598         canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
599}
600
601
602/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
603/// variable declaration with auto, register, or no storage class specifier.
604/// These turn into simple stack objects, or GlobalValues depending on target.
605void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
606  AutoVarEmission emission = EmitAutoVarAlloca(D);
607  EmitAutoVarInit(emission);
608  EmitAutoVarCleanups(emission);
609}
610
611/// EmitAutoVarAlloca - Emit the alloca and debug information for a
612/// local variable.  Does not emit initalization or destruction.
613CodeGenFunction::AutoVarEmission
614CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
615  QualType Ty = D.getType();
616
617  AutoVarEmission emission(D);
618
619  bool isByRef = D.hasAttr<BlocksAttr>();
620  emission.IsByRef = isByRef;
621
622  CharUnits alignment = getContext().getDeclAlign(&D);
623  emission.Alignment = alignment;
624
625  llvm::Value *DeclPtr;
626  if (Ty->isConstantSizeType()) {
627    if (!Target.useGlobalsForAutomaticVariables()) {
628      bool NRVO = getContext().getLangOptions().ElideConstructors &&
629                  D.isNRVOVariable();
630
631      // If this value is a POD array or struct with a statically
632      // determinable constant initializer, there are optimizations we
633      // can do.
634      // TODO: we can potentially constant-evaluate non-POD structs and
635      // arrays as long as the initialization is trivial (e.g. if they
636      // have a non-trivial destructor, but not a non-trivial constructor).
637      if (D.getInit() &&
638          (Ty->isArrayType() || Ty->isRecordType()) && Ty->isPODType() &&
639          D.getInit()->isConstantInitializer(getContext(), false)) {
640
641        // If the variable's a const type, and it's neither an NRVO
642        // candidate nor a __block variable, emit it as a global instead.
643        if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
644            !NRVO && !isByRef) {
645          EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
646
647          emission.Address = 0; // signal this condition to later callbacks
648          assert(emission.wasEmittedAsGlobal());
649          return emission;
650        }
651
652        // Otherwise, tell the initialization code that we're in this case.
653        emission.IsConstantAggregate = true;
654      }
655
656      // A normal fixed sized variable becomes an alloca in the entry block,
657      // unless it's an NRVO variable.
658      const llvm::Type *LTy = ConvertTypeForMem(Ty);
659
660      if (NRVO) {
661        // The named return value optimization: allocate this variable in the
662        // return slot, so that we can elide the copy when returning this
663        // variable (C++0x [class.copy]p34).
664        DeclPtr = ReturnValue;
665
666        if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
667          if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
668            // Create a flag that is used to indicate when the NRVO was applied
669            // to this variable. Set it to zero to indicate that NRVO was not
670            // applied.
671            llvm::Value *Zero = Builder.getFalse();
672            llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
673            EnsureInsertPoint();
674            Builder.CreateStore(Zero, NRVOFlag);
675
676            // Record the NRVO flag for this variable.
677            NRVOFlags[&D] = NRVOFlag;
678            emission.NRVOFlag = NRVOFlag;
679          }
680        }
681      } else {
682        if (isByRef)
683          LTy = BuildByRefType(&D);
684
685        llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
686        Alloc->setName(D.getNameAsString());
687
688        CharUnits allocaAlignment = alignment;
689        if (isByRef)
690          allocaAlignment = std::max(allocaAlignment,
691              getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
692        Alloc->setAlignment(allocaAlignment.getQuantity());
693        DeclPtr = Alloc;
694      }
695    } else {
696      // Targets that don't support recursion emit locals as globals.
697      const char *Class =
698        D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
699      DeclPtr = CreateStaticVarDecl(D, Class,
700                                    llvm::GlobalValue::InternalLinkage);
701    }
702
703    // FIXME: Can this happen?
704    if (Ty->isVariablyModifiedType())
705      EmitVLASize(Ty);
706  } else {
707    EnsureInsertPoint();
708
709    if (!DidCallStackSave) {
710      // Save the stack.
711      llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
712
713      llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
714      llvm::Value *V = Builder.CreateCall(F);
715
716      Builder.CreateStore(V, Stack);
717
718      DidCallStackSave = true;
719
720      // Push a cleanup block and restore the stack there.
721      // FIXME: in general circumstances, this should be an EH cleanup.
722      EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
723    }
724
725    // Get the element type.
726    const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
727    const llvm::Type *LElemPtrTy = LElemTy->getPointerTo(Ty.getAddressSpace());
728
729    llvm::Value *VLASize = EmitVLASize(Ty);
730
731    // Allocate memory for the array.
732    llvm::AllocaInst *VLA =
733      Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla");
734    VLA->setAlignment(alignment.getQuantity());
735
736    DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
737  }
738
739  llvm::Value *&DMEntry = LocalDeclMap[&D];
740  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
741  DMEntry = DeclPtr;
742  emission.Address = DeclPtr;
743
744  // Emit debug info for local var declaration.
745  if (CGDebugInfo *DI = getDebugInfo()) {
746    assert(HaveInsertPoint() && "Unexpected unreachable point!");
747
748    DI->setLocation(D.getLocation());
749    if (Target.useGlobalsForAutomaticVariables()) {
750      DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
751    } else
752      DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
753  }
754
755  return emission;
756}
757
758/// Determines whether the given __block variable is potentially
759/// captured by the given expression.
760static bool isCapturedBy(const VarDecl &var, const Expr *e) {
761  // Skip the most common kinds of expressions that make
762  // hierarchy-walking expensive.
763  e = e->IgnoreParenCasts();
764
765  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
766    const BlockDecl *block = be->getBlockDecl();
767    for (BlockDecl::capture_const_iterator i = block->capture_begin(),
768           e = block->capture_end(); i != e; ++i) {
769      if (i->getVariable() == &var)
770        return true;
771    }
772
773    // No need to walk into the subexpressions.
774    return false;
775  }
776
777  for (Stmt::const_child_range children = e->children(); children; ++children)
778    if (isCapturedBy(var, cast<Expr>(*children)))
779      return true;
780
781  return false;
782}
783
784void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
785  assert(emission.Variable && "emission was not valid!");
786
787  // If this was emitted as a global constant, we're done.
788  if (emission.wasEmittedAsGlobal()) return;
789
790  const VarDecl &D = *emission.Variable;
791  QualType type = D.getType();
792
793  // If this local has an initializer, emit it now.
794  const Expr *Init = D.getInit();
795
796  // If we are at an unreachable point, we don't need to emit the initializer
797  // unless it contains a label.
798  if (!HaveInsertPoint()) {
799    if (!Init || !ContainsLabel(Init)) return;
800    EnsureInsertPoint();
801  }
802
803  CharUnits alignment = emission.Alignment;
804
805  if (emission.IsByRef) {
806    llvm::Value *V;
807
808    BlockFieldFlags fieldFlags;
809    bool fieldNeedsCopyDispose = false;
810
811    if (type->isBlockPointerType()) {
812      fieldFlags |= BLOCK_FIELD_IS_BLOCK;
813      fieldNeedsCopyDispose = true;
814    } else if (getContext().isObjCNSObjectType(type) ||
815               type->isObjCObjectPointerType()) {
816      fieldFlags |= BLOCK_FIELD_IS_OBJECT;
817      fieldNeedsCopyDispose = true;
818    } else if (getLangOptions().CPlusPlus) {
819      if (getContext().getBlockVarCopyInits(&D))
820        fieldNeedsCopyDispose = true;
821      else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl())
822        fieldNeedsCopyDispose = !record->hasTrivialDestructor();
823    }
824
825    llvm::Value *addr = emission.Address;
826
827    // FIXME: Someone double check this.
828    if (type.isObjCGCWeak())
829      fieldFlags |= BLOCK_FIELD_IS_WEAK;
830
831    // Initialize the 'isa', which is just 0 or 1.
832    int isa = 0;
833    if (fieldFlags & BLOCK_FIELD_IS_WEAK)
834      isa = 1;
835    V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
836    Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
837
838    // Store the address of the variable into its own forwarding pointer.
839    Builder.CreateStore(addr,
840                        Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
841
842    // Blocks ABI:
843    //   c) the flags field is set to either 0 if no helper functions are
844    //      needed or BLOCK_HAS_COPY_DISPOSE if they are,
845    BlockFlags flags;
846    if (fieldNeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
847    Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
848                        Builder.CreateStructGEP(addr, 2, "byref.flags"));
849
850    const llvm::Type *V1;
851    V1 = cast<llvm::PointerType>(addr->getType())->getElementType();
852    V = llvm::ConstantInt::get(IntTy, CGM.GetTargetTypeStoreSize(V1).getQuantity());
853    Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
854
855    if (fieldNeedsCopyDispose) {
856      llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
857      Builder.CreateStore(CGM.BuildbyrefCopyHelper(addr->getType(), fieldFlags,
858                                                   alignment.getQuantity(), &D),
859                          copy_helper);
860
861      llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
862      Builder.CreateStore(CGM.BuildbyrefDestroyHelper(addr->getType(),
863                                                      fieldFlags,
864                                                      alignment.getQuantity(),
865                                                      &D),
866                          destroy_helper);
867    }
868  }
869
870  if (!Init) return;
871
872  // Check whether this is a byref variable that's potentially
873  // captured and moved by its own initializer.  If so, we'll need to
874  // emit the initializer first, then copy into the variable.
875  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
876
877  llvm::Value *Loc =
878    capturedByInit ? emission.Address : emission.getObjectAddress(*this);
879
880  if (!emission.IsConstantAggregate)
881    return EmitExprAsInit(Init, &D, Loc, alignment, capturedByInit);
882
883  // If this is a simple aggregate initialization, we can optimize it
884  // in various ways.
885  assert(!capturedByInit && "constant init contains a capturing block?");
886
887  bool isVolatile = type.isVolatileQualified();
888
889  llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this);
890  assert(constant != 0 && "Wasn't a simple constant init?");
891
892  llvm::Value *SizeVal =
893    llvm::ConstantInt::get(IntPtrTy,
894                           getContext().getTypeSizeInChars(type).getQuantity());
895
896  const llvm::Type *BP = Int8PtrTy;
897  if (Loc->getType() != BP)
898    Loc = Builder.CreateBitCast(Loc, BP, "tmp");
899
900  // If the initializer is all or mostly zeros, codegen with memset then do
901  // a few stores afterward.
902  if (shouldUseMemSetPlusStoresToInitialize(constant,
903                CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
904    Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
905                         alignment.getQuantity(), isVolatile);
906    if (!constant->isNullValue()) {
907      Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
908      emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
909    }
910  } else {
911    // Otherwise, create a temporary global with the initializer then
912    // memcpy from the global to the alloca.
913    std::string Name = GetStaticDeclName(*this, D, ".");
914    llvm::GlobalVariable *GV =
915      new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
916                               llvm::GlobalValue::InternalLinkage,
917                               constant, Name, 0, false, 0);
918    GV->setAlignment(alignment.getQuantity());
919
920    llvm::Value *SrcPtr = GV;
921    if (SrcPtr->getType() != BP)
922      SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
923
924    Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
925                         isVolatile);
926  }
927}
928
929/// Emit an expression as an initializer for a variable at the given
930/// location.  The expression is not necessarily the normal
931/// initializer for the variable, and the address is not necessarily
932/// its normal location.
933///
934/// \param init the initializing expression
935/// \param var the variable to act as if we're initializing
936/// \param loc the address to initialize; its type is a pointer
937///   to the LLVM mapping of the variable's type
938/// \param alignment the alignment of the address
939/// \param capturedByInit true if the variable is a __block variable
940///   whose address is potentially changed by the initializer
941void CodeGenFunction::EmitExprAsInit(const Expr *init,
942                                     const VarDecl *var,
943                                     llvm::Value *loc,
944                                     CharUnits alignment,
945                                     bool capturedByInit) {
946  QualType type = var->getType();
947  bool isVolatile = type.isVolatileQualified();
948
949  if (type->isReferenceType()) {
950    RValue RV = EmitReferenceBindingToExpr(init, var);
951    if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
952    EmitStoreOfScalar(RV.getScalarVal(), loc, false,
953                      alignment.getQuantity(), type);
954  } else if (!hasAggregateLLVMType(type)) {
955    llvm::Value *V = EmitScalarExpr(init);
956    if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
957    EmitStoreOfScalar(V, loc, isVolatile, alignment.getQuantity(), type);
958  } else if (type->isAnyComplexType()) {
959    ComplexPairTy complex = EmitComplexExpr(init);
960    if (capturedByInit) loc = BuildBlockByrefAddress(loc, var);
961    StoreComplexToAddr(complex, loc, isVolatile);
962  } else {
963    // TODO: how can we delay here if D is captured by its initializer?
964    EmitAggExpr(init, AggValueSlot::forAddr(loc, isVolatile, true, false));
965  }
966}
967
968void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
969  assert(emission.Variable && "emission was not valid!");
970
971  // If this was emitted as a global constant, we're done.
972  if (emission.wasEmittedAsGlobal()) return;
973
974  const VarDecl &D = *emission.Variable;
975
976  // Handle C++ destruction of variables.
977  if (getLangOptions().CPlusPlus) {
978    QualType type = D.getType();
979    QualType baseType = getContext().getBaseElementType(type);
980    if (const RecordType *RT = baseType->getAs<RecordType>()) {
981      CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
982      if (!ClassDecl->hasTrivialDestructor()) {
983        // Note: We suppress the destructor call when the corresponding NRVO
984        // flag has been set.
985
986        // Note that for __block variables, we want to destroy the
987        // original stack object, not the possible forwarded object.
988        llvm::Value *Loc = emission.getObjectAddress(*this);
989
990        const CXXDestructorDecl *D = ClassDecl->getDestructor();
991        assert(D && "EmitLocalBlockVarDecl - destructor is nul");
992
993        if (type != baseType) {
994          const ConstantArrayType *Array =
995            getContext().getAsConstantArrayType(type);
996          assert(Array && "types changed without array?");
997          EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
998                                             D, Array, Loc);
999        } else {
1000          EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
1001                                           D, emission.NRVOFlag, Loc);
1002        }
1003      }
1004    }
1005  }
1006
1007  // Handle the cleanup attribute.
1008  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1009    const FunctionDecl *FD = CA->getFunctionDecl();
1010
1011    llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1012    assert(F && "Could not find function!");
1013
1014    const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
1015    EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1016  }
1017
1018  // If this is a block variable, call _Block_object_destroy
1019  // (on the unforwarded address).
1020  if (emission.IsByRef &&
1021      CGM.getLangOptions().getGCMode() != LangOptions::GCOnly)
1022    EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
1023}
1024
1025/// Emit an alloca (or GlobalValue depending on target)
1026/// for the specified parameter and set up LocalDeclMap.
1027void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
1028                                   unsigned ArgNo) {
1029  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1030  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1031         "Invalid argument to EmitParmDecl");
1032
1033  Arg->setName(D.getName());
1034
1035  // Use better IR generation for certain implicit parameters.
1036  if (isa<ImplicitParamDecl>(D)) {
1037    // The only implicit argument a block has is its literal.
1038    if (BlockInfo) {
1039      LocalDeclMap[&D] = Arg;
1040
1041      if (CGDebugInfo *DI = getDebugInfo()) {
1042        DI->setLocation(D.getLocation());
1043        DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
1044      }
1045
1046      return;
1047    }
1048  }
1049
1050  QualType Ty = D.getType();
1051
1052  llvm::Value *DeclPtr;
1053  // If this is an aggregate or variable sized value, reuse the input pointer.
1054  if (!Ty->isConstantSizeType() ||
1055      CodeGenFunction::hasAggregateLLVMType(Ty)) {
1056    DeclPtr = Arg;
1057  } else {
1058    // Otherwise, create a temporary to hold the value.
1059    DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
1060
1061    // Store the initial value into the alloca.
1062    EmitStoreOfScalar(Arg, DeclPtr, Ty.isVolatileQualified(),
1063                      getContext().getDeclAlign(&D).getQuantity(), Ty,
1064                      CGM.getTBAAInfo(Ty));
1065  }
1066
1067  llvm::Value *&DMEntry = LocalDeclMap[&D];
1068  assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
1069  DMEntry = DeclPtr;
1070
1071  // Emit debug info for param declaration.
1072  if (CGDebugInfo *DI = getDebugInfo()) {
1073    DI->setLocation(D.getLocation());
1074    DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
1075  }
1076}
1077