Reorganize the emission of local variables.
llvm-svn: 126189
This commit is contained in:
parent
fb1a79af7a
commit
c533cb7008
|
@ -1376,10 +1376,9 @@ llvm::Constant *CodeGenModule::BuildbyrefCopyHelper(const llvm::Type *T,
|
||||||
BlockFieldFlags flags,
|
BlockFieldFlags flags,
|
||||||
unsigned align,
|
unsigned align,
|
||||||
const VarDecl *var) {
|
const VarDecl *var) {
|
||||||
// All alignments below that of pointer alignment collapse down to just
|
// All alignments below pointer alignment are bumped up, as we
|
||||||
// pointer alignment, as we always have at least that much alignment to begin
|
// always have at least that much alignment to begin with.
|
||||||
// with.
|
if (align < PointerAlignInBytes) align = PointerAlignInBytes;
|
||||||
align /= unsigned(getTarget().getPointerAlign(0) / 8);
|
|
||||||
|
|
||||||
// As an optimization, we only generate a single function of each kind we
|
// As an optimization, we only generate a single function of each kind we
|
||||||
// might need. We need a different one for each alignment and for each
|
// might need. We need a different one for each alignment and for each
|
||||||
|
@ -1396,10 +1395,9 @@ llvm::Constant *CodeGenModule::BuildbyrefDestroyHelper(const llvm::Type *T,
|
||||||
BlockFieldFlags flags,
|
BlockFieldFlags flags,
|
||||||
unsigned align,
|
unsigned align,
|
||||||
const VarDecl *var) {
|
const VarDecl *var) {
|
||||||
// All alignments below that of pointer alignment collpase down to just
|
// All alignments below pointer alignment are bumped up, as we
|
||||||
// pointer alignment, as we always have at least that much alignment to begin
|
// always have at least that much alignment to begin with.
|
||||||
// with.
|
if (align < PointerAlignInBytes) align = PointerAlignInBytes;
|
||||||
align /= unsigned(getTarget().getPointerAlign(0) / 8);
|
|
||||||
|
|
||||||
// As an optimization, we only generate a single function of each kind we
|
// As an optimization, we only generate a single function of each kind we
|
||||||
// might need. We need a different one for each alignment and for each
|
// might need. We need a different one for each alignment and for each
|
||||||
|
|
|
@ -474,14 +474,19 @@ namespace {
|
||||||
struct CallCleanupFunction : EHScopeStack::Cleanup {
|
struct CallCleanupFunction : EHScopeStack::Cleanup {
|
||||||
llvm::Constant *CleanupFn;
|
llvm::Constant *CleanupFn;
|
||||||
const CGFunctionInfo &FnInfo;
|
const CGFunctionInfo &FnInfo;
|
||||||
llvm::Value *Addr;
|
|
||||||
const VarDecl &Var;
|
const VarDecl &Var;
|
||||||
|
|
||||||
CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
|
CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
|
||||||
llvm::Value *Addr, const VarDecl *Var)
|
const VarDecl *Var)
|
||||||
: CleanupFn(CleanupFn), FnInfo(*Info), Addr(Addr), Var(*Var) {}
|
: CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
|
||||||
|
|
||||||
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
void Emit(CodeGenFunction &CGF, bool IsForEH) {
|
||||||
|
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();
|
||||||
|
|
||||||
// In some cases, the type of the function argument will be different from
|
// In some cases, the type of the function argument will be different from
|
||||||
// the type of the pointer. An example of this is
|
// the type of the pointer. An example of this is
|
||||||
// void f(void* arg);
|
// void f(void* arg);
|
||||||
|
@ -543,7 +548,7 @@ static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
|
||||||
/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
|
/// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
|
||||||
/// stores that would be required.
|
/// stores that would be required.
|
||||||
static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
|
static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
|
||||||
CGBuilderTy &Builder) {
|
bool isVolatile, CGBuilderTy &Builder) {
|
||||||
// Zero doesn't require any stores.
|
// Zero doesn't require any stores.
|
||||||
if (isa<llvm::ConstantAggregateZero>(Init) ||
|
if (isa<llvm::ConstantAggregateZero>(Init) ||
|
||||||
isa<llvm::ConstantPointerNull>(Init) ||
|
isa<llvm::ConstantPointerNull>(Init) ||
|
||||||
|
@ -554,7 +559,7 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
|
||||||
isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
|
isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
|
||||||
isa<llvm::ConstantExpr>(Init)) {
|
isa<llvm::ConstantExpr>(Init)) {
|
||||||
if (!Init->isNullValue())
|
if (!Init->isNullValue())
|
||||||
Builder.CreateStore(Init, Loc);
|
Builder.CreateStore(Init, Loc, isVolatile);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -567,7 +572,7 @@ static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
|
||||||
|
|
||||||
// Otherwise, get a pointer to the element and emit it.
|
// Otherwise, get a pointer to the element and emit it.
|
||||||
emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
|
emitStoresForInitAfterMemset(Elt, Builder.CreateConstGEP2_32(Loc, 0, i),
|
||||||
Builder);
|
isVolatile, Builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -597,37 +602,55 @@ static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
|
||||||
/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
/// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
|
||||||
/// variable declaration with auto, register, or no storage class specifier.
|
/// variable declaration with auto, register, or no storage class specifier.
|
||||||
/// These turn into simple stack objects, or GlobalValues depending on target.
|
/// These turn into simple stack objects, or GlobalValues depending on target.
|
||||||
void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) {
|
||||||
SpecialInitFn *SpecialInit) {
|
AutoVarEmission emission = EmitAutoVarAlloca(D);
|
||||||
QualType Ty = D.getType();
|
EmitAutoVarInit(emission);
|
||||||
unsigned Alignment = getContext().getDeclAlign(&D).getQuantity();
|
EmitAutoVarCleanups(emission);
|
||||||
bool isByRef = D.hasAttr<BlocksAttr>();
|
}
|
||||||
bool needsDispose = false;
|
|
||||||
CharUnits Align = CharUnits::Zero();
|
/// EmitAutoVarAlloca - Emit the alloca and debug information for a
|
||||||
bool IsSimpleConstantInitializer = false;
|
/// local variable. Does not emit initalization or destruction.
|
||||||
|
CodeGenFunction::AutoVarEmission
|
||||||
|
CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
|
||||||
|
QualType Ty = D.getType();
|
||||||
|
|
||||||
|
AutoVarEmission emission(D);
|
||||||
|
|
||||||
|
bool isByRef = D.hasAttr<BlocksAttr>();
|
||||||
|
emission.IsByRef = isByRef;
|
||||||
|
|
||||||
|
CharUnits alignment = getContext().getDeclAlign(&D);
|
||||||
|
emission.Alignment = alignment;
|
||||||
|
|
||||||
bool NRVO = false;
|
|
||||||
llvm::Value *NRVOFlag = 0;
|
|
||||||
llvm::Value *DeclPtr;
|
llvm::Value *DeclPtr;
|
||||||
if (Ty->isConstantSizeType()) {
|
if (Ty->isConstantSizeType()) {
|
||||||
if (!Target.useGlobalsForAutomaticVariables()) {
|
if (!Target.useGlobalsForAutomaticVariables()) {
|
||||||
NRVO = getContext().getLangOptions().ElideConstructors &&
|
bool NRVO = getContext().getLangOptions().ElideConstructors &&
|
||||||
D.isNRVOVariable();
|
D.isNRVOVariable();
|
||||||
// If this value is an array or struct, is POD, and if the initializer is
|
|
||||||
// a staticly determinable constant, try to optimize it (unless the NRVO
|
// If this value is a POD array or struct with a statically
|
||||||
// is already optimizing this).
|
// determinable constant initializer, there are optimizations we
|
||||||
if (!NRVO && D.getInit() && !isByRef &&
|
// can do.
|
||||||
(Ty->isArrayType() || Ty->isRecordType()) &&
|
// TODO: we can potentially constant-evaluate non-POD structs and
|
||||||
Ty->isPODType() &&
|
// 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() &&
|
||||||
|
(Ty->isArrayType() || Ty->isRecordType()) && Ty->isPODType() &&
|
||||||
D.getInit()->isConstantInitializer(getContext(), false)) {
|
D.getInit()->isConstantInitializer(getContext(), false)) {
|
||||||
// If this variable is marked 'const', emit the value as a global.
|
|
||||||
if (CGM.getCodeGenOpts().MergeAllConstants &&
|
// If the variable's a const type, and it's neither an NRVO
|
||||||
Ty.isConstant(getContext())) {
|
// candidate nor a __block variable, emit it as a global instead.
|
||||||
|
if (CGM.getCodeGenOpts().MergeAllConstants && Ty.isConstQualified() &&
|
||||||
|
!NRVO && !isByRef) {
|
||||||
EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
|
EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
|
||||||
return;
|
|
||||||
|
emission.Address = 0; // signal this condition to later callbacks
|
||||||
|
assert(emission.wasEmittedAsGlobal());
|
||||||
|
return emission;
|
||||||
}
|
}
|
||||||
|
|
||||||
IsSimpleConstantInitializer = true;
|
// Otherwise, tell the initialization code that we're in this case.
|
||||||
|
emission.IsConstantAggregate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A normal fixed sized variable becomes an alloca in the entry block,
|
// A normal fixed sized variable becomes an alloca in the entry block,
|
||||||
|
@ -646,12 +669,13 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
// to this variable. Set it to zero to indicate that NRVO was not
|
// to this variable. Set it to zero to indicate that NRVO was not
|
||||||
// applied.
|
// applied.
|
||||||
llvm::Value *Zero = Builder.getFalse();
|
llvm::Value *Zero = Builder.getFalse();
|
||||||
NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
|
llvm::Value *NRVOFlag = CreateTempAlloca(Zero->getType(), "nrvo");
|
||||||
EnsureInsertPoint();
|
EnsureInsertPoint();
|
||||||
Builder.CreateStore(Zero, NRVOFlag);
|
Builder.CreateStore(Zero, NRVOFlag);
|
||||||
|
|
||||||
// Record the NRVO flag for this variable.
|
// Record the NRVO flag for this variable.
|
||||||
NRVOFlags[&D] = NRVOFlag;
|
NRVOFlags[&D] = NRVOFlag;
|
||||||
|
emission.NRVOFlag = NRVOFlag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -661,11 +685,11 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
|
||||||
Alloc->setName(D.getNameAsString());
|
Alloc->setName(D.getNameAsString());
|
||||||
|
|
||||||
Align = getContext().getDeclAlign(&D);
|
CharUnits allocaAlignment = alignment;
|
||||||
if (isByRef)
|
if (isByRef)
|
||||||
Align = std::max(Align,
|
allocaAlignment = std::max(allocaAlignment,
|
||||||
getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
|
getContext().toCharUnitsFromBits(Target.getPointerAlign(0)));
|
||||||
Alloc->setAlignment(Align.getQuantity());
|
Alloc->setAlignment(allocaAlignment.getQuantity());
|
||||||
DeclPtr = Alloc;
|
DeclPtr = Alloc;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -707,7 +731,7 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
// Allocate memory for the array.
|
// Allocate memory for the array.
|
||||||
llvm::AllocaInst *VLA =
|
llvm::AllocaInst *VLA =
|
||||||
Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla");
|
Builder.CreateAlloca(llvm::Type::getInt8Ty(getLLVMContext()), VLASize, "vla");
|
||||||
VLA->setAlignment(getContext().getDeclAlign(&D).getQuantity());
|
VLA->setAlignment(alignment.getQuantity());
|
||||||
|
|
||||||
DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
|
DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
|
||||||
}
|
}
|
||||||
|
@ -715,6 +739,7 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
llvm::Value *&DMEntry = LocalDeclMap[&D];
|
||||||
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
|
||||||
DMEntry = DeclPtr;
|
DMEntry = DeclPtr;
|
||||||
|
emission.Address = DeclPtr;
|
||||||
|
|
||||||
// Emit debug info for local var declaration.
|
// Emit debug info for local var declaration.
|
||||||
if (CGDebugInfo *DI = getDebugInfo()) {
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
||||||
|
@ -727,53 +752,90 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
DI->EmitDeclareOfAutoVariable(&D, DeclPtr, Builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
|
||||||
|
// If this was emitted as a global constant, we're done.
|
||||||
|
if (emission.wasEmittedAsGlobal()) return;
|
||||||
|
|
||||||
|
const VarDecl &D = emission.Variable;
|
||||||
|
QualType type = D.getType();
|
||||||
|
|
||||||
// If this local has an initializer, emit it now.
|
// If this local has an initializer, emit it now.
|
||||||
const Expr *Init = D.getInit();
|
const Expr *Init = D.getInit();
|
||||||
|
|
||||||
// If we are at an unreachable point, we don't need to emit the initializer
|
// If we are at an unreachable point, we don't need to emit the initializer
|
||||||
// unless it contains a label.
|
// unless it contains a label.
|
||||||
if (!HaveInsertPoint()) {
|
if (!HaveInsertPoint()) {
|
||||||
if (!ContainsLabel(Init))
|
if (!Init || !ContainsLabel(Init)) return;
|
||||||
Init = 0;
|
|
||||||
else
|
|
||||||
EnsureInsertPoint();
|
EnsureInsertPoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isByRef) {
|
CharUnits alignment = emission.Alignment;
|
||||||
EnsureInsertPoint();
|
|
||||||
|
if (emission.IsByRef) {
|
||||||
llvm::Value *V;
|
llvm::Value *V;
|
||||||
|
|
||||||
BlockFieldFlags fieldFlags;
|
BlockFieldFlags fieldFlags;
|
||||||
bool fieldNeedsCopyDispose = false;
|
bool fieldNeedsCopyDispose = false;
|
||||||
|
|
||||||
needsDispose = true;
|
if (type->isBlockPointerType()) {
|
||||||
|
|
||||||
if (Ty->isBlockPointerType()) {
|
|
||||||
fieldFlags |= BLOCK_FIELD_IS_BLOCK;
|
fieldFlags |= BLOCK_FIELD_IS_BLOCK;
|
||||||
fieldNeedsCopyDispose = true;
|
fieldNeedsCopyDispose = true;
|
||||||
} else if (getContext().isObjCNSObjectType(Ty) ||
|
} else if (getContext().isObjCNSObjectType(type) ||
|
||||||
Ty->isObjCObjectPointerType()) {
|
type->isObjCObjectPointerType()) {
|
||||||
fieldFlags |= BLOCK_FIELD_IS_OBJECT;
|
fieldFlags |= BLOCK_FIELD_IS_OBJECT;
|
||||||
fieldNeedsCopyDispose = true;
|
fieldNeedsCopyDispose = true;
|
||||||
} else if (getLangOptions().CPlusPlus) {
|
} else if (getLangOptions().CPlusPlus) {
|
||||||
if (getContext().getBlockVarCopyInits(&D))
|
if (getContext().getBlockVarCopyInits(&D))
|
||||||
fieldNeedsCopyDispose = true;
|
fieldNeedsCopyDispose = true;
|
||||||
else if (const CXXRecordDecl *record = D.getType()->getAsCXXRecordDecl())
|
else if (const CXXRecordDecl *record = type->getAsCXXRecordDecl())
|
||||||
fieldNeedsCopyDispose = !record->hasTrivialDestructor();
|
fieldNeedsCopyDispose = !record->hasTrivialDestructor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Value *addr = emission.Address;
|
||||||
|
|
||||||
// FIXME: Someone double check this.
|
// FIXME: Someone double check this.
|
||||||
if (Ty.isObjCGCWeak())
|
if (type.isObjCGCWeak())
|
||||||
fieldFlags |= BLOCK_FIELD_IS_WEAK;
|
fieldFlags |= BLOCK_FIELD_IS_WEAK;
|
||||||
|
|
||||||
|
// Initialize the 'isa', which is just 0 or 1.
|
||||||
int isa = 0;
|
int isa = 0;
|
||||||
if (fieldFlags & BLOCK_FIELD_IS_WEAK)
|
if (fieldFlags & BLOCK_FIELD_IS_WEAK)
|
||||||
isa = 1;
|
isa = 1;
|
||||||
V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
|
V = Builder.CreateIntToPtr(Builder.getInt32(isa), Int8PtrTy, "isa");
|
||||||
Builder.CreateStore(V, Builder.CreateStructGEP(DeclPtr, 0, "byref.isa"));
|
Builder.CreateStore(V, Builder.CreateStructGEP(addr, 0, "byref.isa"));
|
||||||
|
|
||||||
Builder.CreateStore(DeclPtr, Builder.CreateStructGEP(DeclPtr, 1,
|
// Store the address of the variable into its own forwarding pointer.
|
||||||
"byref.forwarding"));
|
Builder.CreateStore(addr,
|
||||||
|
Builder.CreateStructGEP(addr, 1, "byref.forwarding"));
|
||||||
|
|
||||||
// Blocks ABI:
|
// Blocks ABI:
|
||||||
// c) the flags field is set to either 0 if no helper functions are
|
// c) the flags field is set to either 0 if no helper functions are
|
||||||
|
@ -781,44 +843,51 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
BlockFlags flags;
|
BlockFlags flags;
|
||||||
if (fieldNeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
|
if (fieldNeedsCopyDispose) flags |= BLOCK_HAS_COPY_DISPOSE;
|
||||||
Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
|
Builder.CreateStore(llvm::ConstantInt::get(IntTy, flags.getBitMask()),
|
||||||
Builder.CreateStructGEP(DeclPtr, 2, "byref.flags"));
|
Builder.CreateStructGEP(addr, 2, "byref.flags"));
|
||||||
|
|
||||||
const llvm::Type *V1;
|
const llvm::Type *V1;
|
||||||
V1 = cast<llvm::PointerType>(DeclPtr->getType())->getElementType();
|
V1 = cast<llvm::PointerType>(addr->getType())->getElementType();
|
||||||
V = llvm::ConstantInt::get(IntTy, CGM.GetTargetTypeStoreSize(V1).getQuantity());
|
V = llvm::ConstantInt::get(IntTy, CGM.GetTargetTypeStoreSize(V1).getQuantity());
|
||||||
Builder.CreateStore(V, Builder.CreateStructGEP(DeclPtr, 3, "byref.size"));
|
Builder.CreateStore(V, Builder.CreateStructGEP(addr, 3, "byref.size"));
|
||||||
|
|
||||||
if (fieldNeedsCopyDispose) {
|
if (fieldNeedsCopyDispose) {
|
||||||
llvm::Value *copy_helper = Builder.CreateStructGEP(DeclPtr, 4);
|
llvm::Value *copy_helper = Builder.CreateStructGEP(addr, 4);
|
||||||
Builder.CreateStore(CGM.BuildbyrefCopyHelper(DeclPtr->getType(),
|
Builder.CreateStore(CGM.BuildbyrefCopyHelper(addr->getType(), fieldFlags,
|
||||||
fieldFlags,
|
alignment.getQuantity(), &D),
|
||||||
Align.getQuantity(), &D),
|
|
||||||
copy_helper);
|
copy_helper);
|
||||||
|
|
||||||
llvm::Value *destroy_helper = Builder.CreateStructGEP(DeclPtr, 5);
|
llvm::Value *destroy_helper = Builder.CreateStructGEP(addr, 5);
|
||||||
Builder.CreateStore(CGM.BuildbyrefDestroyHelper(DeclPtr->getType(),
|
Builder.CreateStore(CGM.BuildbyrefDestroyHelper(addr->getType(),
|
||||||
fieldFlags,
|
fieldFlags,
|
||||||
Align.getQuantity(), &D),
|
alignment.getQuantity(),
|
||||||
|
&D),
|
||||||
destroy_helper);
|
destroy_helper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SpecialInit) {
|
if (!Init) return;
|
||||||
SpecialInit(*this, D, DeclPtr);
|
|
||||||
} else if (Init) {
|
|
||||||
llvm::Value *Loc = DeclPtr;
|
|
||||||
|
|
||||||
bool isVolatile = getContext().getCanonicalType(Ty).isVolatileQualified();
|
// 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);
|
||||||
|
|
||||||
// If the initializer was a simple constant initializer, we can optimize it
|
llvm::Value *Loc =
|
||||||
|
capturedByInit ? emission.Address : emission.getObjectAddress(*this);
|
||||||
|
|
||||||
|
bool isVolatile = type.isVolatileQualified();
|
||||||
|
|
||||||
|
// If this is a simple aggregate initialization, we can optimize it
|
||||||
// in various ways.
|
// in various ways.
|
||||||
if (IsSimpleConstantInitializer) {
|
if (emission.IsConstantAggregate) {
|
||||||
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), Ty,this);
|
assert(!capturedByInit && "constant init contains a capturing block?");
|
||||||
|
|
||||||
|
llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), type, this);
|
||||||
assert(Init != 0 && "Wasn't a simple constant init?");
|
assert(Init != 0 && "Wasn't a simple constant init?");
|
||||||
|
|
||||||
llvm::Value *SizeVal =
|
llvm::Value *SizeVal =
|
||||||
llvm::ConstantInt::get(IntPtrTy,
|
llvm::ConstantInt::get(IntPtrTy,
|
||||||
getContext().getTypeSizeInChars(Ty).getQuantity());
|
getContext().getTypeSizeInChars(type).getQuantity());
|
||||||
|
|
||||||
const llvm::Type *BP = Int8PtrTy;
|
const llvm::Type *BP = Int8PtrTy;
|
||||||
if (Loc->getType() != BP)
|
if (Loc->getType() != BP)
|
||||||
|
@ -828,13 +897,12 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
// a few stores afterward.
|
// a few stores afterward.
|
||||||
if (shouldUseMemSetPlusStoresToInitialize(Init,
|
if (shouldUseMemSetPlusStoresToInitialize(Init,
|
||||||
CGM.getTargetData().getTypeAllocSize(Init->getType()))) {
|
CGM.getTargetData().getTypeAllocSize(Init->getType()))) {
|
||||||
Builder.CreateMemSet(Loc, Builder.getInt8(0), SizeVal,
|
Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
|
||||||
Align.getQuantity(), false);
|
alignment.getQuantity(), isVolatile);
|
||||||
if (!Init->isNullValue()) {
|
if (!Init->isNullValue()) {
|
||||||
Loc = Builder.CreateBitCast(Loc, Init->getType()->getPointerTo());
|
Loc = Builder.CreateBitCast(Loc, Init->getType()->getPointerTo());
|
||||||
emitStoresForInitAfterMemset(Init, Loc, Builder);
|
emitStoresForInitAfterMemset(Init, Loc, isVolatile, Builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, create a temporary global with the initializer then
|
// Otherwise, create a temporary global with the initializer then
|
||||||
// memcpy from the global to the alloca.
|
// memcpy from the global to the alloca.
|
||||||
|
@ -843,74 +911,72 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
new llvm::GlobalVariable(CGM.getModule(), Init->getType(), true,
|
new llvm::GlobalVariable(CGM.getModule(), Init->getType(), true,
|
||||||
llvm::GlobalValue::InternalLinkage,
|
llvm::GlobalValue::InternalLinkage,
|
||||||
Init, Name, 0, false, 0);
|
Init, Name, 0, false, 0);
|
||||||
GV->setAlignment(Align.getQuantity());
|
GV->setAlignment(alignment.getQuantity());
|
||||||
|
|
||||||
llvm::Value *SrcPtr = GV;
|
llvm::Value *SrcPtr = GV;
|
||||||
if (SrcPtr->getType() != BP)
|
if (SrcPtr->getType() != BP)
|
||||||
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
||||||
|
|
||||||
Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, Align.getQuantity(), false);
|
Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, alignment.getQuantity(),
|
||||||
|
isVolatile);
|
||||||
}
|
}
|
||||||
} else if (Ty->isReferenceType()) {
|
} else if (type->isReferenceType()) {
|
||||||
RValue RV = EmitReferenceBindingToExpr(Init, &D);
|
RValue RV = EmitReferenceBindingToExpr(Init, &D);
|
||||||
if (isByRef)
|
if (capturedByInit) Loc = BuildBlockByrefAddress(Loc, &D);
|
||||||
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
EmitStoreOfScalar(RV.getScalarVal(), Loc, false, alignment.getQuantity(),
|
||||||
D.getNameAsString());
|
type);
|
||||||
EmitStoreOfScalar(RV.getScalarVal(), Loc, false, Alignment, Ty);
|
} else if (!hasAggregateLLVMType(type)) {
|
||||||
} else if (!hasAggregateLLVMType(Init->getType())) {
|
|
||||||
llvm::Value *V = EmitScalarExpr(Init);
|
llvm::Value *V = EmitScalarExpr(Init);
|
||||||
if (isByRef) {
|
if (capturedByInit) Loc = BuildBlockByrefAddress(Loc, &D);
|
||||||
// When RHS has side-effect, must go through "forwarding' field
|
EmitStoreOfScalar(V, Loc, isVolatile, alignment.getQuantity(), type);
|
||||||
// to get to the address of the __block variable descriptor.
|
} else if (type->isAnyComplexType()) {
|
||||||
if (Init->HasSideEffects(getContext()))
|
ComplexPairTy complex = EmitComplexExpr(Init);
|
||||||
Loc = BuildBlockByrefAddress(DeclPtr, &D);
|
if (capturedByInit) Loc = BuildBlockByrefAddress(Loc, &D);
|
||||||
else
|
StoreComplexToAddr(complex, Loc, isVolatile);
|
||||||
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
||||||
D.getNameAsString());
|
|
||||||
}
|
|
||||||
EmitStoreOfScalar(V, Loc, isVolatile, Alignment, Ty);
|
|
||||||
} else if (Init->getType()->isAnyComplexType()) {
|
|
||||||
if (isByRef)
|
|
||||||
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
||||||
D.getNameAsString());
|
|
||||||
EmitComplexExprIntoAddr(Init, Loc, isVolatile);
|
|
||||||
} else {
|
} else {
|
||||||
if (isByRef)
|
// TODO: how can we delay here if D is captured by its initializer?
|
||||||
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
|
||||||
D.getNameAsString());
|
|
||||||
EmitAggExpr(Init, AggValueSlot::forAddr(Loc, isVolatile, true, false));
|
EmitAggExpr(Init, AggValueSlot::forAddr(Loc, isVolatile, true, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle CXX destruction of variables.
|
void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
|
||||||
QualType DtorTy(Ty);
|
// If this was emitted as a global constant, we're done.
|
||||||
while (const ArrayType *Array = getContext().getAsArrayType(DtorTy))
|
if (emission.wasEmittedAsGlobal()) return;
|
||||||
DtorTy = getContext().getBaseElementType(Array);
|
|
||||||
if (const RecordType *RT = DtorTy->getAs<RecordType>())
|
const VarDecl &D = emission.Variable;
|
||||||
if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
||||||
|
// Handle C++ destruction of variables.
|
||||||
|
if (getLangOptions().CPlusPlus) {
|
||||||
|
QualType type = D.getType();
|
||||||
|
QualType baseType = getContext().getBaseElementType(type);
|
||||||
|
if (const RecordType *RT = baseType->getAs<RecordType>()) {
|
||||||
|
CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
|
||||||
if (!ClassDecl->hasTrivialDestructor()) {
|
if (!ClassDecl->hasTrivialDestructor()) {
|
||||||
// Note: We suppress the destructor call when the corresponding NRVO
|
// Note: We suppress the destructor call when the corresponding NRVO
|
||||||
// flag has been set.
|
// flag has been set.
|
||||||
llvm::Value *Loc = DeclPtr;
|
|
||||||
if (isByRef)
|
// Note that for __block variables, we want to destroy the
|
||||||
Loc = Builder.CreateStructGEP(DeclPtr, getByRefValueLLVMField(&D),
|
// original stack object, not the possible forwarded object.
|
||||||
D.getNameAsString());
|
llvm::Value *Loc = emission.getObjectAddress(*this);
|
||||||
|
|
||||||
const CXXDestructorDecl *D = ClassDecl->getDestructor();
|
const CXXDestructorDecl *D = ClassDecl->getDestructor();
|
||||||
assert(D && "EmitLocalBlockVarDecl - destructor is nul");
|
assert(D && "EmitLocalBlockVarDecl - destructor is nul");
|
||||||
|
|
||||||
if (const ConstantArrayType *Array =
|
if (type != baseType) {
|
||||||
getContext().getAsConstantArrayType(Ty)) {
|
const ConstantArrayType *Array =
|
||||||
|
getContext().getAsConstantArrayType(type);
|
||||||
|
assert(Array && "types changed without array?");
|
||||||
EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
|
EHStack.pushCleanup<CallArrayDtor>(NormalAndEHCleanup,
|
||||||
D, Array, Loc);
|
D, Array, Loc);
|
||||||
} else {
|
} else {
|
||||||
EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
|
EHStack.pushCleanup<CallVarDtor>(NormalAndEHCleanup,
|
||||||
D, NRVOFlag, Loc);
|
D, emission.NRVOFlag, Loc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the cleanup attribute
|
// Handle the cleanup attribute.
|
||||||
if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
|
if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
|
||||||
const FunctionDecl *FD = CA->getFunctionDecl();
|
const FunctionDecl *FD = CA->getFunctionDecl();
|
||||||
|
|
||||||
|
@ -918,13 +984,14 @@ void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D,
|
||||||
assert(F && "Could not find function!");
|
assert(F && "Could not find function!");
|
||||||
|
|
||||||
const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
|
const CGFunctionInfo &Info = CGM.getTypes().getFunctionInfo(FD);
|
||||||
EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup,
|
EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
|
||||||
F, &Info, DeclPtr, &D);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is a block variable, clean it up.
|
// If this is a block variable, call _Block_object_destroy
|
||||||
if (needsDispose && CGM.getLangOptions().getGCMode() != LangOptions::GCOnly)
|
// (on the unforwarded address).
|
||||||
EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, DeclPtr);
|
if (emission.IsByRef &&
|
||||||
|
CGM.getLangOptions().getGCMode() != LangOptions::GCOnly)
|
||||||
|
EHStack.pushCleanup<CallBlockRelease>(NormalAndEHCleanup, emission.Address);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Emit an alloca (or GlobalValue depending on target)
|
/// Emit an alloca (or GlobalValue depending on target)
|
||||||
|
|
|
@ -1086,14 +1086,14 @@ static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
|
||||||
// 3. Enter __cxa_end_catch cleanup
|
// 3. Enter __cxa_end_catch cleanup
|
||||||
// 4. Enter dtor cleanup
|
// 4. Enter dtor cleanup
|
||||||
//
|
//
|
||||||
// We do this by initializing the exception variable with a
|
// We do this by using a slightly abnormal initialization process.
|
||||||
// "special initializer", InitCatchParam. Delegation sequence:
|
// Delegation sequence:
|
||||||
// - ExitCXXTryStmt opens a RunCleanupsScope
|
// - ExitCXXTryStmt opens a RunCleanupsScope
|
||||||
// - EmitLocalBlockVarDecl creates the variable and debug info
|
// - EmitAutoVarAlloca creates the variable and debug info
|
||||||
// - InitCatchParam initializes the variable from the exception
|
// - InitCatchParam initializes the variable from the exception
|
||||||
// - CallBeginCatch calls __cxa_begin_catch
|
// - CallBeginCatch calls __cxa_begin_catch
|
||||||
// - CallBeginCatch enters the __cxa_end_catch cleanup
|
// - CallBeginCatch enters the __cxa_end_catch cleanup
|
||||||
// - EmitLocalBlockVarDecl enters the variable destructor cleanup
|
// - EmitAutoVarCleanups enters the variable destructor cleanup
|
||||||
// - EmitCXXTryStmt emits the code for the catch body
|
// - EmitCXXTryStmt emits the code for the catch body
|
||||||
// - EmitCXXTryStmt close the RunCleanupsScope
|
// - EmitCXXTryStmt close the RunCleanupsScope
|
||||||
|
|
||||||
|
@ -1105,7 +1105,9 @@ static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emit the local.
|
// Emit the local.
|
||||||
CGF.EmitAutoVarDecl(*CatchParam, &InitCatchParam);
|
CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
|
||||||
|
InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF));
|
||||||
|
CGF.EmitAutoVarCleanups(var);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
|
@ -1555,7 +1555,50 @@ public:
|
||||||
/// EmitAutoVarDecl - Emit an auto variable declaration.
|
/// EmitAutoVarDecl - Emit an auto variable declaration.
|
||||||
///
|
///
|
||||||
/// This function can be called with a null (unreachable) insert point.
|
/// This function can be called with a null (unreachable) insert point.
|
||||||
void EmitAutoVarDecl(const VarDecl &D, SpecialInitFn *SpecialInit = 0);
|
void EmitAutoVarDecl(const VarDecl &D);
|
||||||
|
|
||||||
|
class AutoVarEmission {
|
||||||
|
friend class CodeGenFunction;
|
||||||
|
|
||||||
|
const VarDecl &Variable;
|
||||||
|
|
||||||
|
/// The alignment of the variable.
|
||||||
|
CharUnits Alignment;
|
||||||
|
|
||||||
|
/// The address of the alloca. Null if the variable was emitted
|
||||||
|
/// as a global constant.
|
||||||
|
llvm::Value *Address;
|
||||||
|
|
||||||
|
llvm::Value *NRVOFlag;
|
||||||
|
|
||||||
|
/// True if the variable is a __block variable.
|
||||||
|
bool IsByRef;
|
||||||
|
|
||||||
|
/// True if the variable is of aggregate type and has a constant
|
||||||
|
/// initializer.
|
||||||
|
bool IsConstantAggregate;
|
||||||
|
|
||||||
|
AutoVarEmission(const VarDecl &variable)
|
||||||
|
: Variable(variable), Address(0), NRVOFlag(0),
|
||||||
|
IsByRef(false), IsConstantAggregate(false) {}
|
||||||
|
|
||||||
|
bool wasEmittedAsGlobal() const { return Address == 0; }
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// Returns the address of the object within this declaration.
|
||||||
|
/// Note that this does not chase the forwarding pointer for
|
||||||
|
/// __block decls.
|
||||||
|
llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
|
||||||
|
if (!IsByRef) return Address;
|
||||||
|
|
||||||
|
return CGF.Builder.CreateStructGEP(Address,
|
||||||
|
CGF.getByRefValueLLVMField(&Variable),
|
||||||
|
Variable.getNameAsString());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
|
||||||
|
void EmitAutoVarInit(const AutoVarEmission &emission);
|
||||||
|
void EmitAutoVarCleanups(const AutoVarEmission &emission);
|
||||||
|
|
||||||
void EmitStaticVarDecl(const VarDecl &D,
|
void EmitStaticVarDecl(const VarDecl &D,
|
||||||
llvm::GlobalValue::LinkageTypes Linkage);
|
llvm::GlobalValue::LinkageTypes Linkage);
|
||||||
|
|
|
@ -97,6 +97,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
|
||||||
Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
|
Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
|
||||||
Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
|
Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
|
||||||
PointerWidthInBits = C.Target.getPointerWidth(0);
|
PointerWidthInBits = C.Target.getPointerWidth(0);
|
||||||
|
PointerAlignInBytes =
|
||||||
|
C.toCharUnitsFromBits(C.Target.getPointerAlign(0)).getQuantity();
|
||||||
IntTy = llvm::IntegerType::get(LLVMContext, C.Target.getIntWidth());
|
IntTy = llvm::IntegerType::get(LLVMContext, C.Target.getIntWidth());
|
||||||
IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
|
IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
|
||||||
Int8PtrTy = Int8Ty->getPointerTo(0);
|
Int8PtrTy = Int8Ty->getPointerTo(0);
|
||||||
|
|
|
@ -120,8 +120,11 @@ namespace CodeGen {
|
||||||
const llvm::PointerType *Int8PtrPtrTy;
|
const llvm::PointerType *Int8PtrPtrTy;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The width of an address-zero pointer.
|
/// The width of a pointer into the generic address space.
|
||||||
unsigned char PointerWidthInBits;
|
unsigned char PointerWidthInBits;
|
||||||
|
|
||||||
|
/// The alignment of a pointer into the generic address space.
|
||||||
|
unsigned char PointerAlignInBytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// CodeGenModule - This class organizes the cross-function state that is used
|
/// CodeGenModule - This class organizes the cross-function state that is used
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -emit-llvm -fblocks -o %t %s
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin9 -emit-llvm -fblocks -o %t %s
|
||||||
|
|
||||||
|
// 1x for the declaration
|
||||||
|
// 1x for the object-pointer byref copy helper
|
||||||
|
// 1x for the block-pointer byref copy helper
|
||||||
|
// 8x for the block copy helper
|
||||||
// RUN: grep 'object_assign' %t | count 11
|
// RUN: grep 'object_assign' %t | count 11
|
||||||
|
|
||||||
// RUN: grep 'object_dispose' %t | count 29
|
// RUN: grep 'object_dispose' %t | count 29
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
@ -72,8 +72,8 @@ void test2(Test2 *x) {
|
||||||
// CHECK-NEXT: store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_{{.*}} to i8*), i8** [[T5]]
|
// CHECK-NEXT: store i8* bitcast (void (i8*)* @__Block_byref_object_dispose_{{.*}} to i8*), i8** [[T5]]
|
||||||
|
|
||||||
// Actually capture the value.
|
// Actually capture the value.
|
||||||
// CHECK-NEXT: [[CAPTURE:%.*]] = load [[TEST2]]** [[X]]
|
|
||||||
// CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds [[WEAK_T]]* [[WEAKX]], i32 0, i32 6
|
// CHECK-NEXT: [[T6:%.*]] = getelementptr inbounds [[WEAK_T]]* [[WEAKX]], i32 0, i32 6
|
||||||
|
// CHECK-NEXT: [[CAPTURE:%.*]] = load [[TEST2]]** [[X]]
|
||||||
// CHECK-NEXT: store [[TEST2]]* [[CAPTURE]], [[TEST2]]** [[T6]]
|
// CHECK-NEXT: store [[TEST2]]* [[CAPTURE]], [[TEST2]]** [[T6]]
|
||||||
|
|
||||||
// Then we initialize the block, blah blah blah.
|
// Then we initialize the block, blah blah blah.
|
||||||
|
|
Loading…
Reference in New Issue