2010-10-05 05:15:33 +08:00
|
|
|
//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===//
|
2008-08-23 00:00:37 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// We might split this into multiple files if it gets too unwieldy
|
2008-08-23 00:00:37 +08:00
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2010-05-26 03:52:27 +08:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-04 06:50:24 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-24 03:42:54 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-09-28 02:58:34 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2016-04-08 01:49:44 +08:00
|
|
|
#include "clang/Basic/CodeGenOptions.h"
|
2008-08-23 00:00:37 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
/// Try to emit a base destructor as an alias to its primary
|
|
|
|
/// base-class destructor.
|
|
|
|
bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
|
2010-02-19 09:32:20 +08:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-11-14 07:20:45 +08:00
|
|
|
// Producing an alias to a base class ctor/dtor can degrade debug quality
|
2013-12-06 00:25:25 +08:00
|
|
|
// as the debugger cannot tell them apart.
|
2013-11-14 07:20:45 +08:00
|
|
|
if (getCodeGenOpts().OptimizationLevel == 0)
|
|
|
|
return true;
|
|
|
|
|
2015-09-04 07:02:30 +08:00
|
|
|
// If sanitizing memory to check for use-after-dtor, do not emit as
|
|
|
|
// an alias, unless this class owns no members.
|
|
|
|
if (getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
|
|
|
|
!D->getParent()->field_empty())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// If the destructor doesn't have a trivial body, we have to emit it
|
|
|
|
// separately.
|
2011-05-15 07:26:09 +08:00
|
|
|
if (!D->hasTrivialBody())
|
2010-02-23 08:48:20 +08:00
|
|
|
return true;
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
const CXXRecordDecl *Class = D->getParent();
|
|
|
|
|
2014-11-01 03:01:02 +08:00
|
|
|
// We are going to instrument this destructor, so give up even if it is
|
|
|
|
// currently empty.
|
|
|
|
if (Class->mayInsertExtraPadding())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// If we need to manipulate a VTT parameter, give up.
|
|
|
|
if (Class->getNumVBases()) {
|
|
|
|
// Extra Credit: passing extra parameters is perfectly safe
|
|
|
|
// in many calling conventions, so only bail out if the ctor's
|
|
|
|
// calling convention is nonstandard.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-13 08:46:43 +08:00
|
|
|
// If any field has a non-trivial destructor, we have to emit the
|
|
|
|
// destructor separately.
|
2014-03-09 04:12:42 +08:00
|
|
|
for (const auto *I : Class->fields())
|
2012-04-30 10:36:29 +08:00
|
|
|
if (I->getType().isDestructedType())
|
2011-02-13 08:46:43 +08:00
|
|
|
return true;
|
2010-02-23 08:48:20 +08:00
|
|
|
|
|
|
|
// Try to find a unique base class with a non-trivial destructor.
|
2014-05-21 13:09:00 +08:00
|
|
|
const CXXRecordDecl *UniqueBase = nullptr;
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &I : Class->bases()) {
|
2010-02-23 08:48:20 +08:00
|
|
|
|
|
|
|
// We're in the base destructor, so skip virtual bases.
|
2014-03-13 23:41:46 +08:00
|
|
|
if (I.isVirtual()) continue;
|
2010-02-23 08:48:20 +08:00
|
|
|
|
|
|
|
// Skip base classes with trivial destructors.
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *Base =
|
|
|
|
cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
|
2010-02-23 08:48:20 +08:00
|
|
|
if (Base->hasTrivialDestructor()) continue;
|
|
|
|
|
|
|
|
// If we've already found a base class with a non-trivial
|
|
|
|
// destructor, give up.
|
|
|
|
if (UniqueBase) return true;
|
|
|
|
UniqueBase = Base;
|
2010-02-19 09:32:20 +08:00
|
|
|
}
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// If we didn't find any bases with a non-trivial destructor, then
|
|
|
|
// the base destructor is actually effectively trivial, which can
|
|
|
|
// happen if it was needlessly user-defined or if there are virtual
|
|
|
|
// bases with non-trivial destructors.
|
|
|
|
if (!UniqueBase)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the base is at a non-zero offset, give up.
|
|
|
|
const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class);
|
2012-07-05 02:45:14 +08:00
|
|
|
if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
|
2010-02-23 08:48:20 +08:00
|
|
|
return true;
|
|
|
|
|
2014-03-06 05:04:41 +08:00
|
|
|
// Give up if the calling conventions don't match. We could update the call,
|
|
|
|
// but it is probably not worth it.
|
2013-11-09 09:57:21 +08:00
|
|
|
const CXXDestructorDecl *BaseD = UniqueBase->getDestructor();
|
2014-03-06 05:04:41 +08:00
|
|
|
if (BaseD->getType()->getAs<FunctionType>()->getCallConv() !=
|
|
|
|
D->getType()->getAs<FunctionType>()->getCallConv())
|
|
|
|
return true;
|
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base),
|
2013-11-05 02:38:59 +08:00
|
|
|
GlobalDecl(BaseD, Dtor_Base),
|
|
|
|
false);
|
2010-02-23 08:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to emit a definition as a global alias for another definition.
|
2013-11-05 02:38:59 +08:00
|
|
|
/// If \p InEveryTU is true, we know that an equivalent alias can be produced
|
|
|
|
/// in every translation unit.
|
2010-02-23 08:48:20 +08:00
|
|
|
bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl,
|
2013-11-05 02:38:59 +08:00
|
|
|
GlobalDecl TargetDecl,
|
|
|
|
bool InEveryTU) {
|
2010-02-23 08:48:20 +08:00
|
|
|
if (!getCodeGenOpts().CXXCtorDtorAliases)
|
|
|
|
return true;
|
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// The alias will use the linkage of the referent. If we can't
|
2010-02-19 09:32:20 +08:00
|
|
|
// support aliases with that linkage, fail.
|
2013-06-06 01:49:37 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage = getFunctionLinkage(AliasDecl);
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2013-11-05 02:38:59 +08:00
|
|
|
// We can't use an alias if the linkage is not valid for one.
|
|
|
|
if (!llvm::GlobalAlias::isValidLinkage(Linkage))
|
2010-03-05 09:21:10 +08:00
|
|
|
return true;
|
|
|
|
|
2013-11-06 05:37:29 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes TargetLinkage =
|
|
|
|
getFunctionLinkage(TargetDecl);
|
|
|
|
|
2013-11-05 02:38:59 +08:00
|
|
|
// Check if we have it already.
|
|
|
|
StringRef MangledName = getMangledName(AliasDecl);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration())
|
|
|
|
return false;
|
2013-11-06 05:37:29 +08:00
|
|
|
if (Replacements.count(MangledName))
|
|
|
|
return false;
|
2013-11-05 02:38:59 +08:00
|
|
|
|
2010-02-23 08:48:20 +08:00
|
|
|
// Derive the type for the alias.
|
2015-09-15 02:38:22 +08:00
|
|
|
llvm::Type *AliasValueType = getTypes().GetFunctionType(AliasDecl);
|
|
|
|
llvm::PointerType *AliasType = AliasValueType->getPointerTo();
|
2010-02-23 08:48:20 +08:00
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// Find the referent. Some aliases might require a bitcast, in
|
2010-02-23 08:48:20 +08:00
|
|
|
// which case the caller is responsible for ensuring the soundness
|
|
|
|
// of these semantics.
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl));
|
2014-06-03 10:42:01 +08:00
|
|
|
llvm::Constant *Aliasee = Ref;
|
|
|
|
if (Ref->getType() != AliasType)
|
|
|
|
Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType);
|
2010-02-23 08:48:20 +08:00
|
|
|
|
2013-11-09 06:59:46 +08:00
|
|
|
// Instead of creating as alias to a linkonce_odr, replace all of the uses
|
2014-05-17 03:35:48 +08:00
|
|
|
// of the aliasee.
|
2015-09-15 05:35:16 +08:00
|
|
|
if (llvm::GlobalValue::isDiscardableIfUnused(Linkage) &&
|
|
|
|
(TargetLinkage != llvm::GlobalValue::AvailableExternallyLinkage ||
|
|
|
|
!TargetDecl.getDecl()->hasAttr<AlwaysInlineAttr>())) {
|
|
|
|
// FIXME: An extern template instantiation will create functions with
|
|
|
|
// linkage "AvailableExternally". In libc++, some classes also define
|
|
|
|
// members with attribute "AlwaysInline" and expect no reference to
|
|
|
|
// be generated. It is desirable to reenable this optimisation after
|
|
|
|
// corresponding LLVM changes.
|
2016-02-07 20:44:35 +08:00
|
|
|
addReplacement(MangledName, Aliasee);
|
2013-11-09 06:59:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-11 06:23:58 +08:00
|
|
|
// If we have a weak, non-discardable alias (weak, weak_odr), like an extern
|
|
|
|
// template instantiation or a dllexported class, avoid forming it on COFF.
|
|
|
|
// A COFF weak external alias cannot satisfy a normal undefined symbol
|
|
|
|
// reference from another TU. The other TU must also mark the referenced
|
|
|
|
// symbol as weak, which we cannot rely on.
|
|
|
|
if (llvm::GlobalValue::isWeakForLinker(Linkage) &&
|
|
|
|
getTriple().isOSBinFormatCOFF()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:57:21 +08:00
|
|
|
if (!InEveryTU) {
|
2015-01-13 05:22:27 +08:00
|
|
|
// If we don't have a definition for the destructor yet, don't
|
|
|
|
// emit. We can't emit aliases to declarations; that's just not
|
|
|
|
// how aliases work.
|
2014-06-03 10:42:01 +08:00
|
|
|
if (Ref->isDeclaration())
|
2013-11-06 05:37:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-13 06:06:46 +08:00
|
|
|
// Don't create an alias to a linker weak symbol. This avoids producing
|
|
|
|
// different COMDATs in different TUs. Another option would be to
|
|
|
|
// output the alias both for weak_odr and linkonce_odr, but that
|
|
|
|
// requires explicit comdat support in the IL.
|
|
|
|
if (llvm::GlobalValue::isWeakForLinker(TargetLinkage))
|
|
|
|
return true;
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
// Create the alias with no name.
|
2015-09-15 02:38:22 +08:00
|
|
|
auto *Alias = llvm::GlobalAlias::create(AliasValueType, 0, Linkage, "",
|
|
|
|
Aliasee, &getModule());
|
2010-02-19 09:32:20 +08:00
|
|
|
|
2010-02-25 04:32:01 +08:00
|
|
|
// Switch any previous uses to the alias.
|
2010-02-19 09:32:20 +08:00
|
|
|
if (Entry) {
|
2010-02-25 04:32:01 +08:00
|
|
|
assert(Entry->getType() == AliasType &&
|
|
|
|
"declaration exists with different type");
|
2010-03-20 07:29:14 +08:00
|
|
|
Alias->takeName(Entry);
|
2010-02-19 09:32:20 +08:00
|
|
|
Entry->replaceAllUsesWith(Alias);
|
|
|
|
Entry->eraseFromParent();
|
2010-03-20 07:29:14 +08:00
|
|
|
} else {
|
2010-06-23 00:16:50 +08:00
|
|
|
Alias->setName(MangledName);
|
2010-02-19 09:32:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, set up the alias with its proper name and attributes.
|
2014-10-07 21:34:42 +08:00
|
|
|
setAliasAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias);
|
2010-02-19 09:32:20 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-12 07:37:08 +08:00
|
|
|
|
2014-09-16 03:34:18 +08:00
|
|
|
llvm::Function *CodeGenModule::codegenCXXStructor(const CXXMethodDecl *MD,
|
|
|
|
StructorType Type) {
|
|
|
|
const CGFunctionInfo &FnInfo =
|
|
|
|
getTypes().arrangeCXXStructorDeclaration(MD, Type);
|
|
|
|
auto *Fn = cast<llvm::Function>(
|
2015-08-31 21:20:44 +08:00
|
|
|
getAddrOfCXXStructor(MD, Type, &FnInfo, /*FnType=*/nullptr,
|
|
|
|
/*DontDefer=*/true, /*IsForDefinition=*/true));
|
2014-09-16 03:34:18 +08:00
|
|
|
|
|
|
|
GlobalDecl GD;
|
|
|
|
if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
GD = GlobalDecl(DD, toCXXDtorType(Type));
|
|
|
|
} else {
|
|
|
|
const auto *CD = cast<CXXConstructorDecl>(MD);
|
|
|
|
GD = GlobalDecl(CD, toCXXCtorType(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
setFunctionLinkage(GD, Fn);
|
2015-05-29 01:44:56 +08:00
|
|
|
setFunctionDLLStorageClass(GD, Fn);
|
|
|
|
|
2014-09-16 03:34:18 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(GD, Fn, FnInfo);
|
|
|
|
setFunctionDefinitionAttributes(MD, Fn);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(MD, Fn);
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
llvm::Constant *CodeGenModule::getAddrOfCXXStructor(
|
2014-09-09 00:01:27 +08:00
|
|
|
const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo,
|
2015-08-31 21:20:44 +08:00
|
|
|
llvm::FunctionType *FnType, bool DontDefer, bool IsForDefinition) {
|
2014-09-09 00:01:27 +08:00
|
|
|
GlobalDecl GD;
|
|
|
|
if (auto *CD = dyn_cast<CXXConstructorDecl>(MD)) {
|
|
|
|
GD = GlobalDecl(CD, toCXXCtorType(Type));
|
|
|
|
} else {
|
2015-05-07 06:18:39 +08:00
|
|
|
GD = GlobalDecl(cast<CXXDestructorDecl>(MD), toCXXDtorType(Type));
|
2014-09-09 00:01:27 +08:00
|
|
|
}
|
2011-03-09 16:12:35 +08:00
|
|
|
|
2014-09-09 00:01:27 +08:00
|
|
|
if (!FnType) {
|
|
|
|
if (!FnInfo)
|
|
|
|
FnInfo = &getTypes().arrangeCXXStructorDeclaration(MD, Type);
|
|
|
|
FnType = getTypes().GetFunctionType(*FnInfo);
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
return GetOrCreateLLVMFunction(
|
|
|
|
getMangledName(GD), FnType, GD, /*ForVTable=*/false, DontDefer,
|
|
|
|
/*isThunk=*/false, /*ExtraAttrs=*/llvm::AttributeSet(), IsForDefinition);
|
2009-04-17 07:57:24 +08:00
|
|
|
}
|
2009-04-17 09:58:57 +08:00
|
|
|
|
2013-07-19 16:14:45 +08:00
|
|
|
static llvm::Value *BuildAppleKextVirtualCall(CodeGenFunction &CGF,
|
|
|
|
GlobalDecl GD,
|
|
|
|
llvm::Type *Ty,
|
|
|
|
const CXXRecordDecl *RD) {
|
2013-08-21 14:25:03 +08:00
|
|
|
assert(!CGF.CGM.getTarget().getCXXABI().isMicrosoft() &&
|
|
|
|
"No kext in Microsoft ABI");
|
2013-07-19 16:14:45 +08:00
|
|
|
GD = GD.getCanonicalDecl();
|
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
2013-09-27 22:48:01 +08:00
|
|
|
llvm::Value *VTable = CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
|
2013-07-19 16:14:45 +08:00
|
|
|
Ty = Ty->getPointerTo()->getPointerTo();
|
|
|
|
VTable = CGF.Builder.CreateBitCast(VTable, Ty);
|
|
|
|
assert(VTable && "BuildVirtualCall = kext vtbl pointer is null");
|
2013-11-05 23:54:58 +08:00
|
|
|
uint64_t VTableIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(GD);
|
2013-07-19 16:14:45 +08:00
|
|
|
uint64_t AddressPoint =
|
2013-11-05 23:54:58 +08:00
|
|
|
CGM.getItaniumVTableContext().getVTableLayout(RD)
|
2013-07-19 16:14:45 +08:00
|
|
|
.getAddressPoint(BaseSubobject(RD, CharUnits::Zero()));
|
|
|
|
VTableIndex += AddressPoint;
|
|
|
|
llvm::Value *VFuncPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt");
|
Compute and preserve alignment more faithfully in IR-generation.
Introduce an Address type to bundle a pointer value with an
alignment. Introduce APIs on CGBuilderTy to work with Address
values. Change core APIs on CGF/CGM to traffic in Address where
appropriate. Require alignments to be non-zero. Update a ton
of code to compute and propagate alignment information.
As part of this, I've promoted CGBuiltin's EmitPointerWithAlignment
helper function to CGF and made use of it in a number of places in
the expression emitter.
The end result is that we should now be significantly more correct
when performing operations on objects that are locally known to
be under-aligned. Since alignment is not reliably tracked in the
type system, there are inherent limits to this, but at least we
are no longer confused by standard operations like derived-to-base
conversions and array-to-pointer decay. I've also fixed a large
number of bugs where we were applying the complete-object alignment
to a pointer instead of the non-virtual alignment, although most of
these were hidden by the very conservative approach we took with
member alignment.
Also, because IRGen now reliably asserts on zero alignments, we
should no longer be subject to an absurd but frustrating recurring
bug where an incomplete type would report a zero alignment and then
we'd naively do a alignmentAtOffset on it and emit code using an
alignment equal to the largest power-of-two factor of the offset.
We should also now be emitting much more aggressive alignment
attributes in the presence of over-alignment. In particular,
field access now uses alignmentAtOffset instead of min.
Several times in this patch, I had to change the existing
code-generation pattern in order to more effectively use
the Address APIs. For the most part, this seems to be a strict
improvement, like doing pointer arithmetic with GEPs instead of
ptrtoint. That said, I've tried very hard to not change semantics,
but it is likely that I've failed in a few places, for which I
apologize.
ABIArgInfo now always carries the assumed alignment of indirect and
indirect byval arguments. In order to cut down on what was already
a dauntingly large patch, I changed the code to never set align
attributes in the IR on non-byval indirect arguments. That is,
we still generate code which assumes that indirect arguments have
the given alignment, but we don't express this information to the
backend except where it's semantically required (i.e. on byvals).
This is likely a minor regression for those targets that did provide
this information, but it'll be trivial to add it back in a later
patch.
I partially punted on applying this work to CGBuiltin. Please
do not add more uses of the CreateDefaultAligned{Load,Store}
APIs; they will be going away eventually.
llvm-svn: 246985
2015-09-08 16:05:57 +08:00
|
|
|
return CGF.Builder.CreateAlignedLoad(VFuncPtr, CGF.PointerAlignInBytes);
|
2009-11-13 12:45:41 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 16:14:45 +08:00
|
|
|
/// BuildAppleKextVirtualCall - This routine is to support gcc's kext ABI making
|
2011-01-21 01:19:02 +08:00
|
|
|
/// indirect call to virtual functions. It makes the call through indexing
|
|
|
|
/// into the vtable.
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
|
|
|
|
NestedNameSpecifier *Qual,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty) {
|
2011-01-21 01:19:02 +08:00
|
|
|
assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) &&
|
|
|
|
"BuildAppleKextVirtualCall - bad Qual kind");
|
|
|
|
|
|
|
|
const Type *QTy = Qual->getAsType();
|
|
|
|
QualType T = QualType(QTy, 0);
|
|
|
|
const RecordType *RT = T->getAs<RecordType>();
|
|
|
|
assert(RT && "BuildAppleKextVirtualCall - Qual type must be record");
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
if (const auto *DD = dyn_cast<CXXDestructorDecl>(MD))
|
2011-02-02 07:22:34 +08:00
|
|
|
return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD);
|
2013-07-19 16:14:45 +08:00
|
|
|
|
|
|
|
return ::BuildAppleKextVirtualCall(*this, MD, Ty, RD);
|
2011-01-21 01:19:02 +08:00
|
|
|
}
|
|
|
|
|
2011-02-02 07:22:34 +08:00
|
|
|
/// BuildVirtualCall - This routine makes indirect vtable call for
|
|
|
|
/// call to virtual destructors. It returns 0 if it could not do it.
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::BuildAppleKextVirtualDestructorCall(
|
|
|
|
const CXXDestructorDecl *DD,
|
|
|
|
CXXDtorType Type,
|
|
|
|
const CXXRecordDecl *RD) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *MD = cast<CXXMethodDecl>(DD);
|
2011-02-02 07:22:34 +08:00
|
|
|
// FIXME. Dtor_Base dtor is always direct!!
|
|
|
|
// It need be somehow inline expanded into the caller.
|
|
|
|
// -O does that. But need to support -O0 as well.
|
|
|
|
if (MD->isVirtual() && Type != Dtor_Base) {
|
|
|
|
// Compute the function type we're calling.
|
2014-09-09 00:01:27 +08:00
|
|
|
const CGFunctionInfo &FInfo = CGM.getTypes().arrangeCXXStructorDeclaration(
|
|
|
|
DD, StructorType::Complete);
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo);
|
2013-07-19 16:14:45 +08:00
|
|
|
return ::BuildAppleKextVirtualCall(*this, GlobalDecl(DD, Type), Ty, RD);
|
2011-02-02 07:22:34 +08:00
|
|
|
}
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2011-02-02 07:22:34 +08:00
|
|
|
}
|