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:
|
|
|
|
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:
|
|
|
|
|
|
|
|
assert(0 && "Declaration not 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]
|
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
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
case Decl::Typedef: { // typedef int X;
|
|
|
|
const TypedefDecl &TD = cast<TypedefDecl>(D);
|
|
|
|
QualType Ty = TD.getUnderlyingType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-21 05:51:53 +08:00
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
|
|
|
}
|
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,
|
|
|
|
D.isThreadSpecified(), Ty.getAddressSpace());
|
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(),
|
2009-12-05 16:22:11 +08:00
|
|
|
D.getType().getAddressSpace());
|
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())
|
|
|
|
EmitVLASize(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());
|
2010-12-02 12:27:29 +08:00
|
|
|
const llvm::Type *LPtrTy = LTy->getPointerTo(D.getType().getAddressSpace());
|
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
|
|
|
|
2009-09-12 10:14:24 +08:00
|
|
|
unsigned CodeGenFunction::getByRefValueLLVMField(const ValueDecl *VD) const {
|
|
|
|
assert(ByRefValueInfo.count(VD) && "Did not find value!");
|
|
|
|
|
|
|
|
return ByRefValueInfo.find(VD)->second.second;
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
/// BuildByRefType - This routine changes a __block variable declared as T x
|
|
|
|
/// into:
|
|
|
|
///
|
|
|
|
/// struct {
|
|
|
|
/// void *__isa;
|
|
|
|
/// void *__forwarding;
|
|
|
|
/// int32_t __flags;
|
|
|
|
/// int32_t __size;
|
2009-09-22 10:12:52 +08:00
|
|
|
/// void *__copy_helper; // only if needed
|
|
|
|
/// void *__destroy_helper; // only if needed
|
|
|
|
/// char padding[X]; // only if needed
|
2009-03-04 11:23:46 +08:00
|
|
|
/// T x;
|
|
|
|
/// } x
|
|
|
|
///
|
2009-09-09 10:51:03 +08:00
|
|
|
const llvm::Type *CodeGenFunction::BuildByRefType(const ValueDecl *D) {
|
2009-09-12 10:14:24 +08:00
|
|
|
std::pair<const llvm::Type *, unsigned> &Info = ByRefValueInfo[D];
|
|
|
|
if (Info.first)
|
|
|
|
return Info.first;
|
|
|
|
|
2009-09-09 10:51:03 +08:00
|
|
|
QualType Ty = D->getType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
std::vector<const llvm::Type *> Types;
|
2009-09-10 09:32:12 +08:00
|
|
|
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::PointerType *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-10 09:32:12 +08:00
|
|
|
llvm::PATypeHolder ByRefTypeHolder = llvm::OpaqueType::get(VMContext);
|
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// void *__isa;
|
|
|
|
Types.push_back(Int8PtrTy);
|
2009-09-12 10:14:24 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// void *__forwarding;
|
|
|
|
Types.push_back(llvm::PointerType::getUnqual(ByRefTypeHolder));
|
2009-09-12 10:14:24 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
// int32_t __flags;
|
2010-06-27 15:15:29 +08:00
|
|
|
Types.push_back(Int32Ty);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
// int32_t __size;
|
2010-06-27 15:15:29 +08:00
|
|
|
Types.push_back(Int32Ty);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
bool HasCopyAndDispose = BlockRequiresCopying(Ty);
|
|
|
|
if (HasCopyAndDispose) {
|
|
|
|
/// void *__copy_helper;
|
|
|
|
Types.push_back(Int8PtrTy);
|
|
|
|
|
|
|
|
/// void *__destroy_helper;
|
|
|
|
Types.push_back(Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Packed = false;
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits Align = getContext().getDeclAlign(D);
|
|
|
|
if (Align > CharUnits::fromQuantity(Target.getPointerAlign(0) / 8)) {
|
2009-09-12 10:44:18 +08:00
|
|
|
// We have to insert padding.
|
|
|
|
|
|
|
|
// The struct above has 2 32-bit integers.
|
|
|
|
unsigned CurrentOffsetInBytes = 4 * 2;
|
|
|
|
|
|
|
|
// And either 2 or 4 pointers.
|
|
|
|
CurrentOffsetInBytes += (HasCopyAndDispose ? 4 : 2) *
|
|
|
|
CGM.getTargetData().getTypeAllocSize(Int8PtrTy);
|
|
|
|
|
|
|
|
// Align the offset.
|
|
|
|
unsigned AlignedOffsetInBytes =
|
2010-01-28 01:10:57 +08:00
|
|
|
llvm::RoundUpToAlignment(CurrentOffsetInBytes, Align.getQuantity());
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
unsigned NumPaddingBytes = AlignedOffsetInBytes - CurrentOffsetInBytes;
|
2009-09-14 01:55:13 +08:00
|
|
|
if (NumPaddingBytes > 0) {
|
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
|
2009-10-21 08:42:55 +08:00
|
|
|
// FIXME: We need a sema error for alignment larger than the minimum of
|
|
|
|
// the maximal stack alignmint and the alignment of malloc on the system.
|
2009-09-14 01:55:13 +08:00
|
|
|
if (NumPaddingBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPaddingBytes);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-14 01:55:13 +08:00
|
|
|
Types.push_back(Ty);
|
2009-09-12 10:44:18 +08:00
|
|
|
|
2009-09-14 01:55:13 +08:00
|
|
|
// We want a packed struct.
|
|
|
|
Packed = true;
|
|
|
|
}
|
2009-09-12 10:44:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// T x;
|
2010-09-04 07:07:53 +08:00
|
|
|
Types.push_back(ConvertTypeForMem(Ty));
|
2009-09-10 09:32:12 +08:00
|
|
|
|
2009-09-12 10:44:18 +08:00
|
|
|
const llvm::Type *T = llvm::StructType::get(VMContext, Types, Packed);
|
2009-09-10 09:32:12 +08:00
|
|
|
|
|
|
|
cast<llvm::OpaqueType>(ByRefTypeHolder.get())->refineAbstractTypeTo(T);
|
|
|
|
CGM.getModule().addTypeName("struct.__block_byref_" + D->getNameAsString(),
|
|
|
|
ByRefTypeHolder.get());
|
|
|
|
|
2009-09-12 10:14:24 +08:00
|
|
|
Info.first = ByRefTypeHolder.get();
|
2009-09-12 10:44:18 +08:00
|
|
|
|
|
|
|
Info.second = Types.size() - 1;
|
2009-09-12 10:14:24 +08:00
|
|
|
|
|
|
|
return Info.first;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 04:32:21 +08:00
|
|
|
namespace {
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallArrayDtor : EHScopeStack::Cleanup {
|
2010-07-14 04:32:21 +08:00
|
|
|
CallArrayDtor(const CXXDestructorDecl *Dtor,
|
|
|
|
const ConstantArrayType *Type,
|
|
|
|
llvm::Value *Loc)
|
|
|
|
: Dtor(Dtor), Type(Type), Loc(Loc) {}
|
|
|
|
|
|
|
|
const CXXDestructorDecl *Dtor;
|
|
|
|
const ConstantArrayType *Type;
|
|
|
|
llvm::Value *Loc;
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
QualType BaseElementTy = CGF.getContext().getBaseElementType(Type);
|
|
|
|
const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy);
|
|
|
|
BasePtr = llvm::PointerType::getUnqual(BasePtr);
|
|
|
|
llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(Loc, BasePtr);
|
|
|
|
CGF.EmitCXXAggrDestructorCall(Dtor, Type, BaseAddrPtr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallVarDtor : EHScopeStack::Cleanup {
|
2010-07-14 04:32:21 +08:00
|
|
|
CallVarDtor(const CXXDestructorDecl *Dtor,
|
|
|
|
llvm::Value *NRVOFlag,
|
|
|
|
llvm::Value *Loc)
|
|
|
|
: Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(Loc) {}
|
|
|
|
|
|
|
|
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 14:13:08 +08:00
|
|
|
namespace {
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
llvm::Value *Addr;
|
|
|
|
const VarDecl &Var;
|
|
|
|
|
|
|
|
CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
|
|
|
|
llvm::Value *Addr, const VarDecl *Var)
|
|
|
|
: CleanupFn(CleanupFn), FnInfo(*Info), Addr(Addr), Var(*Var) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
// 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;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Arg),
|
|
|
|
CGF.getContext().getPointerType(Var.getType())));
|
|
|
|
CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallBlockRelease : EHScopeStack::Cleanup {
|
2010-07-21 14:13:08 +08:00
|
|
|
llvm::Value *Addr;
|
|
|
|
CallBlockRelease(llvm::Value *Addr) : Addr(Addr) {}
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
|
|
|
llvm::Value *V = CGF.Builder.CreateStructGEP(Addr, 1, "forwarding");
|
|
|
|
V = CGF.Builder.CreateLoad(V);
|
|
|
|
CGF.BuildBlockRelease(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
CGBuilderTy &Builder) {
|
|
|
|
// 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())
|
|
|
|
Builder.CreateStore(Init, Loc);
|
|
|
|
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),
|
|
|
|
Builder);
|
|
|
|
}
|
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.
|
2010-10-15 12:57:14 +08:00
|
|
|
void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
|
|
|
SpecialInitFn *SpecialInit) {
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2010-08-21 10:24:36 +08:00
|
|
|
unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
|
2009-06-30 10:34:44 +08:00
|
|
|
bool isByRef = D.hasAttr<BlocksAttr>();
|
2009-03-05 09:23:13 +08:00
|
|
|
bool needsDispose = false;
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits Align = CharUnits::Zero();
|
2009-12-05 16:22:11 +08:00
|
|
|
bool IsSimpleConstantInitializer = false;
|
2007-06-02 12:53:11 +08:00
|
|
|
|
2010-05-15 14:46:45 +08:00
|
|
|
bool NRVO = false;
|
2010-05-17 23:52:46 +08:00
|
|
|
llvm::Value *NRVOFlag = 0;
|
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()) {
|
2010-05-15 14:46:45 +08:00
|
|
|
NRVO = getContext().getLangOptions().ElideConstructors &&
|
|
|
|
D.isNRVOVariable();
|
2009-12-05 14:49:57 +08:00
|
|
|
// If this value is an array or struct, is POD, and if the initializer is
|
2010-05-15 14:46:45 +08:00
|
|
|
// a staticly determinable constant, try to optimize it (unless the NRVO
|
|
|
|
// is already optimizing this).
|
2010-08-03 05:13:48 +08:00
|
|
|
if (!NRVO && D.getInit() && !isByRef &&
|
2009-12-05 14:49:57 +08:00
|
|
|
(Ty->isArrayType() || Ty->isRecordType()) &&
|
|
|
|
Ty->isPODType() &&
|
2010-08-03 05:13:48 +08:00
|
|
|
D.getInit()->isConstantInitializer(getContext(), false)) {
|
2009-12-05 14:49:57 +08:00
|
|
|
// If this variable is marked 'const', emit the value as a global.
|
|
|
|
if (CGM.getCodeGenOpts().MergeAllConstants &&
|
|
|
|
Ty.isConstant(getContext())) {
|
2010-10-15 12:57:14 +08:00
|
|
|
EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
|
2009-11-04 09:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-05 16:22:11 +08:00
|
|
|
|
|
|
|
IsSimpleConstantInitializer = 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();
|
|
|
|
NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
|
2010-05-17 23:52:46 +08:00
|
|
|
Builder.CreateStore(Zero, NRVOFlag);
|
|
|
|
|
|
|
|
// Record the NRVO flag for this variable.
|
|
|
|
NRVOFlags[&D] = NRVOFlag;
|
|
|
|
}
|
|
|
|
}
|
2010-05-15 14:46:45 +08:00
|
|
|
} else {
|
|
|
|
if (isByRef)
|
|
|
|
LTy = BuildByRefType(&D);
|
|
|
|
|
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
|
|
|
Alloc->setName(D.getNameAsString());
|
|
|
|
|
|
|
|
Align = getContext().getDeclAlign(&D);
|
|
|
|
if (isByRef)
|
|
|
|
Align = std::max(Align,
|
|
|
|
CharUnits::fromQuantity(Target.getPointerAlign(0) / 8));
|
|
|
|
Alloc->setAlignment(Align.getQuantity());
|
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
// FIXME: Can this happen?
|
2008-12-21 04:46:34 +08:00
|
|
|
if (Ty->isVariablyModifiedType())
|
|
|
|
EmitVLASize(Ty);
|
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.
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *LTy = llvm::Type::getInt8PtrTy(VMContext);
|
2008-12-12 15:38:43 +08:00
|
|
|
llvm::Value *Stack = CreateTempAlloca(LTy, "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.
|
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
|
|
|
|
2008-12-12 15:38:43 +08:00
|
|
|
// Get the element type.
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::Type *LElemTy = ConvertTypeForMem(Ty);
|
Improve codegen for initializer lists to use memset more aggressively
when an initializer is variable (I handled the constant case in a previous
patch). This has three pieces:
1. Enhance AggValueSlot to have a 'isZeroed' bit to tell CGExprAgg that
the memory being stored into has previously been memset to zero.
2. Teach CGExprAgg to not emit stores of zero to isZeroed memory.
3. Teach CodeGenFunction::EmitAggExpr to scan initializers to determine
whether they are profitable to emit a memset + inividual stores vs
stores for everything.
The heuristic used is that a global has to be more than 16 bytes and
has to be 3/4 zero to be candidate for this xform. The two testcases
are illustrative of the scenarios this catches. We now codegen test9 into:
call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 400, i32 4, i1 false)
%.array = getelementptr inbounds [100 x i32]* %Arr, i32 0, i32 0
%tmp = load i32* %X.addr, align 4
store i32 %tmp, i32* %.array
and test10 into:
call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 392, i32 8, i1 false)
%tmp = getelementptr inbounds %struct.b* %S, i32 0, i32 0
%tmp1 = getelementptr inbounds %struct.a* %tmp, i32 0, i32 0
%tmp2 = load i32* %X.addr, align 4
store i32 %tmp2, i32* %tmp1, align 4
%tmp5 = getelementptr inbounds %struct.b* %S, i32 0, i32 3
%tmp10 = getelementptr inbounds %struct.a* %tmp5, i32 0, i32 4
%tmp11 = load i32* %X.addr, align 4
store i32 %tmp11, i32* %tmp10, align 4
Previously we produced 99 stores of zero for test9 and also tons for test10.
This xforms should substantially speed up -O0 builds when it kicks in as well
as reducing code size and optimizer heartburn on insane cases. This resolves
PR279.
llvm-svn: 120692
2010-12-02 15:07:26 +08:00
|
|
|
const llvm::Type *LElemPtrTy = LElemTy->getPointerTo(Ty.getAddressSpace());
|
2008-12-12 15:38:43 +08:00
|
|
|
|
2008-12-21 04:46:34 +08:00
|
|
|
llvm::Value *VLASize = EmitVLASize(Ty);
|
2008-12-12 15:38:43 +08:00
|
|
|
|
|
|
|
// Allocate memory for the array.
|
2009-09-27 02:16:06 +08:00
|
|
|
llvm::AllocaInst *VLA =
|
|
|
|
Builder.CreateAlloca(llvm::Type::getInt8Ty(VMContext), VLASize, "vla");
|
2010-01-28 01:10:57 +08:00
|
|
|
VLA->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
2009-09-27 02:16:06 +08:00
|
|
|
|
2008-12-21 07:11:59 +08:00
|
|
|
DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
|
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;
|
2008-05-30 18:30:31 +08:00
|
|
|
|
|
|
|
// Emit debug info for local var declaration.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2009-07-19 15:03:11 +08:00
|
|
|
assert(HaveInsertPoint() && "Unexpected unreachable point!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-18 00:15:48 +08:00
|
|
|
DI->setLocation(D.getLocation());
|
2009-05-22 21:54:25 +08:00
|
|
|
if (Target.useGlobalsForAutomaticVariables()) {
|
|
|
|
DI->EmitGlobalVariable(static_cast<llvm::GlobalVariable *>(DeclPtr), &D);
|
2009-03-04 11:23:46 +08:00
|
|
|
} else
|
|
|
|
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
2008-05-30 18:30:31 +08:00
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
if (!ContainsLabel(Init))
|
|
|
|
Init = 0;
|
|
|
|
else
|
|
|
|
EnsureInsertPoint();
|
|
|
|
}
|
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (isByRef) {
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::PointerType *PtrToInt8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-07-19 14:58:07 +08:00
|
|
|
EnsureInsertPoint();
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *isa_field = Builder.CreateStructGEP(DeclPtr, 0);
|
|
|
|
llvm::Value *forwarding_field = Builder.CreateStructGEP(DeclPtr, 1);
|
|
|
|
llvm::Value *flags_field = Builder.CreateStructGEP(DeclPtr, 2);
|
|
|
|
llvm::Value *size_field = Builder.CreateStructGEP(DeclPtr, 3);
|
|
|
|
llvm::Value *V;
|
|
|
|
int flag = 0;
|
|
|
|
int flags = 0;
|
|
|
|
|
2009-03-07 14:04:31 +08:00
|
|
|
needsDispose = true;
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
if (Ty->isBlockPointerType()) {
|
|
|
|
flag |= BLOCK_FIELD_IS_BLOCK;
|
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
2010-11-17 08:21:28 +08:00
|
|
|
} else if (getContext().isObjCNSObjectType(Ty) ||
|
|
|
|
Ty->isObjCObjectPointerType()) {
|
2009-03-04 11:23:46 +08:00
|
|
|
flag |= BLOCK_FIELD_IS_OBJECT;
|
2009-03-06 12:53:30 +08:00
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
2010-12-03 01:02:11 +08:00
|
|
|
} else if (getContext().getBlockVarCopyInits(&D)) {
|
|
|
|
flag |= BLOCK_HAS_CXX_OBJ;
|
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
2009-03-07 14:04:31 +08:00
|
|
|
|
|
|
|
// FIXME: Someone double check this.
|
|
|
|
if (Ty.isObjCGCWeak())
|
|
|
|
flag |= BLOCK_FIELD_IS_WEAK;
|
2009-03-04 11:23:46 +08:00
|
|
|
|
|
|
|
int isa = 0;
|
2010-12-01 10:05:19 +08:00
|
|
|
if (flag & BLOCK_FIELD_IS_WEAK)
|
2009-03-04 11:23:46 +08:00
|
|
|
isa = 1;
|
2010-12-01 09:47:15 +08:00
|
|
|
V = Builder.CreateIntToPtr(Builder.getInt32(isa), PtrToInt8Ty, "isa");
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, isa_field);
|
|
|
|
|
2009-09-10 09:32:12 +08:00
|
|
|
Builder.CreateStore(DeclPtr, forwarding_field);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2010-12-01 09:47:15 +08:00
|
|
|
Builder.CreateStore(Builder.getInt32(flags), flags_field);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
const llvm::Type *V1;
|
|
|
|
V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
|
2010-12-01 09:47:15 +08:00
|
|
|
V = Builder.getInt32(CGM.GetTargetTypeStoreSize(V1).getQuantity());
|
2009-03-04 11:23:46 +08:00
|
|
|
Builder.CreateStore(V, size_field);
|
|
|
|
|
|
|
|
if (flags & BLOCK_HAS_COPY_DISPOSE) {
|
2010-11-16 03:19:38 +08:00
|
|
|
SynthesizeCopyDisposeHelpers = true;
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
|
2010-01-28 01:10:57 +08:00
|
|
|
Builder.CreateStore(BuildbyrefCopyHelper(DeclPtr->getType(), flag,
|
2010-12-03 01:02:11 +08:00
|
|
|
Align.getQuantity(), &D),
|
2009-03-06 14:12:24 +08:00
|
|
|
copy_helper);
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
|
2009-06-06 07:26:36 +08:00
|
|
|
Builder.CreateStore(BuildbyrefDestroyHelper(DeclPtr->getType(), flag,
|
2010-12-03 01:02:11 +08:00
|
|
|
Align.getQuantity(), &D),
|
2009-03-06 12:53:30 +08:00
|
|
|
destroy_helper);
|
2007-08-26 13:13:54 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-02-08 07:51:38 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
if (SpecialInit) {
|
|
|
|
SpecialInit(*this, D, DeclPtr);
|
|
|
|
} else if (Init) {
|
2010-03-13 05:40:43 +08:00
|
|
|
llvm::Value *Loc = DeclPtr;
|
|
|
|
if (isByRef)
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
|
|
D.getNameAsString());
|
|
|
|
|
2010-08-21 10:17:08 +08:00
|
|
|
bool isVolatile = getContext().getCanonicalType(Ty).isVolatileQualified();
|
2010-03-13 05:40:43 +08:00
|
|
|
|
|
|
|
// If the initializer was a simple constant initializer, we can optimize it
|
|
|
|
// in various ways.
|
|
|
|
if (IsSimpleConstantInitializer) {
|
2010-08-21 10:17:08 +08:00
|
|
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), Ty,this);
|
2010-03-13 05:40:43 +08:00
|
|
|
assert(Init != 0 && "Wasn't a simple constant init?");
|
|
|
|
|
|
|
|
llvm::Value *SizeVal =
|
2010-12-01 09:47:15 +08:00
|
|
|
llvm::ConstantInt::get(CGF.IntPtrTy,
|
2010-03-13 05:40:43 +08:00
|
|
|
getContext().getTypeSizeInChars(Ty).getQuantity());
|
|
|
|
|
2010-12-30 08:13:21 +08:00
|
|
|
const llvm::Type *BP = Builder.getInt8PtrTy();
|
2010-12-02 12:27:29 +08:00
|
|
|
if (Loc->getType() != BP)
|
|
|
|
Loc = Builder.CreateBitCast(Loc, BP, "tmp");
|
2010-04-04 11:10:52 +08:00
|
|
|
|
2010-12-01 10:05:19 +08:00
|
|
|
// If the initializer is all or mostly zeros, codegen with memset then do
|
|
|
|
// a few stores afterward.
|
|
|
|
if (shouldUseMemSetPlusStoresToInitialize(Init,
|
|
|
|
CGM.getTargetData().getTypeAllocSize(Init->getType()))) {
|
2010-12-30 08:13:21 +08:00
|
|
|
Builder.CreateMemSet(Loc, Builder.getInt8(0), SizeVal,
|
|
|
|
Align.getQuantity(), false);
|
2010-12-02 12:27:29 +08:00
|
|
|
if (!Init->isNullValue()) {
|
|
|
|
Loc = Builder.CreateBitCast(Loc, Init->getType()->getPointerTo());
|
|
|
|
emitStoresForInitAfterMemset(Init, Loc, Builder);
|
|
|
|
}
|
2010-12-01 10:05:19 +08:00
|
|
|
|
2010-03-13 05:40:43 +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(), Init->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
Init, Name, 0, false, 0);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
|
|
|
|
|
|
|
llvm::Value *SrcPtr = GV;
|
|
|
|
if (SrcPtr->getType() != BP)
|
|
|
|
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
2010-04-04 11:10:52 +08:00
|
|
|
|
2010-12-30 08:13:21 +08:00
|
|
|
Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, Align.getQuantity(), false);
|
2010-03-13 05:40:43 +08:00
|
|
|
}
|
|
|
|
} else if (Ty->isReferenceType()) {
|
2010-06-27 00:35:32 +08:00
|
|
|
RValue RV = EmitReferenceBindingToExpr(Init, &D);
|
2010-08-21 10:24:36 +08:00
|
|
|
EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Alignment, Ty);
|
2010-03-13 05:40:43 +08:00
|
|
|
} else if (!hasAggregateLLVMType(Init->getType())) {
|
|
|
|
llvm::Value *V = EmitScalarExpr(Init);
|
2010-08-21 10:24:36 +08:00
|
|
|
EmitStoreOfScalar(V, Loc, isVolatile, Alignment, Ty);
|
2010-03-13 05:40:43 +08:00
|
|
|
} else if (Init->getType()->isAnyComplexType()) {
|
|
|
|
EmitComplexExprIntoAddr(Init, Loc, isVolatile);
|
|
|
|
} else {
|
Improve codegen for initializer lists to use memset more aggressively
when an initializer is variable (I handled the constant case in a previous
patch). This has three pieces:
1. Enhance AggValueSlot to have a 'isZeroed' bit to tell CGExprAgg that
the memory being stored into has previously been memset to zero.
2. Teach CGExprAgg to not emit stores of zero to isZeroed memory.
3. Teach CodeGenFunction::EmitAggExpr to scan initializers to determine
whether they are profitable to emit a memset + inividual stores vs
stores for everything.
The heuristic used is that a global has to be more than 16 bytes and
has to be 3/4 zero to be candidate for this xform. The two testcases
are illustrative of the scenarios this catches. We now codegen test9 into:
call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 400, i32 4, i1 false)
%.array = getelementptr inbounds [100 x i32]* %Arr, i32 0, i32 0
%tmp = load i32* %X.addr, align 4
store i32 %tmp, i32* %.array
and test10 into:
call void @llvm.memset.p0i8.i64(i8* %0, i8 0, i64 392, i32 8, i1 false)
%tmp = getelementptr inbounds %struct.b* %S, i32 0, i32 0
%tmp1 = getelementptr inbounds %struct.a* %tmp, i32 0, i32 0
%tmp2 = load i32* %X.addr, align 4
store i32 %tmp2, i32* %tmp1, align 4
%tmp5 = getelementptr inbounds %struct.b* %S, i32 0, i32 3
%tmp10 = getelementptr inbounds %struct.a* %tmp5, i32 0, i32 4
%tmp11 = load i32* %X.addr, align 4
store i32 %tmp11, i32* %tmp10, align 4
Previously we produced 99 stores of zero for test9 and also tons for test10.
This xforms should substantially speed up -O0 builds when it kicks in as well
as reducing code size and optimizer heartburn on insane cases. This resolves
PR279.
llvm-svn: 120692
2010-12-02 15:07:26 +08:00
|
|
|
EmitAggExpr(Init, AggValueSlot::forAddr(Loc, isVolatile, true, false));
|
2010-03-13 05:40:43 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2009-08-04 04:20:07 +08:00
|
|
|
// Handle CXX destruction of variables.
|
2009-08-04 04:51:29 +08:00
|
|
|
QualType DtorTy(Ty);
|
2009-11-10 06:08:55 +08:00
|
|
|
while (const ArrayType *Array = getContext().getAsArrayType(DtorTy))
|
2009-10-30 00:22:54 +08:00
|
|
|
DtorTy = getContext().getBaseElementType(Array);
|
2009-08-04 04:51:29 +08:00
|
|
|
if (const RecordType *RT = DtorTy->getAs<RecordType>())
|
2010-05-15 14:46:45 +08:00
|
|
|
if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
2010-05-16 00:39:56 +08:00
|
|
|
if (!ClassDecl->hasTrivialDestructor()) {
|
2010-05-17 23:52:46 +08:00
|
|
|
// Note: We suppress the destructor call when the corresponding NRVO
|
|
|
|
// flag has been set.
|
2010-05-15 14:46:45 +08:00
|
|
|
llvm::Value *Loc = DeclPtr;
|
|
|
|
if (isByRef)
|
|
|
|
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
|
|
D.getNameAsString());
|
|
|
|
|
2010-07-01 22:13:13 +08:00
|
|
|
const CXXDestructorDecl *D = ClassDecl->getDestructor();
|
2009-08-04 04:20:07 +08:00
|
|
|
assert(D && "EmitLocalBlockVarDecl - destructor is nul");
|
2009-10-30 00:22:54 +08:00
|
|
|
|
|
|
|
if (const ConstantArrayType *Array =
|
|
|
|
getContext().getAsConstantArrayType(Ty)) {
|
2010-07-21 15:22:38 +08:00
|
|
|
EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
|
|
|
|
D, Array, Loc);
|
2009-11-05 01:57:40 +08:00
|
|
|
} else {
|
2010-07-21 15:22:38 +08:00
|
|
|
EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
|
|
|
|
D, NRVOFlag, Loc);
|
2009-11-05 01:57:40 +08:00
|
|
|
}
|
2009-08-04 04:20:07 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-08 07:51:38 +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
|
|
|
|
2009-09-11 07:43:36 +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);
|
2010-07-21 15:22:38 +08:00
|
|
|
EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup,
|
|
|
|
F, &Info, DeclPtr, &D);
|
2009-02-08 07:51:38 +08:00
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2010-07-23 05:25:44 +08:00
|
|
|
// If this is a block variable, clean it up.
|
2010-07-21 14:13:08 +08:00
|
|
|
if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly)
|
2010-10-07 02:56:43 +08:00
|
|
|
EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, DeclPtr);
|
2007-06-02 12:16:21 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
|
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.
|
2008-08-16 11:19:19 +08:00
|
|
|
void CodeGenFunction::EmitParmDecl(const VarDecl &D, llvm::Value *Arg) {
|
|
|
|
// 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");
|
2008-04-07 07:10:54 +08:00
|
|
|
QualType Ty = D.getType();
|
2009-11-04 07:25:48 +08:00
|
|
|
CanQualType CTy = getContext().getCanonicalType(Ty);
|
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
|
|
|
|
|
|
|
// Store the initial value into the alloca.
|
2010-08-21 10:24:36 +08:00
|
|
|
unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
|
|
|
|
EmitStoreOfScalar(Arg, DeclPtr, CTy.isVolatileQualified(), Alignment, Ty);
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|
2010-02-09 06:53:07 +08:00
|
|
|
Arg->setName(D.getName());
|
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.
|
2009-10-10 06:06:15 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
|
|
|
DI->setLocation(D.getLocation());
|
2008-11-10 14:08:34 +08:00
|
|
|
DI->EmitDeclareOfArgVariable(&D, DeclPtr, Builder);
|
2009-10-10 06:06:15 +08:00
|
|
|
}
|
2007-06-14 04:44:40 +08:00
|
|
|
}
|