2007-06-02 12:16:21 +08:00
|
|
|
//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-02 12:16:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Decl nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-30 18:30:31 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-12-22 22:23:30 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-08-25 09:38:19 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-04-19 12:17:09 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-05-08 13:58:21 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-06-16 07:19:56 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2007-10-17 08:52:43 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-12-12 15:38:43 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-03-04 11:23:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-14 04:44:40 +08:00
|
|
|
#include "llvm/Type.h"
|
2007-06-02 12:16:21 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
|
2007-06-09 09:20:56 +08:00
|
|
|
void CodeGenFunction::EmitDecl(const Decl &D) {
|
|
|
|
switch (D.getKind()) {
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::TranslationUnit:
|
|
|
|
case Decl::Namespace:
|
|
|
|
case Decl::UnresolvedUsingTypename:
|
|
|
|
case Decl::ClassTemplateSpecialization:
|
|
|
|
case Decl::ClassTemplatePartialSpecialization:
|
|
|
|
case Decl::TemplateTypeParm:
|
|
|
|
case Decl::UnresolvedUsingValue:
|
2010-05-30 15:21:58 +08:00
|
|
|
case Decl::NonTypeTemplateParm:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::Field:
|
2010-11-21 14:49:41 +08:00
|
|
|
case Decl::IndirectField:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::ObjCIvar:
|
|
|
|
case Decl::ObjCAtDefsField:
|
2007-10-09 05:37:32 +08:00
|
|
|
case Decl::ParmVar:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::ImplicitParam:
|
|
|
|
case Decl::ClassTemplate:
|
|
|
|
case Decl::FunctionTemplate:
|
2011-05-06 05:57:07 +08:00
|
|
|
case Decl::TypeAliasTemplate:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::TemplateTemplateParm:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
case Decl::ObjCProperty:
|
|
|
|
case Decl::ObjCCompatibleAlias:
|
2010-06-05 13:09:32 +08:00
|
|
|
case Decl::AccessSpec:
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
case Decl::ObjCPropertyImpl:
|
|
|
|
case Decl::ObjCClass:
|
|
|
|
case Decl::ObjCForwardProtocol:
|
|
|
|
case Decl::FileScopeAsm:
|
|
|
|
case Decl::Friend:
|
|
|
|
case Decl::FriendTemplate:
|
|
|
|
case Decl::Block:
|
2011-04-06 04:28:21 +08:00
|
|
|
assert(0 && "Declaration should not be in declstmts!");
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::Function: // void X();
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::Record: // struct/union/class X;
|
2007-06-02 12:16:21 +08:00
|
|
|
case Decl::Enum: // enum X;
|
2009-09-09 23:08:12 +08:00
|
|
|
case Decl::EnumConstant: // enum ? { X = ? }
|
2008-10-15 08:42:39 +08:00
|
|
|
case Decl::CXXRecord: // struct/union/class X; [C++]
|
2009-11-23 08:07:06 +08:00
|
|
|
case Decl::Using: // using X; [C++]
|
|
|
|
case Decl::UsingShadow:
|
|
|
|
case Decl::UsingDirective: // using namespace X; [C++]
|
2010-04-23 10:02:43 +08:00
|
|
|
case Decl::NamespaceAlias:
|
2009-12-04 01:26:31 +08:00
|
|
|
case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
|
2011-02-18 10:08:43 +08:00
|
|
|
case Decl::Label: // __label__ x;
|
2007-06-02 12:16:21 +08:00
|
|
|
// None of these decls require codegen support.
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-30 01:28:43 +08:00
|
|
|
case Decl::Var: {
|
|
|
|
const VarDecl &VD = cast<VarDecl>(D);
|
2010-10-15 12:57:14 +08:00
|
|
|
assert(VD.isLocalVarDecl() &&
|
2008-08-30 01:28:43 +08:00
|
|
|
"Should not see file-scope variables inside a function!");
|
2010-10-15 12:57:14 +08:00
|
|
|
return EmitVarDecl(VD);
|
2008-08-30 01:28:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-15 22:24:37 +08:00
|
|
|
case Decl::Typedef: // typedef int X;
|
|
|
|
case Decl::TypeAlias: { // using X = int; [C++0x]
|
|
|
|
const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
|
2008-12-21 05:51:53 +08:00
|
|
|
QualType Ty = TD.getUnderlyingType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (Ty->isVariablyModifiedType())
|
2011-06-25 05:55:10 +08:00
|
|
|
EmitVariablyModifiedType(Ty);
|
2008-12-21 05:51:53 +08:00
|
|
|
}
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
|
|
|
|
2010-10-15 12:57:14 +08:00
|
|
|
/// EmitVarDecl - This method handles emission of any variable declaration
|
2007-06-02 12:53:11 +08:00
|
|
|
/// inside a function, including static vars etc.
|
2010-10-15 12:57:14 +08:00
|
|
|
void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
|
2007-06-02 12:53:11 +08:00
|
|
|
switch (D.getStorageClass()) {
|
2010-08-26 11:08:43 +08:00
|
|
|
case SC_None:
|
|
|
|
case SC_Auto:
|
|
|
|
case SC_Register:
|
2010-10-15 12:57:14 +08:00
|
|
|
return EmitAutoVarDecl(D);
|
2010-08-26 11:08:43 +08:00
|
|
|
case SC_Static: {
|
2010-02-07 10:03:08 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
llvm::GlobalValue::InternalLinkage;
|
|
|
|
|
2010-05-25 12:30:21 +08:00
|
|
|
// If the function definition has some sort of weak linkage, its
|
|
|
|
// static variables should also be weak so that they get properly
|
|
|
|
// uniqued. We can't do this in C, though, because there's no
|
|
|
|
// standard way to agree on which variables are the same (i.e.
|
|
|
|
// there's no mangling).
|
|
|
|
if (getContext().getLangOptions().CPlusPlus)
|
|
|
|
if (llvm::GlobalValue::isWeakForLinker(CurFn->getLinkage()))
|
|
|
|
Linkage = CurFn->getLinkage();
|
2010-02-07 10:03:08 +08:00
|
|
|
|
2010-10-15 12:57:14 +08:00
|
|
|
return EmitStaticVarDecl(D, Linkage);
|
2010-02-07 10:03:08 +08:00
|
|
|
}
|
2010-08-26 11:08:43 +08:00
|
|
|
case SC_Extern:
|
|
|
|
case SC_PrivateExtern:
|
2008-02-17 06:30:38 +08:00
|
|
|
// Don't emit it now, allow it to be emitted lazily on its first use.
|
|
|
|
return;
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
2009-04-14 10:25:56 +08:00
|
|
|
|
|
|
|
assert(0 && "Unknown storage class");
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
static std::string GetStaticDeclName(CodeGenFunction &CGF, const VarDecl &D,
|
|
|
|
const char *Separator) {
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
2010-03-20 07:29:14 +08:00
|
|
|
if (CGF.getContext().getLangOptions().CPlusPlus) {
|
2010-06-23 00:16:50 +08:00
|
|
|
llvm::StringRef Name = CGM.getMangledName(&D);
|
|
|
|
return Name.str();
|
2010-03-20 07:29:14 +08:00
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
std::string ContextName;
|
2010-12-01 07:07:14 +08:00
|
|
|
if (!CGF.CurFuncDecl) {
|
|
|
|
// Better be in a block declared in global scope.
|
|
|
|
const NamedDecl *ND = cast<NamedDecl>(&D);
|
|
|
|
const DeclContext *DC = ND->getDeclContext();
|
|
|
|
if (const BlockDecl *BD = dyn_cast<BlockDecl>(DC)) {
|
|
|
|
MangleBuffer Name;
|
2011-01-14 02:57:25 +08:00
|
|
|
CGM.getBlockMangledName(GlobalDecl(), Name, BD);
|
2010-12-01 07:07:14 +08:00
|
|
|
ContextName = Name.getString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(0 && "Unknown context for block static var decl");
|
|
|
|
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CGF.CurFuncDecl)) {
|
2010-06-23 00:16:50 +08:00
|
|
|
llvm::StringRef Name = CGM.getMangledName(FD);
|
|
|
|
ContextName = Name.str();
|
2010-03-20 07:29:14 +08:00
|
|
|
} else if (isa<ObjCMethodDecl>(CGF.CurFuncDecl))
|
2009-12-05 16:22:11 +08:00
|
|
|
ContextName = CGF.CurFn->getName();
|
|
|
|
else
|
2010-12-01 07:07:14 +08:00
|
|
|
assert(0 && "Unknown context for static var decl");
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
return ContextName + Separator + D.getNameAsString();
|
|
|
|
}
|
|
|
|
|
2009-02-26 03:24:29 +08:00
|
|
|
llvm::GlobalVariable *
|
2010-10-15 12:57:14 +08:00
|
|
|
CodeGenFunction::CreateStaticVarDecl(const VarDecl &D,
|
|
|
|
const char *Separator,
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2009-02-26 03:24:29 +08:00
|
|
|
QualType Ty = D.getType();
|
|
|
|
assert(Ty->isConstantSizeType() && "VLAs can't be static");
|
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
std::string Name = GetStaticDeclName(*this, D, Separator);
|
2009-02-26 03:24:29 +08:00
|
|
|
|
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(Ty);
|
2009-09-27 02:16:06 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), LTy,
|
|
|
|
Ty.isConstant(getContext()), Linkage,
|
|
|
|
CGM.EmitNullConstant(D.getType()), Name, 0,
|
2011-03-19 06:38:29 +08:00
|
|
|
D.isThreadSpecified(),
|
|
|
|
CGM.getContext().getTargetAddressSpace(Ty));
|
2010-01-28 01:10:57 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
2010-11-03 05:04:24 +08:00
|
|
|
if (Linkage != llvm::GlobalValue::InternalLinkage)
|
|
|
|
GV->setVisibility(CurFn->getVisibility());
|
2009-09-27 02:16:06 +08:00
|
|
|
return GV;
|
2009-02-26 03:24:29 +08:00
|
|
|
}
|
|
|
|
|
2010-10-15 12:57:14 +08:00
|
|
|
/// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
|
2009-12-05 16:22:11 +08:00
|
|
|
/// global variable that has already been created for it. If the initializer
|
|
|
|
/// has a different type than GV does, this may free GV and return a different
|
|
|
|
/// one. Otherwise it just returns GV.
|
|
|
|
llvm::GlobalVariable *
|
2010-10-15 12:57:14 +08:00
|
|
|
CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
|
|
|
|
llvm::GlobalVariable *GV) {
|
2009-12-05 16:22:11 +08:00
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), D.getType(), this);
|
2010-07-16 07:40:35 +08:00
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
// If constant emission failed, then this should be a C++ static
|
|
|
|
// initializer.
|
|
|
|
if (!Init) {
|
|
|
|
if (!getContext().getLangOptions().CPlusPlus)
|
|
|
|
CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
|
2010-09-08 09:44:27 +08:00
|
|
|
else if (Builder.GetInsertBlock()) {
|
2010-01-26 12:02:23 +08:00
|
|
|
// Since we have a static initializer, this global variable can't
|
|
|
|
// be constant.
|
|
|
|
GV->setConstant(false);
|
2010-09-08 09:44:27 +08:00
|
|
|
|
2010-11-06 17:44:32 +08:00
|
|
|
EmitCXXGuardedInit(D, GV);
|
2010-01-26 12:02:23 +08:00
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
return GV;
|
|
|
|
}
|
2010-07-16 07:40:35 +08:00
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
// The initializer may differ in type from the global. Rewrite
|
|
|
|
// the global to match the initializer. (We have to do this
|
|
|
|
// because some types, like unions, can't be completely represented
|
|
|
|
// in the LLVM type system.)
|
2010-09-04 02:58:50 +08:00
|
|
|
if (GV->getType()->getElementType() != Init->getType()) {
|
2009-12-05 16:22:11 +08:00
|
|
|
llvm::GlobalVariable *OldGV = GV;
|
|
|
|
|
|
|
|
GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
|
|
|
|
OldGV->isConstant(),
|
|
|
|
OldGV->getLinkage(), Init, "",
|
2010-11-03 05:04:24 +08:00
|
|
|
/*InsertBefore*/ OldGV,
|
|
|
|
D.isThreadSpecified(),
|
2011-03-19 06:38:29 +08:00
|
|
|
CGM.getContext().getTargetAddressSpace(D.getType()));
|
2010-11-03 05:04:24 +08:00
|
|
|
GV->setVisibility(OldGV->getVisibility());
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
// Steal the name of the old global
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
|
|
|
|
// Replace all uses of the old global with the new global
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
GV->setInitializer(Init);
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-10-15 12:57:14 +08:00
|
|
|
void CodeGenFunction::EmitStaticVarDecl(const VarDecl &D,
|
2010-02-07 10:03:08 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
2009-02-26 03:45:19 +08:00
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-10-15 12:57:14 +08:00
|
|
|
llvm::GlobalVariable *GV = CreateStaticVarDecl(D, ".", Linkage);
|
2009-02-26 03:45:19 +08:00
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// Store into LocalDeclMap before generating initializer to handle
|
|
|
|
// circular references.
|
|
|
|
DMEntry = GV;
|
|
|
|
|
2010-05-05 04:45:42 +08:00
|
|
|
// We can't have a VLA here, but we can have a pointer to a VLA,
|
|
|
|
// even though that doesn't really make any sense.
|
2009-04-20 11:54:15 +08:00
|
|
|
// Make sure to evaluate VLA bounds now so that we have them for later.
|
|
|
|
if (D.getType()->isVariablyModifiedType())
|
2011-06-25 05:55:10 +08:00
|
|
|
EmitVariablyModifiedType(D.getType());
|
2010-09-08 07:26:17 +08:00
|
|
|
|
|
|
|
// Local static block variables must be treated as globals as they may be
|
|
|
|
// referenced in their RHS initializer block-literal expresion.
|
|
|
|
CGM.setStaticLocalDeclAddress(&D, GV);
|
2009-04-20 11:54:15 +08:00
|
|
|
|
2009-12-05 16:22:11 +08:00
|
|
|
// If this value has an initializer, emit it.
|
|
|
|
if (D.getInit())
|
2010-10-15 12:57:14 +08:00
|
|
|
GV = AddInitializerToStaticVarDecl(D, GV);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2010-03-11 07:59:59 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
|
|
|
|
2009-02-13 07:32:54 +08:00
|
|
|
// FIXME: Merge attribute handling.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const AnnotateAttr *AA = D.getAttr<AnnotateAttr>()) {
|
2008-04-19 12:17:09 +08:00
|
|
|
SourceManager &SM = CGM.getContext().getSourceManager();
|
|
|
|
llvm::Constant *Ann =
|
2009-09-09 23:08:12 +08:00
|
|
|
CGM.EmitAnnotateAttr(GV, AA,
|
2009-01-16 15:36:28 +08:00
|
|
|
SM.getInstantiationLineNumber(D.getLocation()));
|
2008-04-19 12:17:09 +08:00
|
|
|
CGM.AddAnnotation(Ann);
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = D.getAttr<SectionAttr>())
|
2009-02-13 07:32:54 +08:00
|
|
|
GV->setSection(SA->getName());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (D.hasAttr<UsedAttr>())
|
2009-02-14 06:08:43 +08:00
|
|
|
CGM.AddUsedGlobal(GV);
|
|
|
|
|
2009-02-26 04:08:33 +08:00
|
|
|
// We may have to cast the constant because of the initializer
|
|
|
|
// mismatch above.
|
|
|
|
//
|
|
|
|
// FIXME: It is really dangerous to store this in the map; if anyone
|
|
|
|
// RAUW's the GV uses of this constant will be invalid.
|
2008-06-08 09:23:18 +08:00
|
|
|
const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
|
2011-03-19 06:38:29 +08:00
|
|
|
const llvm::Type *LPtrTy =
|
|
|
|
LTy->getPointerTo(CGM.getContext().getTargetAddressSpace(D.getType()));
|
2009-07-30 02:54:39 +08:00
|
|
|
DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
|
2008-06-05 16:59:10 +08:00
|
|
|
|
|
|
|
// Emit global variable debug descriptor for static vars.
|
2009-02-13 16:11:52 +08:00
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
2009-02-20 08:19:45 +08:00
|
|
|
if (DI) {
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2008-06-05 16:59:10 +08:00
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(GV), &D);
|
|
|
|
}
|
2007-10-17 08:52:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-14 04:32:21 +08:00
|
|
|
namespace {
|
2011-07-09 09:37:26 +08:00
|
|
|
struct DestroyObject : EHScopeStack::Cleanup {
|
|
|
|
DestroyObject(llvm::Value *addr, QualType type,
|
2011-07-11 16:38:19 +08:00
|
|
|
CodeGenFunction::Destroyer *destroyer,
|
|
|
|
bool useEHCleanupForArray)
|
|
|
|
: addr(addr), type(type), destroyer(*destroyer),
|
|
|
|
useEHCleanupForArray(useEHCleanupForArray) {}
|
2010-07-14 04:32:21 +08:00
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
llvm::Value *addr;
|
|
|
|
QualType type;
|
|
|
|
CodeGenFunction::Destroyer &destroyer;
|
2011-07-11 16:38:19 +08:00
|
|
|
bool useEHCleanupForArray;
|
2010-07-14 04:32:21 +08:00
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, bool isForEH) {
|
|
|
|
// Don't use an EH cleanup recursively from an EH cleanup.
|
|
|
|
bool useEHCleanupForArray = !isForEH && this->useEHCleanupForArray;
|
|
|
|
|
|
|
|
CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
|
2010-07-14 04:32:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
struct DestroyNRVOVariable : EHScopeStack::Cleanup {
|
|
|
|
DestroyNRVOVariable(llvm::Value *addr,
|
|
|
|
const CXXDestructorDecl *Dtor,
|
|
|
|
llvm::Value *NRVOFlag)
|
|
|
|
: Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
|
2010-07-14 04:32:21 +08:00
|
|
|
|
|
|
|
const CXXDestructorDecl *Dtor;
|
|
|
|
llvm::Value *NRVOFlag;
|
|
|
|
llvm::Value *Loc;
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
// Along the exceptions path we always execute the dtor.
|
|
|
|
bool NRVO = !IsForEH && NRVOFlag;
|
|
|
|
|
|
|
|
llvm::BasicBlock *SkipDtorBB = 0;
|
|
|
|
if (NRVO) {
|
|
|
|
// If we exited via NRVO, we skip the destructor call.
|
|
|
|
llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
|
|
|
|
SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
|
|
|
|
llvm::Value *DidNRVO = CGF.Builder.CreateLoad(NRVOFlag, "nrvo.val");
|
|
|
|
CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
|
|
|
|
CGF.EmitBlock(RunDtorBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
|
|
|
|
/*ForVirtualBase=*/false, Loc);
|
|
|
|
|
|
|
|
if (NRVO) CGF.EmitBlock(SkipDtorBB);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallStackRestore : EHScopeStack::Cleanup {
|
2010-07-21 14:13:08 +08:00
|
|
|
llvm::Value *Stack;
|
|
|
|
CallStackRestore(llvm::Value *Stack) : Stack(Stack) {}
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
llvm::Value *V = CGF.Builder.CreateLoad(Stack, "tmp");
|
|
|
|
llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
|
|
|
|
CGF.Builder.CreateCall(F, V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-25 07:21:27 +08:00
|
|
|
struct ExtendGCLifetime : EHScopeStack::Cleanup {
|
|
|
|
const VarDecl &Var;
|
|
|
|
ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool forEH) {
|
|
|
|
// Compute the address of the local variable, in case it's a
|
|
|
|
// byref or something.
|
|
|
|
DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
|
|
|
|
SourceLocation());
|
|
|
|
llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE));
|
|
|
|
CGF.EmitExtendGCLifetime(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallCleanupFunction : EHScopeStack::Cleanup {
|
2010-07-21 14:13:08 +08:00
|
|
|
llvm::Constant *CleanupFn;
|
|
|
|
const CGFunctionInfo &FnInfo;
|
|
|
|
const VarDecl &Var;
|
|
|
|
|
|
|
|
CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
|
2011-02-22 14:44:22 +08:00
|
|
|
const VarDecl *Var)
|
|
|
|
: CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
|
2010-07-21 14:13:08 +08:00
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
2011-02-22 14:44:22 +08:00
|
|
|
DeclRefExpr DRE(const_cast<VarDecl*>(&Var), Var.getType(), VK_LValue,
|
|
|
|
SourceLocation());
|
|
|
|
// Compute the address of the local variable, in case it's a byref
|
|
|
|
// or something.
|
|
|
|
llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getAddress();
|
|
|
|
|
2010-07-21 14:13:08 +08:00
|
|
|
// In some cases, the type of the function argument will be different from
|
|
|
|
// the type of the pointer. An example of this is
|
|
|
|
// void f(void* arg);
|
|
|
|
// __attribute__((cleanup(f))) void *g;
|
|
|
|
//
|
|
|
|
// To fix this we insert a bitcast here.
|
|
|
|
QualType ArgTy = FnInfo.arg_begin()->type;
|
|
|
|
llvm::Value *Arg =
|
|
|
|
CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
|
|
|
|
|
|
|
|
CallArgList Args;
|
2011-05-03 01:57:46 +08:00
|
|
|
Args.add(RValue::get(Arg),
|
|
|
|
CGF.getContext().getPointerType(Var.getType()));
|
2010-07-21 14:13:08 +08:00
|
|
|
CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// EmitAutoVarWithLifetime - Does the setup required for an automatic
|
|
|
|
/// variable with lifetime.
|
|
|
|
static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
|
|
|
|
llvm::Value *addr,
|
|
|
|
Qualifiers::ObjCLifetime lifetime) {
|
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
llvm_unreachable("present but none");
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong: {
|
|
|
|
CGF.PushARCReleaseCleanup(CGF.getARCCleanupKind(),
|
|
|
|
var.getType(), addr,
|
|
|
|
var.hasAttr<ObjCPreciseLifetimeAttr>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
// __weak objects always get EH cleanups; otherwise, exceptions
|
|
|
|
// could cause really nasty crashes instead of mere leaks.
|
|
|
|
CGF.PushARCWeakReleaseCleanup(NormalAndEHCleanup, var.getType(), addr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
|
|
|
|
if (const Expr *e = dyn_cast<Expr>(s)) {
|
|
|
|
// Skip the most common kinds of expressions that make
|
|
|
|
// hierarchy-walking expensive.
|
|
|
|
s = e = e->IgnoreParenCasts();
|
|
|
|
|
|
|
|
if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
|
|
|
|
return (ref->getDecl() == &var);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Stmt::const_child_range children = s->children(); children; ++children)
|
2011-06-30 04:00:16 +08:00
|
|
|
// children might be null; as in missing decl or conditional of an if-stmt.
|
|
|
|
if ((*children) && isAccessedBy(var, *children))
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
|
|
|
|
if (!decl) return false;
|
|
|
|
if (!isa<VarDecl>(decl)) return false;
|
|
|
|
const VarDecl *var = cast<VarDecl>(decl);
|
|
|
|
return isAccessedBy(*var, e);
|
|
|
|
}
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
static void drillIntoBlockVariable(CodeGenFunction &CGF,
|
|
|
|
LValue &lvalue,
|
|
|
|
const VarDecl *var) {
|
|
|
|
lvalue.setAddress(CGF.BuildBlockByrefAddress(lvalue.getAddress(), var));
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
void CodeGenFunction::EmitScalarInit(const Expr *init,
|
|
|
|
const ValueDecl *D,
|
2011-06-16 12:16:24 +08:00
|
|
|
LValue lvalue,
|
|
|
|
bool capturedByInit) {
|
|
|
|
Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
|
2011-06-16 07:02:42 +08:00
|
|
|
if (!lifetime) {
|
|
|
|
llvm::Value *value = EmitScalarExpr(init);
|
2011-06-16 12:16:24 +08:00
|
|
|
if (capturedByInit)
|
|
|
|
drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
|
2011-06-25 10:11:03 +08:00
|
|
|
EmitStoreThroughLValue(RValue::get(value), lvalue);
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're emitting a value with lifetime, we have to do the
|
|
|
|
// initialization *before* we leave the cleanup scopes.
|
|
|
|
CodeGenFunction::RunCleanupsScope Scope(*this);
|
|
|
|
if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init))
|
|
|
|
init = ewc->getSubExpr();
|
|
|
|
|
|
|
|
// We have to maintain the illusion that the variable is
|
|
|
|
// zero-initialized. If the variable might be accessed in its
|
|
|
|
// initializer, zero-initialize before running the initializer, then
|
|
|
|
// actually perform the initialization with an assign.
|
|
|
|
bool accessedByInit = false;
|
|
|
|
if (lifetime != Qualifiers::OCL_ExplicitNone)
|
|
|
|
accessedByInit = isAccessedBy(D, init);
|
|
|
|
if (accessedByInit) {
|
2011-06-16 12:16:24 +08:00
|
|
|
LValue tempLV = lvalue;
|
2011-06-16 07:02:42 +08:00
|
|
|
// Drill down to the __block object if necessary.
|
|
|
|
if (capturedByInit) {
|
|
|
|
// We can use a simple GEP for this because it can't have been
|
|
|
|
// moved yet.
|
2011-06-16 12:16:24 +08:00
|
|
|
tempLV.setAddress(Builder.CreateStructGEP(tempLV.getAddress(),
|
|
|
|
getByRefValueLLVMField(cast<VarDecl>(D))));
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
const llvm::PointerType *ty
|
|
|
|
= cast<llvm::PointerType>(tempLV.getAddress()->getType());
|
2011-06-16 07:02:42 +08:00
|
|
|
ty = cast<llvm::PointerType>(ty->getElementType());
|
|
|
|
|
|
|
|
llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
|
|
|
|
|
|
|
|
// If __weak, we want to use a barrier under certain conditions.
|
|
|
|
if (lifetime == Qualifiers::OCL_Weak)
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitARCInitWeak(tempLV.getAddress(), zero);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Otherwise just do a simple store.
|
|
|
|
else
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitStoreOfScalar(zero, tempLV);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the initializer.
|
|
|
|
llvm::Value *value = 0;
|
|
|
|
|
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
llvm_unreachable("present but none");
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// nothing to do
|
|
|
|
value = EmitScalarExpr(init);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong: {
|
|
|
|
value = EmitARCRetainScalarExpr(init);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak: {
|
|
|
|
// No way to optimize a producing initializer into this. It's not
|
|
|
|
// worth optimizing for, because the value will immediately
|
|
|
|
// disappear in the common case.
|
|
|
|
value = EmitScalarExpr(init);
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
|
2011-06-16 07:02:42 +08:00
|
|
|
if (accessedByInit)
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
|
2011-06-16 07:02:42 +08:00
|
|
|
else
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitARCInitWeak(lvalue.getAddress(), value);
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
value = EmitARCRetainAutoreleaseScalarExpr(init);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// If the variable might have been accessed by its initializer, we
|
|
|
|
// might have to initialize with a barrier. We have to do this for
|
|
|
|
// both __weak and __strong, but __weak got filtered out above.
|
|
|
|
if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
|
2011-06-16 12:16:24 +08:00
|
|
|
llvm::Value *oldValue = EmitLoadOfScalar(lvalue);
|
|
|
|
EmitStoreOfScalar(value, lvalue);
|
2011-06-16 07:02:42 +08:00
|
|
|
EmitARCRelease(oldValue, /*precise*/ false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitStoreOfScalar(value, lvalue);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2010-12-01 10:05:19 +08:00
|
|
|
|
2011-06-17 14:42:21 +08:00
|
|
|
/// EmitScalarInit - Initialize the given lvalue with the given object.
|
|
|
|
void CodeGenFunction::EmitScalarInit(llvm::Value *init, LValue lvalue) {
|
|
|
|
Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
|
|
|
|
if (!lifetime)
|
2011-06-25 10:11:03 +08:00
|
|
|
return EmitStoreThroughLValue(RValue::get(init), lvalue);
|
2011-06-17 14:42:21 +08:00
|
|
|
|
|
|
|
switch (lifetime) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
llvm_unreachable("present but none");
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
init = EmitARCRetain(lvalue.getType(), init);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
// Initialize and then skip the primitive store.
|
|
|
|
EmitARCInitWeak(lvalue.getAddress(), init);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
init = EmitARCRetainAutorelease(lvalue.getType(), init);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitStoreOfScalar(init, lvalue);
|
|
|
|
}
|
|
|
|
|
2010-12-01 10:05:19 +08:00
|
|
|
/// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
|
|
|
|
/// non-zero parts of the specified initializer with equal or fewer than
|
|
|
|
/// NumStores scalar stores.
|
|
|
|
static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
|
|
|
|
unsigned &NumStores) {
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
// Zero and Undef never requires any extra stores.
|
|
|
|
if (isa<llvm::ConstantAggregateZero>(Init) ||
|
|
|
|
isa<llvm::ConstantPointerNull>(Init) ||
|
|
|
|
isa<llvm::UndefValue>(Init))
|
|
|
|
return true;
|
|
|
|
if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
|
|
|
|
isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
|
|
|
|
isa<llvm::ConstantExpr>(Init))
|
|
|
|
return Init->isNullValue() || NumStores--;
|
|
|
|
|
|
|
|
// See if we can emit each element.
|
|
|
|
if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
|
|
|
|
for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
|
|
|
|
llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
|
|
|
|
if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-01 10:05:19 +08:00
|
|
|
|
|
|
|
// Anything else is hard and scary.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emitStoresForInitAfterMemset - For inits that
|
|
|
|
/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
|
|
|
|
/// stores that would be required.
|
|
|
|
static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
|
2011-02-22 14:44:22 +08:00
|
|
|
bool isVolatile, CGBuilderTy &Builder) {
|
2010-12-01 10:05:19 +08:00
|
|
|
// Zero doesn't require any stores.
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
if (isa<llvm::ConstantAggregateZero>(Init) ||
|
|
|
|
isa<llvm::ConstantPointerNull>(Init) ||
|
|
|
|
isa<llvm::UndefValue>(Init))
|
|
|
|
return;
|
2010-12-01 10:05:19 +08:00
|
|
|
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
|
|
|
|
isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
|
|
|
|
isa<llvm::ConstantExpr>(Init)) {
|
|
|
|
if (!Init->isNullValue())
|
2011-02-22 14:44:22 +08:00
|
|
|
Builder.CreateStore(Init, Loc, isVolatile);
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
|
|
|
|
"Unknown value type!");
|
2010-12-01 10:05:19 +08:00
|
|
|
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
|
|
|
|
llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
|
|
|
|
if (Elt->isNullValue()) continue;
|
|
|
|
|
|
|
|
// Otherwise, get a pointer to the element and emit it.
|
|
|
|
emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
|
2011-02-22 14:44:22 +08:00
|
|
|
isVolatile, Builder);
|
Enhance the init generation logic to emit a memset followed by a few stores when
a global is larger than 32 bytes and has fewer than 6 non-zero values in the
initializer. Previously we'd turn something like this:
char test8(int X) {
char str[10000] = "abc";
into a 10K global variable which we then memcpy'd from. Now we generate:
%str = alloca [10000 x i8], align 16
%tmp = getelementptr inbounds [10000 x i8]* %str, i64 0, i64 0
call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 10000, i32 16, i1 false)
store i8 97, i8* %tmp, align 16
%0 = getelementptr [10000 x i8]* %str, i64 0, i64 1
store i8 98, i8* %0, align 1
%1 = getelementptr [10000 x i8]* %str, i64 0, i64 2
store i8 99, i8* %1, align 2
Which is much smaller in space and also likely faster.
This is part of PR279
llvm-svn: 120645
2010-12-02 09:58:41 +08:00
|
|
|
}
|
2010-12-01 10:05:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
|
|
|
|
/// plus some stores to initialize a local variable instead of using a memcpy
|
|
|
|
/// from a constant global. It is beneficial to use memset if the global is all
|
|
|
|
/// zeros, or mostly zeros and large.
|
|
|
|
static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
|
|
|
|
uint64_t GlobalSize) {
|
|
|
|
// If a global is all zeros, always use a memset.
|
|
|
|
if (isa<llvm::ConstantAggregateZero>(Init)) return true;
|
|
|
|
|
|
|
|
|
|
|
|
// If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
|
|
|
|
// do it if it will require 6 or fewer scalar stores.
|
|
|
|
// TODO: Should budget depends on the size? Avoiding a large global warrants
|
|
|
|
// plopping in more stores.
|
|
|
|
unsigned StoreBudget = 6;
|
|
|
|
uint64_t SizeLimit = 32;
|
|
|
|
|
|
|
|
return GlobalSize > SizeLimit &&
|
|
|
|
canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-31 04:21:55 +08:00
|
|
|
/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
2007-06-02 12:53:11 +08:00
|
|
|
/// variable declaration with auto, register, or no storage class specifier.
|
2008-05-08 13:58:21 +08:00
|
|
|
/// These turn into simple stack objects, or GlobalValues depending on target.
|
2011-02-22 14:44:22 +08:00
|
|
|
void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
|
|
|
|
AutoVarEmission emission = EmitAutoVarAlloca(D);
|
|
|
|
EmitAutoVarInit(emission);
|
|
|
|
EmitAutoVarCleanups(emission);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitAutoVarAlloca - Emit the alloca and debug information for a
|
|
|
|
/// local variable. Does not emit initalization or destruction.
|
|
|
|
CodeGenFunction::AutoVarEmission
|
|
|
|
CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2011-02-22 14:44:22 +08:00
|
|
|
|
|
|
|
AutoVarEmission emission(D);
|
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
bool isByRef = D.hasAttr<BlocksAttr>();
|
2011-02-22 14:44:22 +08:00
|
|
|
emission.IsByRef = isByRef;
|
|
|
|
|
|
|
|
CharUnits alignment = getContext().getDeclAlign(&D);
|
|
|
|
emission.Alignment = alignment;
|
2007-06-02 12:53:11 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
// If the type is variably-modified, emit all the VLA sizes for it.
|
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVariablyModifiedType(Ty);
|
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
llvm::Value *DeclPtr;
|
2008-02-15 20:20:59 +08:00
|
|
|
if (Ty->isConstantSizeType()) {
|
2008-05-08 13:58:21 +08:00
|
|
|
if (!Target.useGlobalsForAutomaticVariables()) {
|
2011-02-22 14:44:22 +08:00
|
|
|
bool NRVO = getContext().getLangOptions().ElideConstructors &&
|
|
|
|
D.isNRVOVariable();
|
|
|
|
|
|
|
|
// If this value is a POD array or struct with a statically
|
|
|
|
// determinable constant initializer, there are optimizations we
|
|
|
|
// can do.
|
|
|
|
// TODO: we can potentially constant-evaluate non-POD structs and
|
|
|
|
// arrays as long as the initialization is trivial (e.g. if they
|
|
|
|
// have a non-trivial destructor, but not a non-trivial constructor).
|
|
|
|
if (D.getInit() &&
|
2011-06-16 07:02:42 +08:00
|
|
|
(Ty->isArrayType() || Ty->isRecordType()) &&
|
|
|
|
(Ty.isPODType(getContext()) ||
|
|
|
|
getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
|
2010-08-03 05:13:48 +08:00
|
|
|
D.getInit()->isConstantInitializer(getContext(), false)) {
|
2011-02-22 14:44:22 +08:00
|
|
|
|
|
|
|
// If the variable's a const type, and it's neither an NRVO
|
|
|
|
// candidate nor a __block variable, emit it as a global instead.
|
|
|
|
if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
|
|
|
|
!NRVO && !isByRef) {
|
2011-03-07 07:28:21 +08:00
|
|
|
EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
|
2011-02-22 14:44:22 +08:00
|
|
|
|
|
|
|
emission.Address = 0; // signal this condition to later callbacks
|
|
|
|
assert(emission.wasEmittedAsGlobal());
|
|
|
|
return emission;
|
2009-11-04 09:18:09 +08:00
|
|
|
}
|
2011-02-22 14:44:22 +08:00
|
|
|
|
|
|
|
// Otherwise, tell the initialization code that we're in this case.
|
|
|
|
emission.IsConstantAggregate = true;
|
2009-11-04 09:18:09 +08:00
|
|
|
}
|
|
|
|
|
2010-05-15 14:46:45 +08:00
|
|
|
// A normal fixed sized variable becomes an alloca in the entry block,
|
|
|
|
// unless it's an NRVO variable.
|
2009-03-04 12:25:14 +08:00
|
|
|
const llvm::Type *LTy = ConvertTypeForMem(Ty);
|
2010-05-15 14:46:45 +08:00
|
|
|
|
|
|
|
if (NRVO) {
|
|
|
|
// The named return value optimization: allocate this variable in the
|
|
|
|
// return slot, so that we can elide the copy when returning this
|
|
|
|
// variable (C++0x [class.copy]p34).
|
|
|
|
DeclPtr = ReturnValue;
|
2010-05-17 23:52:46 +08:00
|
|
|
|
|
|
|
if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
|
|
|
|
if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
|
|
|
|
// Create a flag that is used to indicate when the NRVO was applied
|
|
|
|
// to this variable. Set it to zero to indicate that NRVO was not
|
|
|
|
// applied.
|
2010-12-01 09:47:15 +08:00
|
|
|
llvm::Value *Zero = Builder.getFalse();
|
2011-02-22 14:44:22 +08:00
|
|
|
llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
|
2011-02-17 07:59:08 +08:00
|
|
|
EnsureInsertPoint();
|
2010-05-17 23:52:46 +08:00
|
|
|
Builder.CreateStore(Zero, NRVOFlag);
|
|
|
|
|
|
|
|
// Record the NRVO flag for this variable.
|
|
|
|
NRVOFlags[&D] = NRVOFlag;
|
2011-02-22 14:44:22 +08:00
|
|
|
emission.NRVOFlag = NRVOFlag;
|
2010-05-17 23:52:46 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-15 14:46:45 +08:00
|
|
|
} else {
|
|
|
|
if (isByRef)
|
|
|
|
LTy = BuildByRefType(&D);
|
|
|
|
|
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
|
|
|
Alloc->setName(D.getNameAsString());
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
CharUnits allocaAlignment = alignment;
|
2010-05-15 14:46:45 +08:00
|
|
|
if (isByRef)
|
2011-02-22 14:44:22 +08:00
|
|
|
allocaAlignment = std::max(allocaAlignment,
|
2011-01-18 10:01:14 +08:00
|
|
|
getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
|
2011-02-22 14:44:22 +08:00
|
|
|
Alloc->setAlignment(allocaAlignment.getQuantity());
|
2010-05-15 14:46:45 +08:00
|
|
|
DeclPtr = Alloc;
|
|
|
|
}
|
2008-05-08 13:58:21 +08:00
|
|
|
} else {
|
|
|
|
// Targets that don't support recursion emit locals as globals.
|
|
|
|
const char *Class =
|
2010-08-26 11:08:43 +08:00
|
|
|
D.getStorageClass() == SC_Register ? ".reg." : ".auto.";
|
2010-10-15 12:57:14 +08:00
|
|
|
DeclPtr = CreateStaticVarDecl(D, Class,
|
|
|
|
llvm::GlobalValue::InternalLinkage);
|
2008-05-08 13:58:21 +08:00
|
|
|
}
|
2007-06-02 12:53:11 +08:00
|
|
|
} else {
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
|
|
|
|
2009-02-10 04:41:50 +08:00
|
|
|
if (!DidCallStackSave) {
|
2008-12-12 15:38:43 +08:00
|
|
|
// Save the stack.
|
2011-02-08 16:22:06 +08:00
|
|
|
llvm::Value *Stack = CreateTempAlloca(Int8PtrTy, "saved_stack");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
|
|
|
|
llvm::Value *V = Builder.CreateCall(F);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
Builder.CreateStore(V, Stack);
|
2009-02-10 04:41:50 +08:00
|
|
|
|
|
|
|
DidCallStackSave = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-21 14:13:08 +08:00
|
|
|
// Push a cleanup block and restore the stack there.
|
2011-01-28 16:37:24 +08:00
|
|
|
// FIXME: in general circumstances, this should be an EH cleanup.
|
2010-07-21 15:22:38 +08:00
|
|
|
EHStack.pushCleanup<CallStackRestore>(NormalCleanup, Stack);
|
2008-12-12 15:38:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
llvm::Value *elementCount;
|
|
|
|
QualType elementType;
|
|
|
|
llvm::tie(elementCount, elementType) = getVLASize(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
const llvm::Type *llvmTy = ConvertTypeForMem(elementType);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
|
|
|
// Allocate memory for the array.
|
2011-06-25 05:55:10 +08:00
|
|
|
llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
|
|
|
|
vla->setAlignment(alignment.getQuantity());
|
2009-09-27 02:16:06 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
DeclPtr = vla;
|
2007-06-02 12:53:11 +08:00
|
|
|
}
|
2008-12-21 07:11:59 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2011-02-22 14:44:22 +08:00
|
|
|
emission.Address = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for local var declaration.
|
2011-06-04 03:21:47 +08:00
|
|
|
if (HaveInsertPoint())
|
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
|
|
|
DI->setLocation(D.getLocation());
|
|
|
|
if (Target.useGlobalsForAutomaticVariables()) {
|
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
|
|
|
|
} else
|
|
|
|
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
|
|
|
}
|
2008-05-30 18:30:31 +08:00
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
return emission;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines whether the given __block variable is potentially
|
|
|
|
/// captured by the given expression.
|
|
|
|
static bool isCapturedBy(const VarDecl &var, const Expr *e) {
|
|
|
|
// Skip the most common kinds of expressions that make
|
|
|
|
// hierarchy-walking expensive.
|
|
|
|
e = e->IgnoreParenCasts();
|
|
|
|
|
|
|
|
if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
|
|
|
|
const BlockDecl *block = be->getBlockDecl();
|
|
|
|
for (BlockDecl::capture_const_iterator i = block->capture_begin(),
|
|
|
|
e = block->capture_end(); i != e; ++i) {
|
|
|
|
if (i->getVariable() == &var)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to walk into the subexpressions.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Stmt::const_child_range children = e->children(); children; ++children)
|
|
|
|
if (isCapturedBy(var, cast<Expr>(*children)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-02 05:08:19 +08:00
|
|
|
/// \brief Determine whether the given initializer is trivial in the sense
|
|
|
|
/// that it requires no code to be generated.
|
|
|
|
static bool isTrivialInitializer(const Expr *Init) {
|
|
|
|
if (!Init)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
|
|
|
|
if (CXXConstructorDecl *Constructor = Construct->getConstructor())
|
|
|
|
if (Constructor->isTrivial() &&
|
|
|
|
Constructor->isDefaultConstructor() &&
|
|
|
|
!Construct->requiresZeroInitialization())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-02-22 14:44:22 +08:00
|
|
|
void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
|
2011-02-22 15:16:58 +08:00
|
|
|
assert(emission.Variable && "emission was not valid!");
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// If this was emitted as a global constant, we're done.
|
|
|
|
if (emission.wasEmittedAsGlobal()) return;
|
|
|
|
|
2011-02-22 15:16:58 +08:00
|
|
|
const VarDecl &D = *emission.Variable;
|
2011-02-22 14:44:22 +08:00
|
|
|
QualType type = D.getType();
|
|
|
|
|
2007-07-12 08:39:48 +08:00
|
|
|
// If this local has an initializer, emit it now.
|
2009-07-19 14:58:07 +08:00
|
|
|
const Expr *Init = D.getInit();
|
|
|
|
|
|
|
|
// If we are at an unreachable point, we don't need to emit the initializer
|
|
|
|
// unless it contains a label.
|
|
|
|
if (!HaveInsertPoint()) {
|
2011-02-22 14:44:22 +08:00
|
|
|
if (!Init || !ContainsLabel(Init)) return;
|
|
|
|
EnsureInsertPoint();
|
2009-07-19 14:58:07 +08:00
|
|
|
}
|
|
|
|
|
2011-03-31 09:59:53 +08:00
|
|
|
// Initialize the structure of a __block variable.
|
|
|
|
if (emission.IsByRef)
|
|
|
|
emitByrefStructureInit(emission);
|
2009-02-08 07:51:38 +08:00
|
|
|
|
2011-07-02 05:08:19 +08:00
|
|
|
if (isTrivialInitializer(Init))
|
|
|
|
return;
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
|
2011-03-31 09:59:53 +08:00
|
|
|
CharUnits alignment = emission.Alignment;
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// Check whether this is a byref variable that's potentially
|
|
|
|
// captured and moved by its own initializer. If so, we'll need to
|
|
|
|
// emit the initializer first, then copy into the variable.
|
|
|
|
bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
|
|
|
|
|
|
|
|
llvm::Value *Loc =
|
|
|
|
capturedByInit ? emission.Address : emission.getObjectAddress(*this);
|
|
|
|
|
2011-06-16 12:16:24 +08:00
|
|
|
if (!emission.IsConstantAggregate) {
|
|
|
|
LValue lv = MakeAddrLValue(Loc, type, alignment.getQuantity());
|
|
|
|
lv.setNonGC(true);
|
|
|
|
return EmitExprAsInit(Init, &D, lv, capturedByInit);
|
|
|
|
}
|
2011-03-08 17:11:50 +08:00
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// If this is a simple aggregate initialization, we can optimize it
|
|
|
|
// in various ways.
|
2011-03-08 17:11:50 +08:00
|
|
|
assert(!capturedByInit && "constant init contains a capturing block?");
|
|
|
|
|
|
|
|
bool isVolatile = type.isVolatileQualified();
|
2010-04-04 11:10:52 +08:00
|
|
|
|
2011-03-08 17:11:50 +08:00
|
|
|
llvm::Constant *constant = CGM.EmitConstantExpr(D.getInit(), type, this);
|
|
|
|
assert(constant != 0 && "Wasn't a simple constant init?");
|
|
|
|
|
|
|
|
llvm::Value *SizeVal =
|
|
|
|
llvm::ConstantInt::get(IntPtrTy,
|
|
|
|
getContext().getTypeSizeInChars(type).getQuantity());
|
|
|
|
|
|
|
|
const llvm::Type *BP = Int8PtrTy;
|
|
|
|
if (Loc->getType() != BP)
|
|
|
|
Loc = Builder.CreateBitCast(Loc, BP, "tmp");
|
|
|
|
|
|
|
|
// If the initializer is all or mostly zeros, codegen with memset then do
|
|
|
|
// a few stores afterward.
|
|
|
|
if (shouldUseMemSetPlusStoresToInitialize(constant,
|
|
|
|
CGM.getTargetData().getTypeAllocSize(constant->getType()))) {
|
|
|
|
Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
|
|
|
|
alignment.getQuantity(), isVolatile);
|
|
|
|
if (!constant->isNullValue()) {
|
|
|
|
Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
|
|
|
|
emitStoresForInitAfterMemset(constant, Loc, isVolatile, Builder);
|
2010-03-13 05:40:43 +08:00
|
|
|
}
|
2011-03-08 17:11:50 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, create a temporary global with the initializer then
|
|
|
|
// memcpy from the global to the alloca.
|
|
|
|
std::string Name = GetStaticDeclName(*this, D, ".");
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
constant, Name, 0, false, 0);
|
|
|
|
GV->setAlignment(alignment.getQuantity());
|
2011-05-28 06:32:55 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
2011-03-08 17:11:50 +08:00
|
|
|
|
|
|
|
llvm::Value *SrcPtr = GV;
|
|
|
|
if (SrcPtr->getType() != BP)
|
|
|
|
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
|
|
|
|
|
|
|
Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
|
|
|
|
isVolatile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an expression as an initializer for a variable at the given
|
|
|
|
/// location. The expression is not necessarily the normal
|
|
|
|
/// initializer for the variable, and the address is not necessarily
|
|
|
|
/// its normal location.
|
|
|
|
///
|
|
|
|
/// \param init the initializing expression
|
|
|
|
/// \param var the variable to act as if we're initializing
|
|
|
|
/// \param loc the address to initialize; its type is a pointer
|
|
|
|
/// to the LLVM mapping of the variable's type
|
|
|
|
/// \param alignment the alignment of the address
|
|
|
|
/// \param capturedByInit true if the variable is a __block variable
|
|
|
|
/// whose address is potentially changed by the initializer
|
|
|
|
void CodeGenFunction::EmitExprAsInit(const Expr *init,
|
2011-06-16 07:02:42 +08:00
|
|
|
const ValueDecl *D,
|
2011-06-16 12:16:24 +08:00
|
|
|
LValue lvalue,
|
2011-03-08 17:11:50 +08:00
|
|
|
bool capturedByInit) {
|
2011-06-16 07:02:42 +08:00
|
|
|
QualType type = D->getType();
|
2011-03-08 17:11:50 +08:00
|
|
|
|
|
|
|
if (type->isReferenceType()) {
|
2011-06-16 12:16:24 +08:00
|
|
|
RValue rvalue = EmitReferenceBindingToExpr(init, D);
|
2011-06-16 07:02:42 +08:00
|
|
|
if (capturedByInit)
|
2011-06-16 12:16:24 +08:00
|
|
|
drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
|
2011-06-25 10:11:03 +08:00
|
|
|
EmitStoreThroughLValue(rvalue, lvalue);
|
2011-02-22 14:44:22 +08:00
|
|
|
} else if (!hasAggregateLLVMType(type)) {
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitScalarInit(init, D, lvalue, capturedByInit);
|
2011-02-22 14:44:22 +08:00
|
|
|
} else if (type->isAnyComplexType()) {
|
2011-03-08 17:11:50 +08:00
|
|
|
ComplexPairTy complex = EmitComplexExpr(init);
|
2011-06-16 12:16:24 +08:00
|
|
|
if (capturedByInit)
|
|
|
|
drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
|
|
|
|
StoreComplexToAddr(complex, lvalue.getAddress(), lvalue.isVolatile());
|
2011-02-22 14:44:22 +08:00
|
|
|
} else {
|
|
|
|
// TODO: how can we delay here if D is captured by its initializer?
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitAggExpr(init, AggValueSlot::forLValue(lvalue, true, false));
|
2010-03-13 05:40:43 +08:00
|
|
|
}
|
2011-02-22 14:44:22 +08:00
|
|
|
}
|
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
/// Enter a destroy cleanup for the given local variable.
|
|
|
|
void CodeGenFunction::emitAutoVarTypeCleanup(
|
|
|
|
const CodeGenFunction::AutoVarEmission &emission,
|
|
|
|
QualType::DestructionKind dtorKind) {
|
|
|
|
assert(dtorKind != QualType::DK_none);
|
|
|
|
|
|
|
|
// Note that for __block variables, we want to destroy the
|
|
|
|
// original stack object, not the possibly forwarded object.
|
|
|
|
llvm::Value *addr = emission.getObjectAddress(*this);
|
|
|
|
|
|
|
|
const VarDecl *var = emission.Variable;
|
|
|
|
QualType type = var->getType();
|
|
|
|
|
|
|
|
CleanupKind cleanupKind = NormalAndEHCleanup;
|
|
|
|
CodeGenFunction::Destroyer *destroyer = 0;
|
|
|
|
|
|
|
|
switch (dtorKind) {
|
|
|
|
case QualType::DK_none:
|
|
|
|
llvm_unreachable("no cleanup for trivially-destructible variable");
|
|
|
|
|
|
|
|
case QualType::DK_cxx_destructor:
|
|
|
|
// If there's an NRVO flag on the emission, we need a different
|
|
|
|
// cleanup.
|
|
|
|
if (emission.NRVOFlag) {
|
|
|
|
assert(!type->isArrayType());
|
|
|
|
CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
|
|
|
|
EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr, dtor,
|
|
|
|
emission.NRVOFlag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QualType::DK_objc_strong_lifetime:
|
|
|
|
// Suppress cleanups for pseudo-strong variables.
|
|
|
|
if (var->isARCPseudoStrong()) return;
|
|
|
|
|
|
|
|
// Otherwise, consider whether to use an EH cleanup or not.
|
|
|
|
cleanupKind = getARCCleanupKind();
|
|
|
|
|
|
|
|
// Use the imprecise destroyer by default.
|
|
|
|
if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
|
|
|
|
destroyer = CodeGenFunction::destroyARCStrongImprecise;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case QualType::DK_objc_weak_lifetime:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't chosen a more specific destroyer, use the default.
|
|
|
|
if (!destroyer) destroyer = &getDestroyer(dtorKind);
|
2011-07-11 16:38:19 +08:00
|
|
|
|
|
|
|
// Use an EH cleanup in array destructors iff the destructor itself
|
|
|
|
// is being pushed as an EH cleanup.
|
|
|
|
bool useEHCleanup = (cleanupKind & EHCleanup);
|
|
|
|
EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
|
|
|
|
useEHCleanup);
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
|
2011-02-22 15:16:58 +08:00
|
|
|
assert(emission.Variable && "emission was not valid!");
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// If this was emitted as a global constant, we're done.
|
|
|
|
if (emission.wasEmittedAsGlobal()) return;
|
|
|
|
|
2011-02-22 15:16:58 +08:00
|
|
|
const VarDecl &D = *emission.Variable;
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
// Check the type for a cleanup.
|
|
|
|
if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
|
|
|
|
emitAutoVarTypeCleanup(emission, dtorKind);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-06-25 07:21:27 +08:00
|
|
|
// In GC mode, honor objc_precise_lifetime.
|
|
|
|
if (getLangOptions().getGCMode() != LangOptions::NonGC &&
|
|
|
|
D.hasAttr<ObjCPreciseLifetimeAttr>()) {
|
|
|
|
EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
|
|
|
|
}
|
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// Handle the cleanup attribute.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
|
2009-02-08 07:51:38 +08:00
|
|
|
const FunctionDecl *FD = CA->getFunctionDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
llvm::Constant *F = CGM.GetAddrOfFunction(FD);
|
2009-02-08 07:51:38 +08:00
|
|
|
assert(F && "Could not find function!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-26 08:34:20 +08:00
|
|
|
const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
|
2011-02-22 14:44:22 +08:00
|
|
|
EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
|
2009-02-08 07:51:38 +08:00
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2011-02-22 14:44:22 +08:00
|
|
|
// If this is a block variable, call _Block_object_destroy
|
|
|
|
// (on the unforwarded address).
|
2011-03-31 09:59:53 +08:00
|
|
|
if (emission.IsByRef)
|
|
|
|
enterByrefCleanup(emission);
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
CodeGenFunction::Destroyer &
|
|
|
|
CodeGenFunction::getDestroyer(QualType::DestructionKind kind) {
|
2011-07-09 17:09:00 +08:00
|
|
|
// This is surprisingly compiler-dependent. GCC 4.2 can't bind
|
|
|
|
// references to functions directly in returns, and using '*&foo'
|
|
|
|
// confuses MSVC. Luckily, the following code pattern works in both.
|
|
|
|
Destroyer *destroyer = 0;
|
2011-07-09 09:37:26 +08:00
|
|
|
switch (kind) {
|
|
|
|
case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
|
2011-07-09 17:09:00 +08:00
|
|
|
case QualType::DK_cxx_destructor:
|
|
|
|
destroyer = &destroyCXXObject;
|
|
|
|
break;
|
|
|
|
case QualType::DK_objc_strong_lifetime:
|
|
|
|
destroyer = &destroyARCStrongPrecise;
|
|
|
|
break;
|
|
|
|
case QualType::DK_objc_weak_lifetime:
|
|
|
|
destroyer = &destroyARCWeak;
|
|
|
|
break;
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
2011-07-09 17:09:00 +08:00
|
|
|
return *destroyer;
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, llvm::Value *addr,
|
2011-07-11 16:38:19 +08:00
|
|
|
QualType type, Destroyer &destroyer,
|
|
|
|
bool useEHCleanupForArray) {
|
|
|
|
EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
|
|
|
|
useEHCleanupForArray);
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
/// emitDestroy - Immediately perform the destruction of the given
|
|
|
|
/// object.
|
|
|
|
///
|
|
|
|
/// \param addr - the address of the object; a type*
|
|
|
|
/// \param type - the type of the object; if an array type, all
|
|
|
|
/// objects are destroyed in reverse order
|
|
|
|
/// \param destroyer - the function to call to destroy individual
|
|
|
|
/// elements
|
|
|
|
/// \param useEHCleanupForArray - whether an EH cleanup should be
|
|
|
|
/// used when destroying array elements, in case one of the
|
|
|
|
/// destructions throws an exception
|
2011-07-09 09:37:26 +08:00
|
|
|
void CodeGenFunction::emitDestroy(llvm::Value *addr, QualType type,
|
2011-07-11 16:38:19 +08:00
|
|
|
Destroyer &destroyer,
|
|
|
|
bool useEHCleanupForArray) {
|
2011-07-09 09:37:26 +08:00
|
|
|
const ArrayType *arrayType = getContext().getAsArrayType(type);
|
|
|
|
if (!arrayType)
|
|
|
|
return destroyer(*this, addr, type);
|
|
|
|
|
|
|
|
llvm::Value *begin = addr;
|
|
|
|
llvm::Value *length = emitArrayLength(arrayType, type, begin);
|
|
|
|
llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
|
2011-07-11 16:38:19 +08:00
|
|
|
emitArrayDestroy(begin, end, type, destroyer, useEHCleanupForArray);
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
/// emitArrayDestroy - Destroys all the elements of the given array,
|
|
|
|
/// beginning from last to first. The array cannot be zero-length.
|
|
|
|
///
|
|
|
|
/// \param begin - a type* denoting the first element of the array
|
|
|
|
/// \param end - a type* denoting one past the end of the array
|
|
|
|
/// \param type - the element type of the array
|
|
|
|
/// \param destroyer - the function to call to destroy elements
|
|
|
|
/// \param useEHCleanup - whether to push an EH cleanup to destroy
|
|
|
|
/// the remaining elements in case the destruction of a single
|
|
|
|
/// element throws
|
2011-07-09 09:37:26 +08:00
|
|
|
void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
|
|
|
|
llvm::Value *end,
|
|
|
|
QualType type,
|
2011-07-11 16:38:19 +08:00
|
|
|
Destroyer &destroyer,
|
|
|
|
bool useEHCleanup) {
|
2011-07-09 09:37:26 +08:00
|
|
|
assert(!type->isArrayType());
|
|
|
|
|
|
|
|
// The basic structure here is a do-while loop, because we don't
|
|
|
|
// need to check for the zero-element case.
|
|
|
|
llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
|
|
|
|
llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
|
|
|
|
|
|
|
|
// Enter the loop body, making that address the current address.
|
|
|
|
llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
|
|
|
|
EmitBlock(bodyBB);
|
|
|
|
llvm::PHINode *elementPast =
|
|
|
|
Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
|
|
|
|
elementPast->addIncoming(end, entryBB);
|
|
|
|
|
|
|
|
// Shift the address back by one element.
|
|
|
|
llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
|
|
|
|
llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
|
|
|
|
"arraydestroy.element");
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
if (useEHCleanup)
|
|
|
|
pushRegularPartialArrayCleanup(begin, element, type, destroyer);
|
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
// Perform the actual destruction there.
|
|
|
|
destroyer(*this, element, type);
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
if (useEHCleanup)
|
|
|
|
PopCleanupBlock();
|
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
// Check whether we've reached the end.
|
|
|
|
llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
|
|
|
|
Builder.CreateCondBr(done, doneBB, bodyBB);
|
|
|
|
elementPast->addIncoming(element, Builder.GetInsertBlock());
|
|
|
|
|
|
|
|
// Done.
|
|
|
|
EmitBlock(doneBB);
|
|
|
|
}
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
/// Perform partial array destruction as if in an EH cleanup. Unlike
|
|
|
|
/// emitArrayDestroy, the element type here may still be an array type.
|
|
|
|
///
|
|
|
|
/// Essentially does an emitArrayDestroy, but checking for the
|
|
|
|
/// possibility of a zero-length array (in case the initializer for
|
|
|
|
/// the first element throws).
|
|
|
|
static void emitPartialArrayDestroy(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *begin, llvm::Value *end,
|
|
|
|
QualType type,
|
|
|
|
CodeGenFunction::Destroyer &destroyer) {
|
|
|
|
// Check whether the array is empty. For the sake of prettier IR,
|
|
|
|
// we want to jump to the end of the array destroy loop instead of
|
|
|
|
// jumping to yet another block. We can do this with some modest
|
|
|
|
// assumptions about how emitArrayDestroy works.
|
|
|
|
|
|
|
|
llvm::Value *earlyTest =
|
|
|
|
CGF.Builder.CreateICmpEQ(begin, end, "pad.isempty");
|
|
|
|
|
|
|
|
llvm::BasicBlock *nextBB = CGF.createBasicBlock("pad.arraydestroy");
|
|
|
|
|
|
|
|
// Temporarily, build the conditional branch with identical
|
|
|
|
// successors. We'll patch this in a bit.
|
|
|
|
llvm::BranchInst *br =
|
|
|
|
CGF.Builder.CreateCondBr(earlyTest, nextBB, nextBB);
|
|
|
|
CGF.EmitBlock(nextBB);
|
|
|
|
|
|
|
|
llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
|
|
|
|
|
|
|
|
// If the element type is itself an array, drill down.
|
|
|
|
llvm::SmallVector<llvm::Value*,4> gepIndices;
|
|
|
|
gepIndices.push_back(zero);
|
|
|
|
while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
|
|
|
|
// VLAs don't require a GEP index to walk into.
|
|
|
|
if (!isa<VariableArrayType>(arrayType))
|
|
|
|
gepIndices.push_back(zero);
|
|
|
|
type = arrayType->getElementType();
|
|
|
|
}
|
|
|
|
if (gepIndices.size() != 1) {
|
|
|
|
begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices.begin(),
|
|
|
|
gepIndices.end(), "pad.arraybegin");
|
|
|
|
end = CGF.Builder.CreateInBoundsGEP(end, gepIndices.begin(),
|
|
|
|
gepIndices.end(), "pad.arrayend");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we know that the array isn't empty, destroy it. We
|
|
|
|
// don't ever need an EH cleanup because we assume that we're in an
|
|
|
|
// EH cleanup ourselves, so a throwing destructor causes an
|
|
|
|
// immediate terminate.
|
|
|
|
CGF.emitArrayDestroy(begin, end, type, destroyer, /*useEHCleanup*/ false);
|
|
|
|
|
|
|
|
// Set the conditional branch's 'false' successor to doneBB.
|
|
|
|
llvm::BasicBlock *doneBB = CGF.Builder.GetInsertBlock();
|
|
|
|
assert(CGF.Builder.GetInsertPoint() == doneBB->begin());
|
|
|
|
br->setSuccessor(0, doneBB);
|
|
|
|
}
|
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
namespace {
|
2011-07-11 16:38:19 +08:00
|
|
|
/// RegularPartialArrayDestroy - a cleanup which performs a partial
|
|
|
|
/// array destroy where the end pointer is regularly determined and
|
|
|
|
/// does not need to be loaded from a local.
|
|
|
|
class RegularPartialArrayDestroy : public EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *ArrayBegin;
|
|
|
|
llvm::Value *ArrayEnd;
|
|
|
|
QualType ElementType;
|
|
|
|
CodeGenFunction::Destroyer &Destroyer;
|
|
|
|
public:
|
|
|
|
RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
|
|
|
|
QualType elementType,
|
|
|
|
CodeGenFunction::Destroyer *destroyer)
|
|
|
|
: ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
|
|
|
|
ElementType(elementType), Destroyer(*destroyer) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool isForEH) {
|
|
|
|
emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
|
|
|
|
ElementType, Destroyer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// IrregularPartialArrayDestroy - a cleanup which performs a
|
|
|
|
/// partial array destroy where the end pointer is irregularly
|
|
|
|
/// determined and must be loaded from a local.
|
|
|
|
class IrregularPartialArrayDestroy : public EHScopeStack::Cleanup {
|
2011-07-09 09:37:26 +08:00
|
|
|
llvm::Value *ArrayBegin;
|
|
|
|
llvm::Value *ArrayEndPointer;
|
|
|
|
QualType ElementType;
|
|
|
|
CodeGenFunction::Destroyer &Destroyer;
|
|
|
|
public:
|
2011-07-11 16:38:19 +08:00
|
|
|
IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
|
|
|
|
llvm::Value *arrayEndPointer,
|
|
|
|
QualType elementType,
|
|
|
|
CodeGenFunction::Destroyer *destroyer)
|
2011-07-09 09:37:26 +08:00
|
|
|
: ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
|
|
|
|
ElementType(elementType), Destroyer(*destroyer) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool isForEH) {
|
|
|
|
llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
|
2011-07-11 16:38:19 +08:00
|
|
|
emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
|
|
|
|
ElementType, Destroyer);
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-07-11 16:38:19 +08:00
|
|
|
/// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
|
|
|
|
/// already-constructed elements of the given array. The cleanup
|
|
|
|
/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
|
|
|
|
///
|
|
|
|
/// \param elementType - the immediate element type of the array;
|
|
|
|
/// possibly still an array type
|
|
|
|
/// \param array - a value of type elementType*
|
|
|
|
/// \param destructionKind - the kind of destruction required
|
|
|
|
/// \param initializedElementCount - a value of type size_t* holding
|
|
|
|
/// the number of successfully-constructed elements
|
|
|
|
void CodeGenFunction::pushIrregularPartialArrayCleanup(llvm::Value *array,
|
|
|
|
llvm::Value *arrayEndPointer,
|
|
|
|
QualType elementType,
|
|
|
|
Destroyer &destroyer) {
|
|
|
|
// FIXME: can this be in a conditional expression?
|
|
|
|
EHStack.pushCleanup<IrregularPartialArrayDestroy>(EHCleanup, array,
|
|
|
|
arrayEndPointer,
|
|
|
|
elementType, &destroyer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
|
2011-07-09 09:37:26 +08:00
|
|
|
/// already-constructed elements of the given array. The cleanup
|
2011-07-11 16:38:19 +08:00
|
|
|
/// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
|
2011-07-09 09:37:26 +08:00
|
|
|
///
|
|
|
|
/// \param elementType - the immediate element type of the array;
|
|
|
|
/// possibly still an array type
|
|
|
|
/// \param array - a value of type elementType*
|
|
|
|
/// \param destructionKind - the kind of destruction required
|
|
|
|
/// \param initializedElementCount - a value of type size_t* holding
|
|
|
|
/// the number of successfully-constructed elements
|
2011-07-11 16:38:19 +08:00
|
|
|
void CodeGenFunction::pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
|
|
|
|
llvm::Value *arrayEnd,
|
|
|
|
QualType elementType,
|
|
|
|
Destroyer &destroyer) {
|
2011-07-09 09:37:26 +08:00
|
|
|
// FIXME: can this be in a conditional expression?
|
2011-07-11 16:38:19 +08:00
|
|
|
EHStack.pushCleanup<RegularPartialArrayDestroy>(EHCleanup,
|
|
|
|
arrayBegin, arrayEnd,
|
|
|
|
elementType, &destroyer);
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
namespace {
|
|
|
|
/// A cleanup to perform a release of an object at the end of a
|
|
|
|
/// function. This is used to balance out the incoming +1 of a
|
|
|
|
/// ns_consumed argument when we can't reasonably do that just by
|
|
|
|
/// not doing the initial retain for a __block argument.
|
|
|
|
struct ConsumeARCParameter : EHScopeStack::Cleanup {
|
|
|
|
ConsumeARCParameter(llvm::Value *param) : Param(param) {}
|
|
|
|
|
|
|
|
llvm::Value *Param;
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
CGF.EmitARCRelease(Param, /*precise*/ false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// Emit an alloca (or GlobalValue depending on target)
|
2008-05-08 13:58:21 +08:00
|
|
|
/// for the specified parameter and set up LocalDeclMap.
|
2011-03-04 04:13:15 +08:00
|
|
|
void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg,
|
|
|
|
unsigned ArgNo) {
|
2008-08-16 11:19:19 +08:00
|
|
|
// FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
|
2008-10-31 17:52:39 +08:00
|
|
|
assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
|
2008-08-16 11:19:19 +08:00
|
|
|
"Invalid argument to EmitParmDecl");
|
2011-02-23 06:38:33 +08:00
|
|
|
|
|
|
|
Arg->setName(D.getName());
|
|
|
|
|
|
|
|
// Use better IR generation for certain implicit parameters.
|
|
|
|
if (isa<ImplicitParamDecl>(D)) {
|
|
|
|
// The only implicit argument a block has is its literal.
|
|
|
|
if (BlockInfo) {
|
|
|
|
LocalDeclMap[&D] = Arg;
|
|
|
|
|
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
|
|
|
DI->setLocation(D.getLocation());
|
|
|
|
DI->EmitDeclareOfBlockLiteralArgVariable(*BlockInfo, Arg, Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-14 04:44:40 +08:00
|
|
|
llvm::Value *DeclPtr;
|
2010-02-09 06:53:07 +08:00
|
|
|
// If this is an aggregate or variable sized value, reuse the input pointer.
|
|
|
|
if (!Ty->isConstantSizeType() ||
|
|
|
|
CodeGenFunction::hasAggregateLLVMType(Ty)) {
|
2007-06-14 04:44:40 +08:00
|
|
|
DeclPtr = Arg;
|
|
|
|
} else {
|
2010-02-09 06:53:07 +08:00
|
|
|
// Otherwise, create a temporary to hold the value.
|
2010-02-09 10:48:28 +08:00
|
|
|
DeclPtr = CreateMemTemp(Ty, D.getName() + ".addr");
|
2010-02-09 06:53:07 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
bool doStore = true;
|
|
|
|
|
|
|
|
Qualifiers qs = Ty.getQualifiers();
|
|
|
|
|
|
|
|
if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
|
|
|
|
// We honor __attribute__((ns_consumed)) for types with lifetime.
|
|
|
|
// For __strong, it's handled by just skipping the initial retain;
|
|
|
|
// otherwise we have to balance out the initial +1 with an extra
|
|
|
|
// cleanup to do the release at the end of the function.
|
|
|
|
bool isConsumed = D.hasAttr<NSConsumedAttr>();
|
|
|
|
|
|
|
|
// 'self' is always formally __strong, but if this is not an
|
|
|
|
// init method then we don't want to retain it.
|
2011-06-17 14:42:21 +08:00
|
|
|
if (D.isARCPseudoStrong()) {
|
2011-06-16 07:02:42 +08:00
|
|
|
const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
|
|
|
|
assert(&D == method->getSelfDecl());
|
2011-06-17 14:42:21 +08:00
|
|
|
assert(lt == Qualifiers::OCL_Strong);
|
|
|
|
assert(qs.hasConst());
|
2011-06-16 07:02:42 +08:00
|
|
|
assert(method->getMethodFamily() != OMF_init);
|
2011-06-16 07:40:09 +08:00
|
|
|
(void) method;
|
2011-06-16 07:02:42 +08:00
|
|
|
lt = Qualifiers::OCL_ExplicitNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lt == Qualifiers::OCL_Strong) {
|
|
|
|
if (!isConsumed)
|
|
|
|
// Don't use objc_retainBlock for block pointers, because we
|
|
|
|
// don't want to Block_copy something just because we got it
|
|
|
|
// as a parameter.
|
|
|
|
Arg = EmitARCRetainNonBlock(Arg);
|
|
|
|
} else {
|
|
|
|
// Push the cleanup for a consumed parameter.
|
|
|
|
if (isConsumed)
|
|
|
|
EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), Arg);
|
|
|
|
|
|
|
|
if (lt == Qualifiers::OCL_Weak) {
|
|
|
|
EmitARCInitWeak(DeclPtr, Arg);
|
|
|
|
doStore = false; // The weak init is a store, no need to do two
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the cleanup scope.
|
|
|
|
EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
|
|
|
|
}
|
|
|
|
|
2010-02-09 06:53:07 +08:00
|
|
|
// Store the initial value into the alloca.
|
2011-06-25 10:11:03 +08:00
|
|
|
if (doStore) {
|
|
|
|
LValue lv = MakeAddrLValue(DeclPtr, Ty,
|
|
|
|
getContext().getDeclAlign(&D).getQuantity());
|
|
|
|
EmitStoreOfScalar(Arg, lv);
|
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
|
|
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
|
|
|
DMEntry = DeclPtr;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for param declaration.
|
2011-06-16 01:57:08 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo())
|
2011-03-04 04:13:15 +08:00
|
|
|
DI->EmitDeclareOfArgVariable(&D, DeclPtr, ArgNo, Builder);
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|