2007-05-28 09:07:47 +08:00
|
|
|
//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
|
|
|
|
//
|
|
|
|
// 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-05-28 09:07:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-module state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
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
|
|
|
#include "CGBlocks.h"
|
2011-10-07 02:29:37 +08:00
|
|
|
#include "CGCUDARuntime.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGCall.h"
|
|
|
|
#include "CGDebugInfo.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2011-09-20 05:14:35 +08:00
|
|
|
#include "CGOpenCLRuntime.h"
|
2014-05-06 18:08:46 +08:00
|
|
|
#include "CGOpenMPRuntime.h"
|
2016-02-08 23:59:20 +08:00
|
|
|
#include "CGOpenMPRuntimeNVPTX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "CodeGenPGO.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "CodeGenTBAA.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "CoverageMappingGen.h"
|
2010-01-10 20:58:08 +08:00
|
|
|
#include "TargetInfo.h"
|
2007-06-16 08:16:26 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-01-26 21:48:07 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2008-11-05 00:51:42 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-06-22 02:41:26 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2011-01-14 02:57:25 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2009-12-01 07:41:22 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2011-10-27 04:41:06 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-12-19 22:41:01 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2007-12-02 15:19:18 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2013-01-15 01:21:00 +08:00
|
|
|
#include "clang/Basic/Module.h"
|
2008-04-19 12:17:09 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-06-23 02:48:09 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2013-10-17 03:28:50 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2017-03-03 04:04:19 +08:00
|
|
|
#include "clang/CodeGen/ConstantInitBuilder.h"
|
2016-04-09 00:52:00 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-10-23 03:26:13 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2010-03-06 08:35:14 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2014-03-04 19:02:08 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/CallingConv.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2014-04-19 05:52:00 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2009-11-07 17:22:46 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-12-16 07:00:20 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
static const char AnnotationSection[] = "llvm.metadata";
|
|
|
|
|
2013-12-29 05:58:40 +08:00
|
|
|
static CGCXXABI *createCXXABI(CodeGenModule &CGM) {
|
2013-04-17 06:48:15 +08:00
|
|
|
switch (CGM.getTarget().getCXXABI().getKind()) {
|
2013-01-31 20:13:10 +08:00
|
|
|
case TargetCXXABI::GenericAArch64:
|
2013-01-26 07:36:14 +08:00
|
|
|
case TargetCXXABI::GenericARM:
|
|
|
|
case TargetCXXABI::iOS:
|
2014-03-29 23:09:45 +08:00
|
|
|
case TargetCXXABI::iOS64:
|
2015-10-31 00:30:36 +08:00
|
|
|
case TargetCXXABI::WatchOS:
|
2015-02-18 23:21:35 +08:00
|
|
|
case TargetCXXABI::GenericMIPS:
|
2013-01-26 07:36:14 +08:00
|
|
|
case TargetCXXABI::GenericItanium:
|
2015-09-04 06:51:53 +08:00
|
|
|
case TargetCXXABI::WebAssembly:
|
2013-12-29 05:58:40 +08:00
|
|
|
return CreateItaniumCXXABI(CGM);
|
2013-01-26 07:36:14 +08:00
|
|
|
case TargetCXXABI::Microsoft:
|
2013-12-29 05:58:40 +08:00
|
|
|
return CreateMicrosoftCXXABI(CGM);
|
2010-08-23 05:01:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid C++ ABI kind");
|
|
|
|
}
|
|
|
|
|
2015-06-30 10:26:03 +08:00
|
|
|
CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
|
|
|
|
const PreprocessorOptions &PPO,
|
|
|
|
const CodeGenOptions &CGO, llvm::Module &M,
|
2014-08-05 02:41:51 +08:00
|
|
|
DiagnosticsEngine &diags,
|
|
|
|
CoverageSourceInfo *CoverageInfo)
|
2015-06-30 10:26:03 +08:00
|
|
|
: Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
|
|
|
|
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
|
2015-07-25 00:04:29 +08:00
|
|
|
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
|
2016-04-14 07:37:17 +08:00
|
|
|
VMContext(M.getContext()), Types(*this), VTables(*this),
|
2016-04-29 01:09:37 +08:00
|
|
|
SanitizerMD(new SanitizerMetadata(*this)) {
|
2013-01-18 19:30:38 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
// Initialize the type cache.
|
|
|
|
llvm::LLVMContext &LLVMContext = M.getContext();
|
|
|
|
VoidTy = llvm::Type::getVoidTy(LLVMContext);
|
|
|
|
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
|
|
|
|
Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
|
|
|
|
Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
|
|
|
|
Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
|
|
|
|
FloatTy = llvm::Type::getFloatTy(LLVMContext);
|
|
|
|
DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
|
|
|
|
PointerWidthInBits = C.getTargetInfo().getPointerWidth(0);
|
|
|
|
PointerAlignInBytes =
|
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
|
|
|
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
|
2016-08-19 13:17:25 +08:00
|
|
|
SizeSizeInBytes =
|
|
|
|
C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
|
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
|
|
|
IntAlignInBytes =
|
|
|
|
C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
|
2012-02-07 08:39:47 +08:00
|
|
|
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
|
2016-08-19 13:17:25 +08:00
|
|
|
IntPtrTy = llvm::IntegerType::get(LLVMContext,
|
|
|
|
C.getTargetInfo().getMaxPointerWidth());
|
2012-02-07 08:39:47 +08:00
|
|
|
Int8PtrTy = Int8Ty->getPointerTo(0);
|
|
|
|
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
|
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
|
2014-12-03 00:04:58 +08:00
|
|
|
BuiltinCC = getTargetCodeGenInfo().getABIInfo().getBuiltinCC();
|
2013-03-01 03:01:20 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.ObjC1)
|
2011-09-20 05:14:35 +08:00
|
|
|
createObjCRuntime();
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.OpenCL)
|
2011-09-20 05:14:35 +08:00
|
|
|
createOpenCLRuntime();
|
2014-05-06 18:08:46 +08:00
|
|
|
if (LangOpts.OpenMP)
|
|
|
|
createOpenMPRuntime();
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CUDA)
|
2011-10-07 02:29:37 +08:00
|
|
|
createCUDARuntime();
|
2008-05-08 16:54:20 +08:00
|
|
|
|
2012-04-24 14:57:01 +08:00
|
|
|
// Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
|
2012-04-24 14:57:01 +08:00
|
|
|
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
|
2016-04-14 07:37:17 +08:00
|
|
|
TBAA.reset(new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
|
|
|
|
getCXXABI().getMangleContext()));
|
2010-10-15 07:06:10 +08:00
|
|
|
|
2011-04-22 07:44:07 +08:00
|
|
|
// If debug info or coverage generation is enabled, create the CGDebugInfo
|
|
|
|
// object.
|
2016-02-02 19:06:51 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo ||
|
|
|
|
CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)
|
2016-04-14 07:37:17 +08:00
|
|
|
DebugInfo.reset(new CGDebugInfo(*this));
|
2011-02-08 16:22:06 +08:00
|
|
|
|
|
|
|
Block.GlobalUniqueCount = 0;
|
2011-02-15 17:22:45 +08:00
|
|
|
|
2015-10-22 02:06:43 +08:00
|
|
|
if (C.getLangOpts().ObjC1)
|
2016-04-14 07:37:17 +08:00
|
|
|
ObjCData.reset(new ObjCEntrypoints());
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2016-03-03 04:59:36 +08:00
|
|
|
if (CodeGenOpts.hasProfileClangUse()) {
|
|
|
|
auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
|
|
|
|
CodeGenOpts.ProfileInstrumentUsePath);
|
2016-05-19 11:54:54 +08:00
|
|
|
if (auto E = ReaderOrErr.takeError()) {
|
2014-04-19 05:52:00 +08:00
|
|
|
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
|
2015-06-26 06:56:00 +08:00
|
|
|
"Could not read profile %0: %1");
|
2016-05-19 11:54:54 +08:00
|
|
|
llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
|
|
|
|
getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath
|
|
|
|
<< EI.message();
|
|
|
|
});
|
2015-04-06 12:16:48 +08:00
|
|
|
} else
|
|
|
|
PGOReader = std::move(ReaderOrErr.get());
|
2014-04-19 05:52:00 +08:00
|
|
|
}
|
2014-08-05 02:41:51 +08:00
|
|
|
|
|
|
|
// If coverage mapping generation is enabled, create the
|
|
|
|
// CoverageMappingModuleGen object.
|
|
|
|
if (CodeGenOpts.CoverageMapping)
|
|
|
|
CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
|
2008-03-01 16:45:05 +08:00
|
|
|
}
|
|
|
|
|
2016-04-14 07:37:17 +08:00
|
|
|
CodeGenModule::~CodeGenModule() {}
|
2008-08-06 02:50:11 +08:00
|
|
|
|
2010-01-23 10:40:42 +08:00
|
|
|
void CodeGenModule::createObjCRuntime() {
|
2012-06-20 14:18:46 +08:00
|
|
|
// This is just isGNUFamily(), but we want to force implementors of
|
|
|
|
// new ABIs to decide how best to do this.
|
|
|
|
switch (LangOpts.ObjCRuntime.getKind()) {
|
2012-07-04 04:49:52 +08:00
|
|
|
case ObjCRuntime::GNUstep:
|
|
|
|
case ObjCRuntime::GCC:
|
2012-07-12 10:07:58 +08:00
|
|
|
case ObjCRuntime::ObjFW:
|
2016-04-14 07:37:17 +08:00
|
|
|
ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
|
2012-06-20 14:18:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ObjCRuntime::FragileMacOSX:
|
|
|
|
case ObjCRuntime::MacOSX:
|
|
|
|
case ObjCRuntime::iOS:
|
2015-10-31 00:30:36 +08:00
|
|
|
case ObjCRuntime::WatchOS:
|
2016-04-14 07:37:17 +08:00
|
|
|
ObjCRuntime.reset(CreateMacObjCRuntime(*this));
|
2012-06-20 14:18:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad runtime kind");
|
2010-01-23 10:40:42 +08:00
|
|
|
}
|
|
|
|
|
2011-09-20 05:14:35 +08:00
|
|
|
void CodeGenModule::createOpenCLRuntime() {
|
2016-04-14 07:37:17 +08:00
|
|
|
OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
|
2011-09-20 05:14:35 +08:00
|
|
|
}
|
|
|
|
|
2014-05-06 18:08:46 +08:00
|
|
|
void CodeGenModule::createOpenMPRuntime() {
|
2016-02-08 23:59:20 +08:00
|
|
|
// Select a specialized code generation class based on the target, if any.
|
|
|
|
// If it does not exist use the default implementation.
|
2016-09-14 23:17:46 +08:00
|
|
|
switch (getTriple().getArch()) {
|
2016-02-08 23:59:20 +08:00
|
|
|
case llvm::Triple::nvptx:
|
|
|
|
case llvm::Triple::nvptx64:
|
|
|
|
assert(getLangOpts().OpenMPIsDevice &&
|
|
|
|
"OpenMP NVPTX is only prepared to deal with device code.");
|
2016-04-14 07:37:17 +08:00
|
|
|
OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this));
|
2016-02-08 23:59:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-14 07:37:17 +08:00
|
|
|
OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
|
2016-02-08 23:59:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-05-06 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
2011-10-07 02:29:37 +08:00
|
|
|
void CodeGenModule::createCUDARuntime() {
|
2016-04-14 07:37:17 +08:00
|
|
|
CUDARuntime.reset(CreateNVCUDARuntime(*this));
|
2011-10-07 02:29:37 +08:00
|
|
|
}
|
|
|
|
|
2014-09-16 23:18:21 +08:00
|
|
|
void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
|
|
|
|
Replacements[Name] = C;
|
|
|
|
}
|
|
|
|
|
2013-11-06 05:37:29 +08:00
|
|
|
void CodeGenModule::applyReplacements() {
|
2015-06-11 20:33:25 +08:00
|
|
|
for (auto &I : Replacements) {
|
|
|
|
StringRef MangledName = I.first();
|
|
|
|
llvm::Constant *Replacement = I.second;
|
2013-11-06 05:37:29 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (!Entry)
|
|
|
|
continue;
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *OldF = cast<llvm::Function>(Entry);
|
|
|
|
auto *NewF = dyn_cast<llvm::Function>(Replacement);
|
2013-11-12 12:53:19 +08:00
|
|
|
if (!NewF) {
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
|
2014-05-17 06:20:18 +08:00
|
|
|
NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
|
2014-02-04 02:54:51 +08:00
|
|
|
} else {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *CE = cast<llvm::ConstantExpr>(Replacement);
|
2014-02-04 02:54:51 +08:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast ||
|
|
|
|
CE->getOpcode() == llvm::Instruction::GetElementPtr);
|
|
|
|
NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
|
|
|
|
}
|
2013-11-12 12:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace old with new, but keep the old order.
|
|
|
|
OldF->replaceAllUsesWith(Replacement);
|
|
|
|
if (NewF) {
|
|
|
|
NewF->removeFromParent();
|
2015-11-07 07:00:41 +08:00
|
|
|
OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
|
|
|
|
NewF);
|
2013-11-12 12:53:19 +08:00
|
|
|
}
|
|
|
|
OldF->eraseFromParent();
|
2013-11-06 05:37:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
|
|
|
|
GlobalValReplacements.push_back(std::make_pair(GV, C));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::applyGlobalValReplacements() {
|
|
|
|
for (auto &I : GlobalValReplacements) {
|
|
|
|
llvm::GlobalValue *GV = I.first;
|
|
|
|
llvm::Constant *C = I.second;
|
|
|
|
|
|
|
|
GV->replaceAllUsesWith(C);
|
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 10:42:01 +08:00
|
|
|
// This is only used in aliases that we created and we know they have a
|
|
|
|
// linear structure.
|
2016-04-11 15:48:59 +08:00
|
|
|
static const llvm::GlobalObject *getAliasedGlobal(
|
|
|
|
const llvm::GlobalIndirectSymbol &GIS) {
|
|
|
|
llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited;
|
|
|
|
const llvm::Constant *C = &GIS;
|
2014-06-03 10:42:01 +08:00
|
|
|
for (;;) {
|
2014-06-05 03:03:20 +08:00
|
|
|
C = C->stripPointerCasts();
|
2014-06-03 10:42:01 +08:00
|
|
|
if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
|
|
|
|
return GO;
|
|
|
|
// stripPointerCasts will not walk over weak aliases.
|
2016-04-11 15:48:59 +08:00
|
|
|
auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C);
|
|
|
|
if (!GIS2)
|
2014-06-03 10:42:01 +08:00
|
|
|
return nullptr;
|
2016-04-11 15:48:59 +08:00
|
|
|
if (!Visited.insert(GIS2).second)
|
2014-06-03 10:42:01 +08:00
|
|
|
return nullptr;
|
2016-04-11 15:48:59 +08:00
|
|
|
C = GIS2->getIndirectSymbol();
|
2014-06-03 10:42:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 03:26:13 +08:00
|
|
|
void CodeGenModule::checkAliases() {
|
2014-03-27 23:27:20 +08:00
|
|
|
// Check if the constructed aliases are well formed. It is really unfortunate
|
|
|
|
// that we have to do this in CodeGen, but we only construct mangled names
|
|
|
|
// and aliases during codegen.
|
2013-10-23 03:26:13 +08:00
|
|
|
bool Error = false;
|
2014-05-06 03:33:09 +08:00
|
|
|
DiagnosticsEngine &Diags = getDiags();
|
2015-06-11 20:33:25 +08:00
|
|
|
for (const GlobalDecl &GD : Aliases) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *D = cast<ValueDecl>(GD.getDecl());
|
2016-04-11 15:48:59 +08:00
|
|
|
SourceLocation Location;
|
|
|
|
bool IsIFunc = D->hasAttr<IFuncAttr>();
|
|
|
|
if (const Attr *A = D->getDefiningAttr())
|
|
|
|
Location = A->getLocation();
|
|
|
|
else
|
|
|
|
llvm_unreachable("Not an alias or ifunc?");
|
2013-10-23 03:26:13 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2016-04-11 15:48:59 +08:00
|
|
|
auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry);
|
2014-06-03 10:42:01 +08:00
|
|
|
const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
|
|
|
|
if (!GV) {
|
|
|
|
Error = true;
|
2016-04-11 15:48:59 +08:00
|
|
|
Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
|
2014-06-03 10:42:01 +08:00
|
|
|
} else if (GV->isDeclaration()) {
|
2014-03-13 23:47:50 +08:00
|
|
|
Error = true;
|
2016-04-11 15:48:59 +08:00
|
|
|
Diags.Report(Location, diag::err_alias_to_undefined)
|
|
|
|
<< IsIFunc << IsIFunc;
|
|
|
|
} else if (IsIFunc) {
|
|
|
|
// Check resolver function type.
|
|
|
|
llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(
|
|
|
|
GV->getType()->getPointerElementType());
|
|
|
|
assert(FTy);
|
|
|
|
if (!FTy->getReturnType()->isPointerTy())
|
|
|
|
Diags.Report(Location, diag::err_ifunc_resolver_return);
|
|
|
|
if (FTy->getNumParams())
|
|
|
|
Diags.Report(Location, diag::err_ifunc_resolver_params);
|
2013-10-23 03:26:13 +08:00
|
|
|
}
|
2014-03-27 23:27:20 +08:00
|
|
|
|
2016-04-11 15:48:59 +08:00
|
|
|
llvm::Constant *Aliasee = Alias->getIndirectSymbol();
|
2014-06-03 10:42:01 +08:00
|
|
|
llvm::GlobalValue *AliaseeGV;
|
|
|
|
if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
|
|
|
|
AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
|
|
|
|
else
|
|
|
|
AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
|
|
|
|
|
2014-05-06 04:21:03 +08:00
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
|
|
|
|
StringRef AliasSection = SA->getName();
|
2014-06-03 10:42:01 +08:00
|
|
|
if (AliasSection != AliaseeGV->getSection())
|
2014-05-06 04:21:03 +08:00
|
|
|
Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
|
2016-04-11 15:48:59 +08:00
|
|
|
<< AliasSection << IsIFunc << IsIFunc;
|
2014-05-06 04:21:03 +08:00
|
|
|
}
|
2014-06-03 10:42:01 +08:00
|
|
|
|
|
|
|
// We have to handle alias to weak aliases in here. LLVM itself disallows
|
|
|
|
// this since the object semantics would not match the IL one. For
|
|
|
|
// compatibility with gcc we implement it by just pointing the alias
|
|
|
|
// to its aliasee's aliasee. We also warn, since the user is probably
|
|
|
|
// expecting the link to be weak.
|
2016-04-11 15:48:59 +08:00
|
|
|
if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) {
|
2016-04-08 09:31:02 +08:00
|
|
|
if (GA->isInterposable()) {
|
2016-04-11 15:48:59 +08:00
|
|
|
Diags.Report(Location, diag::warn_alias_to_weak_alias)
|
|
|
|
<< GV->getName() << GA->getName() << IsIFunc;
|
2014-06-03 10:42:01 +08:00
|
|
|
Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
|
2016-04-11 15:48:59 +08:00
|
|
|
GA->getIndirectSymbol(), Alias->getType());
|
|
|
|
Alias->setIndirectSymbol(Aliasee);
|
2014-06-03 10:42:01 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 03:26:13 +08:00
|
|
|
}
|
|
|
|
if (!Error)
|
|
|
|
return;
|
|
|
|
|
2015-06-11 20:33:25 +08:00
|
|
|
for (const GlobalDecl &GD : Aliases) {
|
2013-10-23 03:26:13 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2016-04-11 15:48:59 +08:00
|
|
|
auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry);
|
2013-10-23 03:26:13 +08:00
|
|
|
Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
|
|
|
|
Alias->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 22:59:08 +08:00
|
|
|
void CodeGenModule::clear() {
|
|
|
|
DeferredDeclsToEmit.clear();
|
2015-03-18 12:13:55 +08:00
|
|
|
if (OpenMPRuntime)
|
|
|
|
OpenMPRuntime->clear();
|
2013-12-09 22:59:08 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 09:45:07 +08:00
|
|
|
void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags,
|
|
|
|
StringRef MainFile) {
|
|
|
|
if (!hasDiagnostics())
|
|
|
|
return;
|
|
|
|
if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
|
|
|
|
if (MainFile.empty())
|
|
|
|
MainFile = "<stdin>";
|
|
|
|
Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
|
|
|
|
} else
|
|
|
|
Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Missing
|
|
|
|
<< Mismatched;
|
|
|
|
}
|
|
|
|
|
2008-08-06 02:50:11 +08:00
|
|
|
void CodeGenModule::Release() {
|
2009-03-23 05:21:57 +08:00
|
|
|
EmitDeferred();
|
2015-08-31 21:20:44 +08:00
|
|
|
applyGlobalValReplacements();
|
2013-11-06 05:37:29 +08:00
|
|
|
applyReplacements();
|
2013-10-23 03:26:13 +08:00
|
|
|
checkAliases();
|
2010-01-08 08:50:11 +08:00
|
|
|
EmitCXXGlobalInitFunc();
|
2010-03-20 12:15:41 +08:00
|
|
|
EmitCXXGlobalDtorFunc();
|
2013-04-20 00:42:07 +08:00
|
|
|
EmitCXXThreadLocalInitFunc();
|
2011-07-28 04:29:46 +08:00
|
|
|
if (ObjCRuntime)
|
|
|
|
if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
|
2008-08-12 02:12:00 +08:00
|
|
|
AddGlobalCtor(ObjCInitFunction);
|
2015-05-08 03:34:16 +08:00
|
|
|
if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice &&
|
|
|
|
CUDARuntime) {
|
|
|
|
if (llvm::Function *CudaCtorFunction = CUDARuntime->makeModuleCtorFunction())
|
|
|
|
AddGlobalCtor(CudaCtorFunction);
|
|
|
|
if (llvm::Function *CudaDtorFunction = CUDARuntime->makeModuleDtorFunction())
|
|
|
|
AddGlobalDtor(CudaDtorFunction);
|
|
|
|
}
|
2016-01-06 21:42:12 +08:00
|
|
|
if (OpenMPRuntime)
|
|
|
|
if (llvm::Function *OpenMPRegistrationFunction =
|
|
|
|
OpenMPRuntime->emitRegistrationFunction())
|
|
|
|
AddGlobalCtor(OpenMPRegistrationFunction, 0);
|
2015-12-18 03:14:27 +08:00
|
|
|
if (PGOReader) {
|
2016-03-25 05:32:25 +08:00
|
|
|
getModule().setProfileSummary(PGOReader->getSummary().getMD(VMContext));
|
2015-12-18 03:14:27 +08:00
|
|
|
if (PGOStats.hasDiagnostics())
|
|
|
|
PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
|
|
|
|
}
|
2008-08-01 08:01:51 +08:00
|
|
|
EmitCtorList(GlobalCtors, "llvm.global_ctors");
|
|
|
|
EmitCtorList(GlobalDtors, "llvm.global_dtors");
|
2011-09-10 06:41:49 +08:00
|
|
|
EmitGlobalAnnotations();
|
2013-04-06 13:00:46 +08:00
|
|
|
EmitStaticExternCAliases();
|
2014-08-05 02:41:51 +08:00
|
|
|
EmitDeferredUnusedCoverageMappings();
|
|
|
|
if (CoverageMapping)
|
|
|
|
CoverageMapping->emit();
|
2016-01-26 07:34:52 +08:00
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso)
|
|
|
|
CodeGenFunction(*this).EmitCfiCheckFail();
|
2014-03-07 06:15:10 +08:00
|
|
|
emitLLVMUsed();
|
2016-01-16 08:31:22 +08:00
|
|
|
if (SanStats)
|
|
|
|
SanStats->finish();
|
2013-01-16 09:23:41 +08:00
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
if (CodeGenOpts.Autolink &&
|
|
|
|
(Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
|
2013-01-16 09:23:41 +08:00
|
|
|
EmitModuleLinkOptions();
|
|
|
|
}
|
2015-08-06 02:51:13 +08:00
|
|
|
if (CodeGenOpts.DwarfVersion) {
|
2013-06-19 09:46:49 +08:00
|
|
|
// We actually want the latest version when there are conflicts.
|
|
|
|
// We can change from Warning to Latest if such mode is supported.
|
|
|
|
getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version",
|
|
|
|
CodeGenOpts.DwarfVersion);
|
2015-08-06 02:51:13 +08:00
|
|
|
}
|
|
|
|
if (CodeGenOpts.EmitCodeView) {
|
|
|
|
// Indicate that we want CodeView in the metadata.
|
|
|
|
getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
|
|
|
|
}
|
2015-09-16 05:46:50 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
|
|
|
|
// We don't support LTO with 2 with different StrictVTablePointers
|
|
|
|
// FIXME: we could support it by stripping all the information introduced
|
|
|
|
// by StrictVTablePointers.
|
|
|
|
|
|
|
|
getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
|
|
|
|
|
|
|
|
llvm::Metadata *Ops[2] = {
|
|
|
|
llvm::MDString::get(VMContext, "StrictVTablePointers"),
|
|
|
|
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 1))};
|
|
|
|
|
|
|
|
getModule().addModuleFlag(llvm::Module::Require,
|
|
|
|
"StrictVTablePointersRequirement",
|
|
|
|
llvm::MDNode::get(VMContext, Ops));
|
|
|
|
}
|
2013-11-23 03:42:45 +08:00
|
|
|
if (DebugInfo)
|
2014-05-20 07:40:06 +08:00
|
|
|
// We support a single version in the linked module. The LLVM
|
|
|
|
// parser will drop debug info with a different version number
|
|
|
|
// (and warn about it, too).
|
|
|
|
getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
|
2013-12-03 04:10:37 +08:00
|
|
|
llvm::DEBUG_METADATA_VERSION);
|
Switch autolinking metadata format over to actual linker options, e.g.,
!0 = metadata !{metadata !"-lautolink"}
!1 = metadata !{metadata !"-framework", metadata !"autolink_framework"}
referenced from llvm.module.linkoptions, e.g.,
!llvm.module.linkoptions = !{!0, !1, !2, !3}
This conceptually moves the logic for figuring out the syntax the
linker will accept from LLVM into Clang. Moreover, it makes it easier
to support MSVC's
#pragma comment(linker, "some option")
in the future, should anyone care to do so.
llvm-svn: 172441
2013-01-15 02:28:43 +08:00
|
|
|
|
2014-06-20 20:43:07 +08:00
|
|
|
// We need to record the widths of enums and wchar_t, so that we can generate
|
|
|
|
// the correct build attributes in the ARM backend.
|
2014-06-24 04:03:28 +08:00
|
|
|
llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
|
2014-06-20 20:43:07 +08:00
|
|
|
if ( Arch == llvm::Triple::arm
|
|
|
|
|| Arch == llvm::Triple::armeb
|
|
|
|
|| Arch == llvm::Triple::thumb
|
|
|
|
|| Arch == llvm::Triple::thumbeb) {
|
|
|
|
// Width of wchar_t in bytes
|
|
|
|
uint64_t WCharWidth =
|
|
|
|
Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
|
|
|
|
getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
|
|
|
|
|
|
|
|
// The minimum width of an enum in bytes
|
|
|
|
uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
|
|
|
|
getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
|
|
|
|
}
|
|
|
|
|
2015-12-16 07:00:20 +08:00
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso) {
|
|
|
|
// Indicate that we want cross-DSO control flow integrity checks.
|
|
|
|
getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:17:46 +08:00
|
|
|
if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
|
2016-04-06 02:26:20 +08:00
|
|
|
// Indicate whether __nvvm_reflect should be configured to flush denormal
|
|
|
|
// floating point values to 0. (This corresponds to its "__CUDA_FTZ"
|
|
|
|
// property.)
|
|
|
|
getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
|
|
|
|
LangOpts.CUDADeviceFlushDenormalsToZero ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2014-11-18 14:17:20 +08:00
|
|
|
if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
|
2016-04-29 06:34:00 +08:00
|
|
|
assert(PLevel < 3 && "Invalid PIC Level");
|
|
|
|
getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
|
2016-06-23 23:07:32 +08:00
|
|
|
if (Context.getLangOpts().PIE)
|
|
|
|
getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
|
2014-11-18 14:17:20 +08:00
|
|
|
}
|
|
|
|
|
2010-09-16 14:16:50 +08:00
|
|
|
SimplifyPersonality();
|
|
|
|
|
2010-07-07 07:57:41 +08:00
|
|
|
if (getCodeGenOpts().EmitDeclMetadata)
|
|
|
|
EmitDeclMetadata();
|
2011-05-05 04:46:58 +08:00
|
|
|
|
|
|
|
if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
|
2011-05-05 08:08:20 +08:00
|
|
|
EmitCoverageFile();
|
2011-08-17 04:58:22 +08:00
|
|
|
|
|
|
|
if (DebugInfo)
|
|
|
|
DebugInfo->finalize();
|
2013-10-17 03:28:50 +08:00
|
|
|
|
|
|
|
EmitVersionIdentMetadata();
|
2014-07-03 17:30:33 +08:00
|
|
|
|
|
|
|
EmitTargetMetadata();
|
2008-10-01 08:49:24 +08:00
|
|
|
}
|
|
|
|
|
2011-03-24 00:29:39 +08:00
|
|
|
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
|
|
|
|
// Make sure that this type is translated.
|
|
|
|
Types.UpdateCompletedType(TD);
|
|
|
|
}
|
|
|
|
|
2016-01-27 03:30:26 +08:00
|
|
|
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
|
|
|
|
// Make sure that this type is translated.
|
|
|
|
Types.RefreshTypeCacheForClass(RD);
|
|
|
|
}
|
|
|
|
|
2010-10-15 07:06:10 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-10-15 07:06:10 +08:00
|
|
|
return TBAA->getTBAAInfo(QTy);
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:03:51 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAInfoForVTablePtr() {
|
|
|
|
if (!TBAA)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2012-03-27 01:03:51 +08:00
|
|
|
return TBAA->getTBAAInfoForVTablePtr();
|
|
|
|
}
|
|
|
|
|
2012-09-29 05:58:29 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2012-09-29 05:58:29 +08:00
|
|
|
return TBAA->getTBAAStructInfo(QTy);
|
|
|
|
}
|
|
|
|
|
2013-04-05 05:53:22 +08:00
|
|
|
llvm::MDNode *CodeGenModule::getTBAAStructTagInfo(QualType BaseTy,
|
|
|
|
llvm::MDNode *AccessN,
|
|
|
|
uint64_t O) {
|
|
|
|
if (!TBAA)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-04-05 05:53:22 +08:00
|
|
|
return TBAA->getTBAAStructTagInfo(BaseTy, AccessN, O);
|
|
|
|
}
|
|
|
|
|
2013-10-08 08:08:49 +08:00
|
|
|
/// Decorate the instruction with a TBAA tag. For both scalar TBAA
|
|
|
|
/// and struct-path aware TBAA, the tag has the same format:
|
|
|
|
/// base type, access type and offset.
|
2013-04-12 07:02:56 +08:00
|
|
|
/// When ConvertTypeToTag is true, we create a tag based on the scalar type.
|
2015-09-16 05:46:55 +08:00
|
|
|
void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
|
|
|
|
llvm::MDNode *TBAAInfo,
|
|
|
|
bool ConvertTypeToTag) {
|
2013-10-08 08:08:49 +08:00
|
|
|
if (ConvertTypeToTag && TBAA)
|
2013-04-12 07:02:56 +08:00
|
|
|
Inst->setMetadata(llvm::LLVMContext::MD_tbaa,
|
|
|
|
TBAA->getTBAAScalarTagInfo(TBAAInfo));
|
|
|
|
else
|
|
|
|
Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo);
|
2010-10-15 07:06:10 +08:00
|
|
|
}
|
|
|
|
|
2015-09-16 05:46:55 +08:00
|
|
|
void CodeGenModule::DecorateInstructionWithInvariantGroup(
|
|
|
|
llvm::Instruction *I, const CXXRecordDecl *RD) {
|
|
|
|
llvm::Metadata *MD = CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
|
|
|
|
auto *MetaDataNode = dyn_cast<llvm::MDNode>(MD);
|
|
|
|
// Check if we have to wrap MDString in MDNode.
|
|
|
|
if (!MetaDataNode)
|
|
|
|
MetaDataNode = llvm::MDNode::get(getLLVMContext(), MD);
|
2015-09-18 04:25:46 +08:00
|
|
|
I->setMetadata(llvm::LLVMContext::MD_invariant_group, MetaDataNode);
|
2015-09-16 05:46:55 +08:00
|
|
|
}
|
|
|
|
|
2014-01-26 14:17:37 +08:00
|
|
|
void CodeGenModule::Error(SourceLocation loc, StringRef message) {
|
|
|
|
unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
|
|
|
|
getDiags().Report(Context.getFullLoc(loc), diagID) << message;
|
2011-03-18 10:56:14 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 15:19:18 +08:00
|
|
|
/// specified stmt yet.
|
2013-08-20 05:02:26 +08:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
|
2011-09-26 07:23:43 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
|
2009-02-07 03:18:03 +08:00
|
|
|
"cannot compile this %0 yet");
|
2007-12-02 15:19:18 +08:00
|
|
|
std::string Msg = Type;
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID)
|
|
|
|
<< Msg << S->getSourceRange();
|
2007-12-02 15:19:18 +08:00
|
|
|
}
|
2007-12-02 14:27:33 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2008-01-12 15:05:38 +08:00
|
|
|
/// specified decl yet.
|
2013-08-20 05:02:26 +08:00
|
|
|
void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
|
2011-09-26 07:23:43 +08:00
|
|
|
unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error,
|
2009-02-07 03:18:03 +08:00
|
|
|
"cannot compile this %0 yet");
|
2008-01-12 15:05:38 +08:00
|
|
|
std::string Msg = Type;
|
This reworks some of the Diagnostic interfaces a bit to change how diagnostics
are formed. In particular, a diagnostic with all its strings and ranges is now
packaged up and sent to DiagnosticClients as a DiagnosticInfo instead of as a
ton of random stuff. This has the benefit of simplifying the interface, making
it more extensible, and allowing us to do more checking for things like access
past the end of the various arrays passed in.
In addition to introducing DiagnosticInfo, this also substantially changes how
Diagnostic::Report works. Instead of being passed in all of the info required
to issue a diagnostic, Report now takes only the required info (a location and
ID) and returns a fresh DiagnosticInfo *by value*. The caller is then free to
stuff strings and ranges into the DiagnosticInfo with the << operator. When
the dtor runs on the DiagnosticInfo object (which should happen at the end of
the statement), the diagnostic is actually emitted with all of the accumulated
information. This is a somewhat tricky dance, but it means that the
accumulated DiagnosticInfo is allowed to keep pointers to other expression
temporaries without those pointers getting invalidated.
This is just the minimal change to get this stuff working, but this will allow
us to eliminate the zillions of variant "Diag" methods scattered throughout
(e.g.) sema. For example, instead of calling:
Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
SourceRange(BuiltinLoc, RParenLoc));
We will soon be able to just do:
Diag(BuiltinLoc, diag::err_overload_no_match)
<< typeNames << SourceRange(BuiltinLoc, RParenLoc));
This scales better to support arbitrary types being passed in (not just
strings) in a type-safe way. Go operator overloading?!
llvm-svn: 59502
2008-11-18 15:04:44 +08:00
|
|
|
getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
|
2008-01-12 15:05:38 +08:00
|
|
|
}
|
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
|
|
|
|
return llvm::ConstantInt::get(SizeTy, size.getQuantity());
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
|
2011-01-30 03:39:23 +08:00
|
|
|
const NamedDecl *D) const {
|
2009-04-14 14:00:08 +08:00
|
|
|
// Internal definitions always have default visibility.
|
2009-04-14 13:27:13 +08:00
|
|
|
if (GV->hasLocalLinkage()) {
|
2009-04-11 04:26:50 +08:00
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-07 13:48:37 +08:00
|
|
|
return;
|
2009-04-11 04:26:50 +08:00
|
|
|
}
|
2009-04-07 13:48:37 +08:00
|
|
|
|
2010-10-30 19:50:40 +08:00
|
|
|
// Set visibility for definitions.
|
2013-02-27 10:15:29 +08:00
|
|
|
LinkageInfo LV = D->getLinkageAndVisibility();
|
2013-02-27 10:56:45 +08:00
|
|
|
if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage())
|
|
|
|
GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
|
2009-04-03 11:28:57 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 16:01:44 +08:00
|
|
|
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
|
|
|
|
return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
|
|
|
|
.Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
|
|
|
|
.Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
|
|
|
|
.Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
|
|
|
|
.Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(
|
|
|
|
CodeGenOptions::TLSModel M) {
|
|
|
|
switch (M) {
|
|
|
|
case CodeGenOptions::GeneralDynamicTLSModel:
|
|
|
|
return llvm::GlobalVariable::GeneralDynamicTLSModel;
|
|
|
|
case CodeGenOptions::LocalDynamicTLSModel:
|
|
|
|
return llvm::GlobalVariable::LocalDynamicTLSModel;
|
|
|
|
case CodeGenOptions::InitialExecTLSModel:
|
|
|
|
return llvm::GlobalVariable::InitialExecTLSModel;
|
|
|
|
case CodeGenOptions::LocalExecTLSModel:
|
|
|
|
return llvm::GlobalVariable::LocalExecTLSModel;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid TLS model!");
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:38:23 +08:00
|
|
|
void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
|
2013-04-13 10:43:54 +08:00
|
|
|
assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
|
2012-06-28 16:01:44 +08:00
|
|
|
|
2014-10-16 06:38:23 +08:00
|
|
|
llvm::GlobalValue::ThreadLocalMode TLM;
|
2012-10-24 04:05:01 +08:00
|
|
|
TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel());
|
2012-06-28 16:01:44 +08:00
|
|
|
|
|
|
|
// Override the TLS model if it is explicitly specified.
|
2013-12-19 11:09:10 +08:00
|
|
|
if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
|
2012-06-28 16:01:44 +08:00
|
|
|
TLM = GetLLVMTLSModel(Attr->getModel());
|
|
|
|
}
|
|
|
|
|
|
|
|
GV->setThreadLocalMode(TLM);
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef CodeGenModule::getMangledName(GlobalDecl GD) {
|
2016-01-09 04:48:26 +08:00
|
|
|
GlobalDecl CanonicalGD = GD.getCanonicalDecl();
|
|
|
|
|
|
|
|
// Some ABIs don't have constructor variants. Make sure that base and
|
|
|
|
// complete constructors get mangled the same.
|
|
|
|
if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
|
|
|
|
if (!getTarget().getCXXABI().hasConstructorVariants()) {
|
|
|
|
CXXCtorType OrigCtorType = GD.getCtorType();
|
|
|
|
assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
|
|
|
|
if (OrigCtorType == Ctor_Base)
|
|
|
|
CanonicalGD = GlobalDecl(CD, Ctor_Complete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef &FoundStr = MangledDeclNames[CanonicalGD];
|
2014-06-06 06:10:59 +08:00
|
|
|
if (!FoundStr.empty())
|
|
|
|
return FoundStr;
|
2010-06-23 00:05:32 +08:00
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
const auto *ND = cast<NamedDecl>(GD.getDecl());
|
|
|
|
SmallString<256> Buffer;
|
|
|
|
StringRef Str;
|
|
|
|
if (getCXXABI().getMangleContext().shouldMangleDeclName(ND)) {
|
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
if (const auto *D = dyn_cast<CXXConstructorDecl>(ND))
|
|
|
|
getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out);
|
|
|
|
else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND))
|
|
|
|
getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out);
|
|
|
|
else
|
|
|
|
getCXXABI().getMangleContext().mangleName(ND, Out);
|
|
|
|
Str = Out.str();
|
|
|
|
} else {
|
2010-06-23 00:05:32 +08:00
|
|
|
IdentifierInfo *II = ND->getIdentifier();
|
|
|
|
assert(II && "Attempt to mangle unnamed decl.");
|
2016-11-03 02:29:35 +08:00
|
|
|
const auto *FD = dyn_cast<FunctionDecl>(ND);
|
|
|
|
|
|
|
|
if (FD &&
|
|
|
|
FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
|
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
Out << "__regcall3__" << II->getName();
|
|
|
|
Str = Out.str();
|
|
|
|
} else {
|
|
|
|
Str = II->getName();
|
|
|
|
}
|
2010-06-23 00:05:32 +08:00
|
|
|
}
|
|
|
|
|
2014-08-02 08:50:16 +08:00
|
|
|
// Keep the first result in the case of a mangling collision.
|
|
|
|
auto Result = Manglings.insert(std::make_pair(Str, GD));
|
|
|
|
return FoundStr = Result.first->first();
|
2010-06-23 00:05:32 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD,
|
|
|
|
const BlockDecl *BD) {
|
2011-01-14 02:57:25 +08:00
|
|
|
MangleContext &MangleCtx = getCXXABI().getMangleContext();
|
|
|
|
const Decl *D = GD.getDecl();
|
2014-06-03 10:13:57 +08:00
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
SmallString<256> Buffer;
|
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!D)
|
2012-06-27 00:06:38 +08:00
|
|
|
MangleCtx.mangleGlobalBlock(BD,
|
|
|
|
dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
|
2014-05-09 08:08:36 +08:00
|
|
|
else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
|
2014-05-09 08:08:36 +08:00
|
|
|
else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
|
2011-01-14 02:57:25 +08:00
|
|
|
else
|
2011-02-11 07:59:36 +08:00
|
|
|
MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
|
2014-06-03 10:13:57 +08:00
|
|
|
|
2014-08-02 08:50:16 +08:00
|
|
|
auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
|
|
|
|
return Result.first->first();
|
2010-06-09 10:36:32 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
|
2010-03-20 07:29:14 +08:00
|
|
|
return getModule().getNamedValue(Name);
|
2009-02-13 08:10:09 +08:00
|
|
|
}
|
|
|
|
|
2008-03-15 01:18:18 +08:00
|
|
|
/// AddGlobalCtor - Add a function to the list that will be called before
|
|
|
|
/// main() runs.
|
2014-05-24 05:13:45 +08:00
|
|
|
void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
|
|
|
|
llvm::Constant *AssociatedData) {
|
2009-01-13 10:25:00 +08:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2014-05-24 05:13:45 +08:00
|
|
|
GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
|
2008-03-15 01:18:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-01 08:01:51 +08:00
|
|
|
/// AddGlobalDtor - Add a function to the list that will be called
|
|
|
|
/// when the module is unloaded.
|
2014-05-24 05:13:45 +08:00
|
|
|
void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) {
|
2009-01-13 10:25:00 +08:00
|
|
|
// FIXME: Type coercion of void()* types.
|
2014-06-09 10:04:02 +08:00
|
|
|
GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
|
2008-08-01 08:01:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-27 17:12:20 +08:00
|
|
|
void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
|
2016-11-19 16:17:24 +08:00
|
|
|
if (Fns.empty()) return;
|
|
|
|
|
2008-08-01 08:01:51 +08:00
|
|
|
// Ctor function type is void()*.
|
2011-05-15 09:53:33 +08:00
|
|
|
llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
2014-05-24 05:13:45 +08:00
|
|
|
// Get the type of a ctor entry, { i32, void ()*, i8* }.
|
|
|
|
llvm::StructType *CtorStructTy = llvm::StructType::get(
|
2014-12-02 06:02:27 +08:00
|
|
|
Int32Ty, llvm::PointerType::getUnqual(CtorFTy), VoidPtrTy, nullptr);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
|
|
|
// Construct the constructor and destructor arrays.
|
2016-11-29 06:18:27 +08:00
|
|
|
ConstantInitBuilder builder(*this);
|
2016-11-19 16:17:24 +08:00
|
|
|
auto ctors = builder.beginArray(CtorStructTy);
|
2015-06-11 20:33:25 +08:00
|
|
|
for (const auto &I : Fns) {
|
2016-11-19 16:17:24 +08:00
|
|
|
auto ctor = ctors.beginStruct(CtorStructTy);
|
|
|
|
ctor.addInt(Int32Ty, I.Priority);
|
|
|
|
ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
|
|
|
|
if (I.AssociatedData)
|
|
|
|
ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
|
|
|
|
else
|
|
|
|
ctor.addNullPointer(VoidPtrTy);
|
2016-11-29 06:18:30 +08:00
|
|
|
ctor.finishAndAddTo(ctors);
|
2008-03-15 01:18:18 +08:00
|
|
|
}
|
2016-11-19 16:17:24 +08:00
|
|
|
|
2016-11-20 04:12:25 +08:00
|
|
|
auto list =
|
|
|
|
ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
|
|
|
|
/*constant*/ false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage);
|
|
|
|
|
|
|
|
// The LTO linker doesn't seem to like it when we set an alignment
|
|
|
|
// on appending variables. Take it off as a workaround.
|
|
|
|
list->setAlignment(0);
|
|
|
|
|
2016-10-27 17:12:20 +08:00
|
|
|
Fns.clear();
|
2008-03-15 01:18:18 +08:00
|
|
|
}
|
|
|
|
|
2010-02-19 09:32:20 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes
|
2013-06-06 01:49:37 +08:00
|
|
|
CodeGenModule::getFunctionLinkage(GlobalDecl GD) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *D = cast<FunctionDecl>(GD.getDecl());
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
|
2010-07-30 02:15:58 +08:00
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2014-05-28 09:52:23 +08:00
|
|
|
if (isa<CXXDestructorDecl>(D) &&
|
2013-12-12 03:21:27 +08:00
|
|
|
getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
|
2014-05-28 09:52:23 +08:00
|
|
|
GD.getDtorType())) {
|
|
|
|
// Destructor variants in the Microsoft C++ ABI are always internal or
|
|
|
|
// linkonce_odr thunks emitted on an as-needed basis.
|
|
|
|
return Linkage == GVA_Internal ? llvm::GlobalValue::InternalLinkage
|
|
|
|
: llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
}
|
2013-12-12 03:21:27 +08:00
|
|
|
|
P0136R1, DR1573, DR1645, DR1715, DR1736, DR1903, DR1941, DR1959, DR1991:
Replace inheriting constructors implementation with new approach, voted into
C++ last year as a DR against C++11.
Instead of synthesizing a set of derived class constructors for each inherited
base class constructor, we make the constructors of the base class visible to
constructor lookup in the derived class, using the normal rules for
using-declarations.
For constructors, UsingShadowDecl now has a ConstructorUsingShadowDecl derived
class that tracks the requisite additional information. We create shadow
constructors (not found by name lookup) in the derived class to model the
actual initialization, and have a new expression node,
CXXInheritedCtorInitExpr, to model the initialization of a base class from such
a constructor. (This initialization is special because it performs real perfect
forwarding of arguments.)
In cases where argument forwarding is not possible (for inalloca calls,
variadic calls, and calls with callee parameter cleanup), the shadow inheriting
constructor is not emitted and instead we directly emit the initialization code
into the caller of the inherited constructor.
Note that this new model is not perfectly compatible with the old model in some
corner cases. In particular:
* if B inherits a private constructor from A, and C uses that constructor to
construct a B, then we previously required that A befriends B and B
befriends C, but the new rules require A to befriend C directly, and
* if a derived class has its own constructors (and so its implicit default
constructor is suppressed), it may still inherit a default constructor from
a base class
llvm-svn: 274049
2016-06-29 03:03:57 +08:00
|
|
|
if (isa<CXXConstructorDecl>(D) &&
|
|
|
|
cast<CXXConstructorDecl>(D)->isInheritingConstructor() &&
|
|
|
|
Context.getTargetInfo().getCXXABI().isMicrosoft()) {
|
|
|
|
// Our approach to inheriting constructors is fundamentally different from
|
|
|
|
// that used by the MS ABI, so keep our inheriting constructor thunks
|
|
|
|
// internal rather than trying to pick an unambiguous mangling for them.
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
|
|
|
}
|
|
|
|
|
2014-05-28 09:52:23 +08:00
|
|
|
return getLLVMLinkageForDeclarator(D, Linkage, /*isConstantVariable=*/false);
|
2010-02-19 09:32:20 +08:00
|
|
|
}
|
2008-06-08 23:45:52 +08:00
|
|
|
|
2015-05-29 01:44:56 +08:00
|
|
|
void CodeGenModule::setFunctionDLLStorageClass(GlobalDecl GD, llvm::Function *F) {
|
|
|
|
const auto *FD = cast<FunctionDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(FD)) {
|
|
|
|
if (getCXXABI().useThunkForDtorVariant(Dtor, GD.getDtorType())) {
|
|
|
|
// Don't dllexport/import destructor thunks.
|
|
|
|
F->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->hasAttr<DLLImportAttr>())
|
|
|
|
F->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
|
|
|
|
else if (FD->hasAttr<DLLExportAttr>())
|
|
|
|
F->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
|
|
|
|
else
|
|
|
|
F->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
|
|
|
|
}
|
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
|
2015-12-16 07:00:20 +08:00
|
|
|
llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
|
|
|
|
if (!MDS) return nullptr;
|
|
|
|
|
2016-11-23 19:20:27 +08:00
|
|
|
return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
|
2015-12-16 07:00:20 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 23:26:12 +08:00
|
|
|
void CodeGenModule::setFunctionDefinitionAttributes(const FunctionDecl *D,
|
|
|
|
llvm::Function *F) {
|
|
|
|
setNonAliasAttributes(D, F);
|
2008-06-08 23:45:52 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 15:08:30 +08:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D,
|
2009-09-09 23:08:12 +08:00
|
|
|
const CGFunctionInfo &Info,
|
2009-04-14 15:08:30 +08:00
|
|
|
llvm::Function *F) {
|
2009-09-12 08:59:20 +08:00
|
|
|
unsigned CallingConv;
|
2008-09-26 05:02:23 +08:00
|
|
|
AttributeListType AttributeList;
|
2016-01-06 22:35:46 +08:00
|
|
|
ConstructAttributeList(F->getName(), Info, D, AttributeList, CallingConv,
|
|
|
|
false);
|
2012-12-08 07:17:26 +08:00
|
|
|
F->setAttributes(llvm::AttributeSet::get(getLLVMContext(), AttributeList));
|
2009-09-12 08:59:20 +08:00
|
|
|
F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2011-10-02 09:16:38 +08:00
|
|
|
/// Determines whether the language options require us to model
|
|
|
|
/// unwind exceptions. We treat -fexceptions as mandating this
|
|
|
|
/// except under the fragile ObjC ABI with only ObjC exceptions
|
|
|
|
/// enabled. This means, for example, that C with -fexceptions
|
|
|
|
/// enables this.
|
2012-03-11 15:00:24 +08:00
|
|
|
static bool hasUnwindExceptions(const LangOptions &LangOpts) {
|
2011-10-02 09:16:38 +08:00
|
|
|
// If exceptions are completely disabled, obviously this is false.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!LangOpts.Exceptions) return false;
|
2011-10-02 09:16:38 +08:00
|
|
|
|
|
|
|
// If C++ exceptions are enabled, this is true.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CXXExceptions) return true;
|
2011-10-02 09:16:38 +08:00
|
|
|
|
|
|
|
// If ObjC exceptions are enabled, this depends on the ABI.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.ObjCExceptions) {
|
2012-07-04 04:49:52 +08:00
|
|
|
return LangOpts.ObjCRuntime.hasUnwindExceptions();
|
2011-10-02 09:16:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
|
|
|
|
llvm::Function *F) {
|
2013-05-11 20:45:37 +08:00
|
|
|
llvm::AttrBuilder B;
|
|
|
|
|
2011-05-25 11:44:55 +08:00
|
|
|
if (CodeGenOpts.UnwindTables)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::UWTable);
|
2011-05-25 11:44:55 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!hasUnwindExceptions(LangOpts))
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::NoUnwind);
|
2008-10-28 08:17:57 +08:00
|
|
|
|
2015-10-09 03:30:57 +08:00
|
|
|
if (LangOpts.getStackProtector() == LangOptions::SSPOn)
|
|
|
|
B.addAttribute(llvm::Attribute::StackProtect);
|
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
|
|
|
|
B.addAttribute(llvm::Attribute::StackProtectStrong);
|
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
|
|
|
|
B.addAttribute(llvm::Attribute::StackProtectReq);
|
|
|
|
|
|
|
|
if (!D) {
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
// If we don't have a declaration to control inlining, the function isn't
|
|
|
|
// explicitly marked as alwaysinline for semantic reasons, and inlining is
|
|
|
|
// disabled, mark the function as noinline.
|
|
|
|
if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
|
|
|
|
CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
|
2015-10-09 03:30:57 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(
|
|
|
|
F->getContext(),
|
|
|
|
llvm::AttributeSet::FunctionIndex, B));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
if (D->hasAttr<OptimizeNoneAttr>()) {
|
|
|
|
B.addAttribute(llvm::Attribute::OptimizeNone);
|
|
|
|
|
|
|
|
// OptimizeNone implies noinline; we should not be inlining such functions.
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
|
|
|
|
"OptimizeNone and AlwaysInline on same function!");
|
|
|
|
|
|
|
|
// We still need to handle naked functions even though optnone subsumes
|
|
|
|
// much of their semantics.
|
|
|
|
if (D->hasAttr<NakedAttr>())
|
|
|
|
B.addAttribute(llvm::Attribute::Naked);
|
|
|
|
|
|
|
|
// OptimizeNone wins over OptimizeForSize and MinSize.
|
|
|
|
F->removeFnAttr(llvm::Attribute::OptimizeForSize);
|
|
|
|
F->removeFnAttr(llvm::Attribute::MinSize);
|
|
|
|
} else if (D->hasAttr<NakedAttr>()) {
|
2011-08-23 07:55:33 +08:00
|
|
|
// Naked implies noinline: we should not be inlining such functions.
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::Naked);
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
2014-02-23 00:59:24 +08:00
|
|
|
} else if (D->hasAttr<NoDuplicateAttr>()) {
|
|
|
|
B.addAttribute(llvm::Attribute::NoDuplicate);
|
2013-05-11 20:45:37 +08:00
|
|
|
} else if (D->hasAttr<NoInlineAttr>()) {
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
2014-02-25 17:53:29 +08:00
|
|
|
} else if (D->hasAttr<AlwaysInlineAttr>() &&
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
!F->hasFnAttribute(llvm::Attribute::NoInline)) {
|
2013-05-11 20:45:37 +08:00
|
|
|
// (noinline wins over always_inline, and we can't specify both in IR)
|
2015-09-15 05:35:16 +08:00
|
|
|
B.addAttribute(llvm::Attribute::AlwaysInline);
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
} else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
|
|
|
|
// If we're not inlining, then force everything that isn't always_inline to
|
|
|
|
// carry an explicit noinline attribute.
|
|
|
|
if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
} else {
|
|
|
|
// Otherwise, propagate the inline hint attribute and potentially use its
|
|
|
|
// absence to mark things as noinline.
|
|
|
|
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (any_of(FD->redecls(), [&](const FunctionDecl *Redecl) {
|
|
|
|
return Redecl->isInlineSpecified();
|
|
|
|
})) {
|
|
|
|
B.addAttribute(llvm::Attribute::InlineHint);
|
|
|
|
} else if (CodeGenOpts.getInlining() ==
|
|
|
|
CodeGenOptions::OnlyHintInlining &&
|
|
|
|
!FD->isInlined() &&
|
|
|
|
!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
|
|
|
|
B.addAttribute(llvm::Attribute::NoInline);
|
|
|
|
}
|
|
|
|
}
|
2011-08-23 07:55:33 +08:00
|
|
|
}
|
2010-09-30 02:20:25 +08:00
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
// Add other optimization related attributes if we are optimizing this
|
|
|
|
// function.
|
|
|
|
if (!D->hasAttr<OptimizeNoneAttr>()) {
|
|
|
|
if (D->hasAttr<ColdAttr>()) {
|
2014-12-12 04:14:04 +08:00
|
|
|
B.addAttribute(llvm::Attribute::OptimizeForSize);
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
B.addAttribute(llvm::Attribute::Cold);
|
|
|
|
}
|
2012-05-13 05:10:52 +08:00
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
if (D->hasAttr<MinSizeAttr>())
|
|
|
|
B.addAttribute(llvm::Attribute::MinSize);
|
|
|
|
}
|
2012-09-29 06:46:07 +08:00
|
|
|
|
2013-05-11 20:45:37 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(
|
|
|
|
F->getContext(), llvm::AttributeSet::FunctionIndex, B));
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
|
|
|
|
if (alignment)
|
|
|
|
F->setAlignment(alignment);
|
|
|
|
|
2015-09-04 06:51:53 +08:00
|
|
|
// Some C++ ABIs require 2-byte alignment for member functions, in order to
|
|
|
|
// reserve a bit for differentiating between virtual and non-virtual member
|
|
|
|
// functions. If the current target's C++ ABI requires this and this is a
|
|
|
|
// member function, set its alignment accordingly.
|
|
|
|
if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
|
|
|
|
if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
|
|
|
|
F->setAlignment(2);
|
|
|
|
}
|
2016-11-01 06:28:10 +08:00
|
|
|
|
|
|
|
// In the cross-dso CFI mode, we want !type attributes on definitions only.
|
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso)
|
|
|
|
if (auto *FD = dyn_cast<FunctionDecl>(D))
|
|
|
|
CreateFunctionTypeMetadata(FD, F);
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::SetCommonAttributes(const Decl *D,
|
2009-04-14 16:05:55 +08:00
|
|
|
llvm::GlobalValue *GV) {
|
2015-10-09 04:26:34 +08:00
|
|
|
if (const auto *ND = dyn_cast_or_null<NamedDecl>(D))
|
2011-01-30 03:41:00 +08:00
|
|
|
setGlobalVisibility(GV, ND);
|
2010-10-23 05:05:15 +08:00
|
|
|
else
|
|
|
|
GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2015-10-09 04:26:34 +08:00
|
|
|
if (D && D->hasAttr<UsedAttr>())
|
2014-03-07 06:15:10 +08:00
|
|
|
addUsedGlobal(GV);
|
2014-05-06 04:21:03 +08:00
|
|
|
}
|
|
|
|
|
2014-09-20 06:06:24 +08:00
|
|
|
void CodeGenModule::setAliasAttributes(const Decl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
|
|
|
SetCommonAttributes(D, GV);
|
|
|
|
|
|
|
|
// Process the dllexport attribute based on whether the original definition
|
|
|
|
// (not necessarily the aliasee) was exported.
|
|
|
|
if (D->hasAttr<DLLExportAttr>())
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
|
|
|
}
|
|
|
|
|
2014-05-06 04:21:03 +08:00
|
|
|
void CodeGenModule::setNonAliasAttributes(const Decl *D,
|
2014-05-14 02:45:53 +08:00
|
|
|
llvm::GlobalObject *GO) {
|
|
|
|
SetCommonAttributes(D, GO);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2015-10-09 04:26:34 +08:00
|
|
|
if (D)
|
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
|
|
|
|
GO->setSection(SA->getName());
|
2010-01-10 20:58:08 +08:00
|
|
|
|
2015-06-06 06:03:00 +08:00
|
|
|
getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
|
2009-04-14 16:05:55 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 08:48:04 +08:00
|
|
|
void CodeGenModule::SetInternalFunctionAttributes(const Decl *D,
|
|
|
|
llvm::Function *F,
|
|
|
|
const CGFunctionInfo &FI) {
|
|
|
|
SetLLVMFunctionAttributes(D, FI, F);
|
|
|
|
SetLLVMFunctionAttributesForDefinition(D, F);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
|
|
|
F->setLinkage(llvm::Function::InternalLinkage);
|
|
|
|
|
2014-05-06 04:21:03 +08:00
|
|
|
setNonAliasAttributes(D, F);
|
2008-09-05 07:41:35 +08:00
|
|
|
}
|
|
|
|
|
2014-04-26 01:07:16 +08:00
|
|
|
static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV,
|
|
|
|
const NamedDecl *ND) {
|
|
|
|
// Set linkage and visibility in case we never see a definition.
|
|
|
|
LinkageInfo LV = ND->getLinkageAndVisibility();
|
|
|
|
if (LV.getLinkage() != ExternalLinkage) {
|
|
|
|
// Don't set internal linkage on declarations.
|
|
|
|
} else {
|
|
|
|
if (ND->hasAttr<DLLImportAttr>()) {
|
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
|
|
|
|
} else if (ND->hasAttr<DLLExportAttr>()) {
|
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
|
|
|
} else if (ND->hasAttr<WeakAttr>() || ND->isWeakImported()) {
|
|
|
|
// "extern_weak" is overloaded in LLVM; we probably should have
|
|
|
|
// separate linkage types for this.
|
|
|
|
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set visibility on a declaration only if it's explicit.
|
|
|
|
if (LV.isVisibilityExplicit())
|
|
|
|
GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
void CodeGenModule::CreateFunctionTypeMetadata(const FunctionDecl *FD,
|
|
|
|
llvm::Function *F) {
|
2015-12-16 07:00:20 +08:00
|
|
|
// Only if we are checking indirect calls.
|
|
|
|
if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Non-static class methods are handled via vtable pointer checks elsewhere.
|
|
|
|
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Additionally, if building with cross-DSO support...
|
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso) {
|
|
|
|
// Skip available_externally functions. They won't be codegen'ed in the
|
|
|
|
// current module anyway.
|
|
|
|
if (getContext().GetGVALinkageForFunction(FD) == GVA_AvailableExternally)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
|
2016-06-25 05:21:46 +08:00
|
|
|
F->addTypeMetadata(0, MD);
|
2015-12-16 07:00:20 +08:00
|
|
|
|
|
|
|
// Emit a hash-based bit set entry for cross-DSO calls.
|
2016-06-25 05:21:46 +08:00
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso)
|
|
|
|
if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
|
|
|
|
F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
|
2015-12-16 07:00:20 +08:00
|
|
|
}
|
|
|
|
|
2014-11-01 13:42:23 +08:00
|
|
|
void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
|
|
|
|
bool IsIncompleteFunction,
|
|
|
|
bool IsThunk) {
|
2015-05-21 01:17:45 +08:00
|
|
|
if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
|
2011-04-06 20:29:04 +08:00
|
|
|
// If this is an intrinsic function, set the function's attributes
|
|
|
|
// to the intrinsic's attributes.
|
2015-05-21 01:17:45 +08:00
|
|
|
F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
|
2011-04-06 20:29:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *FD = cast<FunctionDecl>(GD.getDecl());
|
2010-02-06 10:44:09 +08:00
|
|
|
|
2009-05-26 09:22:57 +08:00
|
|
|
if (!IsIncompleteFunction)
|
2012-02-17 11:33:10 +08:00
|
|
|
SetLLVMFunctionAttributes(FD, getTypes().arrangeGlobalDeclaration(GD), F);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-04-01 09:38:16 +08:00
|
|
|
// Add the Returned attribute for "this", except for iOS 5 and earlier
|
|
|
|
// where substantial code, including the libstdc++ dylib, was compiled with
|
|
|
|
// GCC and does not actually return "this".
|
2014-11-01 13:42:23 +08:00
|
|
|
if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
|
2016-09-14 23:17:46 +08:00
|
|
|
!(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
|
2013-07-01 04:40:16 +08:00
|
|
|
assert(!F->arg_empty() &&
|
|
|
|
F->arg_begin()->getType()
|
|
|
|
->canLosslesslyBitCastTo(F->getReturnType()) &&
|
|
|
|
"unexpected this return");
|
|
|
|
F->addAttribute(1, llvm::Attribute::Returned);
|
|
|
|
}
|
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
// Only a few attributes are set on declarations; these may later be
|
|
|
|
// overridden by a definition.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-04-26 01:07:16 +08:00
|
|
|
setLinkageAndVisibilityForGV(F, FD);
|
2009-04-14 16:05:55 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = FD->getAttr<SectionAttr>())
|
2009-04-14 16:05:55 +08:00
|
|
|
F->setSection(SA->getName());
|
2013-07-22 07:12:18 +08:00
|
|
|
|
2016-04-08 05:46:12 +08:00
|
|
|
if (FD->isReplaceableGlobalAllocationFunction()) {
|
|
|
|
// A replaceable global allocation function does not act like a builtin by
|
|
|
|
// default, only if it is invoked by a new-expression or delete-expression.
|
2013-07-22 07:12:18 +08:00
|
|
|
F->addAttribute(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::Attribute::NoBuiltin);
|
2015-09-10 10:17:40 +08:00
|
|
|
|
2016-04-08 05:46:12 +08:00
|
|
|
// A sane operator new returns a non-aliasing pointer.
|
|
|
|
// FIXME: Also add NonNull attribute to the return value
|
|
|
|
// for the non-nothrow forms?
|
|
|
|
auto Kind = FD->getDeclName().getCXXOverloadedOperator();
|
|
|
|
if (getCodeGenOpts().AssumeSaneOperatorNew &&
|
|
|
|
(Kind == OO_New || Kind == OO_Array_New))
|
|
|
|
F->addAttribute(llvm::AttributeSet::ReturnIndex,
|
|
|
|
llvm::Attribute::NoAlias);
|
|
|
|
}
|
|
|
|
|
2016-03-15 02:41:59 +08:00
|
|
|
if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
|
2016-06-15 05:02:05 +08:00
|
|
|
F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2016-03-15 02:41:59 +08:00
|
|
|
else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
|
|
|
|
if (MD->isVirtual())
|
2016-06-15 05:02:05 +08:00
|
|
|
F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2016-03-15 02:41:59 +08:00
|
|
|
|
2016-11-01 06:28:10 +08:00
|
|
|
// Don't emit entries for function declarations in the cross-DSO mode. This
|
|
|
|
// is handled with better precision by the receiving DSO.
|
|
|
|
if (!CodeGenOpts.SanitizeCfiCrossDso)
|
|
|
|
CreateFunctionTypeMetadata(FD, F);
|
2008-09-09 07:44:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 06:15:10 +08:00
|
|
|
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!GV->isDeclaration() &&
|
2009-02-14 04:29:50 +08:00
|
|
|
"Only globals with definition can force usage.");
|
2015-05-30 03:42:19 +08:00
|
|
|
LLVMUsed.emplace_back(GV);
|
2009-02-14 04:29:50 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 06:15:10 +08:00
|
|
|
void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
|
|
|
|
assert(!GV->isDeclaration() &&
|
|
|
|
"Only globals with definition can force usage.");
|
2015-05-30 03:42:19 +08:00
|
|
|
LLVMCompilerUsed.emplace_back(GV);
|
2014-03-07 06:15:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitUsed(CodeGenModule &CGM, StringRef Name,
|
|
|
|
std::vector<llvm::WeakVH> &List) {
|
2009-02-14 04:29:50 +08:00
|
|
|
// Don't create llvm.used if there is no need.
|
2014-03-07 06:15:10 +08:00
|
|
|
if (List.empty())
|
2009-02-14 04:29:50 +08:00
|
|
|
return;
|
|
|
|
|
2014-03-07 06:15:10 +08:00
|
|
|
// Convert List to what ConstantArray needs.
|
2012-02-07 06:16:34 +08:00
|
|
|
SmallVector<llvm::Constant*, 8> UsedArray;
|
2014-03-07 06:15:10 +08:00
|
|
|
UsedArray.resize(List.size());
|
|
|
|
for (unsigned i = 0, e = List.size(); i != e; ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
UsedArray[i] =
|
2015-02-03 05:05:49 +08:00
|
|
|
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
|
|
|
|
cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
|
2009-04-01 06:37:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-24 05:47:46 +08:00
|
|
|
if (UsedArray.empty())
|
|
|
|
return;
|
2014-03-07 06:15:10 +08:00
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
|
|
|
|
llvm::ConstantArray::get(ATy, UsedArray), Name);
|
2009-02-14 04:29:50 +08:00
|
|
|
|
|
|
|
GV->setSection("llvm.metadata");
|
|
|
|
}
|
|
|
|
|
2014-03-07 06:15:10 +08:00
|
|
|
void CodeGenModule::emitLLVMUsed() {
|
|
|
|
emitUsed(*this, "llvm.used", LLVMUsed);
|
|
|
|
emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
void CodeGenModule::AppendLinkerOptions(StringRef Opts) {
|
2014-12-10 02:39:32 +08:00
|
|
|
auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
|
2013-05-08 21:44:39 +08:00
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-06-04 10:07:14 +08:00
|
|
|
void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
|
|
|
|
llvm::SmallString<32> Opt;
|
|
|
|
getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
|
2014-12-10 02:39:32 +08:00
|
|
|
auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
|
2013-06-04 10:07:14 +08:00
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:44:39 +08:00
|
|
|
void CodeGenModule::AddDependentLib(StringRef Lib) {
|
|
|
|
llvm::SmallString<24> Opt;
|
|
|
|
getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt);
|
2014-12-10 02:39:32 +08:00
|
|
|
auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
|
2013-05-08 21:44:39 +08:00
|
|
|
LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:53:57 +08:00
|
|
|
/// \brief Add link options implied by the given module, including modules
|
|
|
|
/// it depends on, using a postorder walk.
|
2014-12-10 02:39:32 +08:00
|
|
|
static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod,
|
|
|
|
SmallVectorImpl<llvm::Metadata *> &Metadata,
|
2013-01-15 04:53:57 +08:00
|
|
|
llvm::SmallPtrSet<Module *, 16> &Visited) {
|
|
|
|
// Import this module's parent.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Mod->Parent && Visited.insert(Mod->Parent).second) {
|
2013-05-08 21:44:39 +08:00
|
|
|
addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Import this module's dependencies.
|
|
|
|
for (unsigned I = Mod->Imports.size(); I > 0; --I) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Visited.insert(Mod->Imports[I - 1]).second)
|
2013-05-08 21:44:39 +08:00
|
|
|
addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add linker options to link against the libraries/frameworks
|
|
|
|
// described by this module.
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::LLVMContext &Context = CGM.getLLVMContext();
|
2013-01-15 04:53:57 +08:00
|
|
|
for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
|
2013-05-08 21:44:39 +08:00
|
|
|
// Link against a framework. Frameworks are currently Darwin only, so we
|
|
|
|
// don't to ask TargetCodeGenInfo for the spelling of the linker option.
|
2013-01-15 04:53:57 +08:00
|
|
|
if (Mod->LinkLibraries[I-1].IsFramework) {
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Metadata *Args[2] = {
|
|
|
|
llvm::MDString::get(Context, "-framework"),
|
|
|
|
llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)};
|
2013-01-15 04:53:57 +08:00
|
|
|
|
|
|
|
Metadata.push_back(llvm::MDNode::get(Context, Args));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link against a library.
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::SmallString<24> Opt;
|
|
|
|
CGM.getTargetCodeGenInfo().getDependentLibraryOption(
|
|
|
|
Mod->LinkLibraries[I-1].Library, Opt);
|
2014-12-10 02:39:32 +08:00
|
|
|
auto *OptString = llvm::MDString::get(Context, Opt);
|
2013-01-15 04:53:57 +08:00
|
|
|
Metadata.push_back(llvm::MDNode::get(Context, OptString));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitModuleLinkOptions() {
|
|
|
|
// Collect the set of all of the modules we want to visit to emit link
|
|
|
|
// options, which is essentially the imported modules and all of their
|
|
|
|
// non-explicit child modules.
|
|
|
|
llvm::SetVector<clang::Module *> LinkModules;
|
|
|
|
llvm::SmallPtrSet<clang::Module *, 16> Visited;
|
|
|
|
SmallVector<clang::Module *, 16> Stack;
|
|
|
|
|
|
|
|
// Seed the stack with imported modules.
|
2017-01-12 02:47:38 +08:00
|
|
|
for (Module *M : ImportedModules) {
|
|
|
|
// Do not add any link flags when an implementation TU of a module imports
|
|
|
|
// a header of that same module.
|
|
|
|
if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
|
|
|
|
!getLangOpts().isCompilingModule())
|
|
|
|
continue;
|
2015-06-11 20:33:25 +08:00
|
|
|
if (Visited.insert(M).second)
|
|
|
|
Stack.push_back(M);
|
2017-01-12 02:47:38 +08:00
|
|
|
}
|
2013-01-15 04:53:57 +08:00
|
|
|
|
|
|
|
// Find all of the modules to import, making a little effort to prune
|
|
|
|
// non-leaf modules.
|
|
|
|
while (!Stack.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
clang::Module *Mod = Stack.pop_back_val();
|
2013-01-15 04:53:57 +08:00
|
|
|
|
|
|
|
bool AnyChildren = false;
|
|
|
|
|
|
|
|
// Visit the submodules of this module.
|
|
|
|
for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
|
|
|
|
SubEnd = Mod->submodule_end();
|
|
|
|
Sub != SubEnd; ++Sub) {
|
|
|
|
// Skip explicit children; they need to be explicitly imported to be
|
|
|
|
// linked against.
|
|
|
|
if ((*Sub)->IsExplicit)
|
|
|
|
continue;
|
|
|
|
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Visited.insert(*Sub).second) {
|
2013-01-15 04:53:57 +08:00
|
|
|
Stack.push_back(*Sub);
|
|
|
|
AnyChildren = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find any children, so add this module to the list of
|
|
|
|
// modules to link against.
|
|
|
|
if (!AnyChildren) {
|
|
|
|
LinkModules.insert(Mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add link options for all of the imported modules in reverse topological
|
2013-05-08 21:44:39 +08:00
|
|
|
// order. We don't do anything to try to order import link flags with respect
|
|
|
|
// to linker options inserted by things like #pragma comment().
|
2014-12-10 02:39:32 +08:00
|
|
|
SmallVector<llvm::Metadata *, 16> MetadataArgs;
|
2013-01-15 04:53:57 +08:00
|
|
|
Visited.clear();
|
2015-06-11 20:33:25 +08:00
|
|
|
for (Module *M : LinkModules)
|
|
|
|
if (Visited.insert(M).second)
|
|
|
|
addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
|
2013-01-17 09:35:06 +08:00
|
|
|
std::reverse(MetadataArgs.begin(), MetadataArgs.end());
|
2013-05-08 21:44:39 +08:00
|
|
|
LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
|
2013-01-15 04:53:57 +08:00
|
|
|
|
2013-01-17 09:35:06 +08:00
|
|
|
// Add the linker options metadata flag.
|
|
|
|
getModule().addModuleFlag(llvm::Module::AppendUnique, "Linker Options",
|
2013-05-08 21:44:39 +08:00
|
|
|
llvm::MDNode::get(getLLVMContext(),
|
|
|
|
LinkerOptionsMetadata));
|
2013-01-15 04:53:57 +08:00
|
|
|
}
|
|
|
|
|
2009-02-14 04:29:50 +08:00
|
|
|
void CodeGenModule::EmitDeferred() {
|
2009-03-21 17:44:56 +08:00
|
|
|
// Emit code for any potentially referenced deferred decls. Since a
|
|
|
|
// previously unused static decl may become used during the generation of code
|
2011-07-18 13:26:13 +08:00
|
|
|
// for a static function, iterate until no changes are made.
|
2010-03-10 10:19:29 +08:00
|
|
|
|
2015-01-22 08:24:57 +08:00
|
|
|
if (!DeferredVTables.empty()) {
|
|
|
|
EmitDeferredVTables();
|
|
|
|
|
2016-01-29 09:35:53 +08:00
|
|
|
// Emitting a vtable doesn't directly cause more vtables to
|
2015-01-22 08:24:57 +08:00
|
|
|
// become deferred, although it can cause functions to be
|
2016-01-29 09:35:53 +08:00
|
|
|
// emitted that then need those vtables.
|
2015-01-22 08:24:57 +08:00
|
|
|
assert(DeferredVTables.empty());
|
|
|
|
}
|
2010-03-10 10:19:29 +08:00
|
|
|
|
2016-01-29 09:35:53 +08:00
|
|
|
// Stop if we're out of both deferred vtables and deferred declarations.
|
2015-01-22 08:24:57 +08:00
|
|
|
if (DeferredDeclsToEmit.empty())
|
|
|
|
return;
|
2013-01-26 06:31:03 +08:00
|
|
|
|
2015-01-22 08:24:57 +08:00
|
|
|
// Grab the list of decls to emit. If EmitGlobalDefinition schedules more
|
|
|
|
// work, it will not interfere with this.
|
|
|
|
std::vector<DeferredGlobal> CurDeclsToEmit;
|
|
|
|
CurDeclsToEmit.swap(DeferredDeclsToEmit);
|
|
|
|
|
|
|
|
for (DeferredGlobal &G : CurDeclsToEmit) {
|
2013-12-09 22:59:08 +08:00
|
|
|
GlobalDecl D = G.GD;
|
2015-01-22 08:24:57 +08:00
|
|
|
G.GV = nullptr;
|
2009-03-21 17:44:56 +08:00
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
// We should call GetAddrOfGlobal with IsForDefinition set to true in order
|
|
|
|
// to get GlobalValue with exactly the type we need, not something that
|
|
|
|
// might had been created for another decl with the same mangled name but
|
|
|
|
// different type.
|
2016-01-14 18:41:16 +08:00
|
|
|
llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
|
2016-12-01 07:25:13 +08:00
|
|
|
GetAddrOfGlobal(D, ForDefinition));
|
2016-01-14 18:41:16 +08:00
|
|
|
|
|
|
|
// In case of different address spaces, we may still get a cast, even with
|
|
|
|
// IsForDefinition equal to true. Query mangled names table to get
|
|
|
|
// GlobalValue.
|
|
|
|
if (!GV)
|
|
|
|
GV = GetGlobalValue(getMangledName(D));
|
|
|
|
|
|
|
|
// Make sure GetGlobalValue returned non-null.
|
|
|
|
assert(GV);
|
2015-01-10 09:19:48 +08:00
|
|
|
|
2010-05-27 09:45:30 +08:00
|
|
|
// Check to see if we've already emitted this. This is necessary
|
|
|
|
// for a couple of reasons: first, decls can end up in the
|
|
|
|
// deferred-decls queue multiple times, and second, decls can end
|
|
|
|
// up with definitions in unusual ways (e.g. by an extern inline
|
|
|
|
// function acquiring a strong function redefinition). Just
|
|
|
|
// ignore these cases.
|
2016-01-14 18:41:16 +08:00
|
|
|
if (!GV->isDeclaration())
|
2009-03-21 17:44:56 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:44:56 +08:00
|
|
|
// Otherwise, emit the definition and move on to the next one.
|
2013-12-10 00:01:03 +08:00
|
|
|
EmitGlobalDefinition(D, GV);
|
2015-01-22 08:24:57 +08:00
|
|
|
|
|
|
|
// If we found out that we need to emit more decls, do that recursively.
|
|
|
|
// This has the advantage that the decls are emitted in a DFS and related
|
|
|
|
// ones are close together, which is convenient for testing.
|
|
|
|
if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
|
|
|
|
EmitDeferred();
|
|
|
|
assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
|
|
|
|
}
|
2009-03-21 17:44:56 +08:00
|
|
|
}
|
2007-05-28 09:07:47 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
void CodeGenModule::EmitGlobalAnnotations() {
|
|
|
|
if (Annotations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a new global variable for the ConstantStruct in the Module.
|
|
|
|
llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
|
|
|
|
Annotations[0]->getType(), Annotations.size()), Annotations);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
|
|
|
Array, "llvm.global.annotations");
|
2011-09-10 06:41:49 +08:00
|
|
|
gv->setSection(AnnotationSection);
|
|
|
|
}
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
|
2013-11-11 00:55:11 +08:00
|
|
|
llvm::Constant *&AStr = AnnotationStrings[Str];
|
|
|
|
if (AStr)
|
|
|
|
return AStr;
|
2011-09-10 06:41:49 +08:00
|
|
|
|
|
|
|
// Not found yet, create a new global.
|
2012-02-05 10:30:40 +08:00
|
|
|
llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *gv =
|
|
|
|
new llvm::GlobalVariable(getModule(), s->getType(), true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage, s, ".str");
|
2011-09-10 06:41:49 +08:00
|
|
|
gv->setSection(AnnotationSection);
|
2016-06-15 05:02:05 +08:00
|
|
|
gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2013-11-11 00:55:11 +08:00
|
|
|
AStr = gv;
|
2011-09-10 06:41:49 +08:00
|
|
|
return gv;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) {
|
|
|
|
SourceManager &SM = getContext().getSourceManager();
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
if (PLoc.isValid())
|
|
|
|
return EmitAnnotationString(PLoc.getFilename());
|
|
|
|
return EmitAnnotationString(SM.getBufferName(Loc));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) {
|
|
|
|
SourceManager &SM = getContext().getSourceManager();
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(L);
|
|
|
|
unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
|
|
|
|
SM.getExpansionLineNumber(L);
|
|
|
|
return llvm::ConstantInt::get(Int32Ty, LineNo);
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
|
2008-04-19 12:17:09 +08:00
|
|
|
const AnnotateAttr *AA,
|
2011-09-10 06:41:49 +08:00
|
|
|
SourceLocation L) {
|
|
|
|
// Get the globals for file name, annotation, and the line number.
|
|
|
|
llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
|
|
|
|
*UnitGV = EmitAnnotationUnit(L),
|
|
|
|
*LineNoCst = EmitAnnotationLineNo(L);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2009-04-15 06:41:13 +08:00
|
|
|
// Create the ConstantStruct for the global annotation.
|
2008-04-19 12:17:09 +08:00
|
|
|
llvm::Constant *Fields[4] = {
|
2011-09-10 06:41:49 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
|
|
|
|
llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
|
|
|
|
LineNoCst
|
2008-04-19 12:17:09 +08:00
|
|
|
};
|
2011-06-20 12:01:35 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(Fields);
|
2008-04-19 12:17:09 +08:00
|
|
|
}
|
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
|
|
|
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
|
|
|
|
// Get the struct elements for these annotations.
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *I : D->specific_attrs<AnnotateAttr>())
|
|
|
|
Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
|
2011-09-10 06:41:49 +08:00
|
|
|
}
|
|
|
|
|
SanitizerBlacklist: blacklist functions by their source location.
This commit changes the way we blacklist functions in ASan, TSan,
MSan and UBSan. We used to treat function as "blacklisted"
and turned off instrumentation in it in two cases:
1) Function is explicitly blacklisted by its mangled name.
This part is not changed.
2) Function is located in llvm::Module, whose identifier is
contained in the list of blacklisted sources. This is completely
wrong, as llvm::Module may not correspond to the actual source
file function is defined in. Also, function can be defined in
a header, in which case user had to blacklist the .cpp file
this header was #include'd into, not the header itself.
Such functions could cause other problems - for instance, if the
header was included in multiple source files, compiled
separately and linked into a single executable, we could end up
with both instrumented and non-instrumented version of the same
function participating in the same link.
After this change we will make blacklisting decision based on
the SourceLocation of a function definition. If a function is
not explicitly defined in the source file, (for example, the
function is compiler-generated and responsible for
initialization/destruction of a global variable), then it will
be blacklisted if the corresponding global variable is defined
in blacklisted source file, and will be instrumented otherwise.
After this commit, the active users of blacklist files may have
to revisit them. This is a backwards-incompatible change, but
I don't think it's possible or makes sense to support the
old incorrect behavior.
I plan to make similar change for blacklisting GlobalVariables
(which is ASan-specific).
llvm-svn: 219997
2014-10-17 08:20:19 +08:00
|
|
|
bool CodeGenModule::isInSanitizerBlacklist(llvm::Function *Fn,
|
|
|
|
SourceLocation Loc) const {
|
|
|
|
const auto &SanitizerBL = getContext().getSanitizerBlacklist();
|
|
|
|
// Blacklist by function name.
|
|
|
|
if (SanitizerBL.isBlacklistedFunction(Fn->getName()))
|
|
|
|
return true;
|
|
|
|
// Blacklist by location.
|
2015-10-03 13:15:57 +08:00
|
|
|
if (Loc.isValid())
|
SanitizerBlacklist: blacklist functions by their source location.
This commit changes the way we blacklist functions in ASan, TSan,
MSan and UBSan. We used to treat function as "blacklisted"
and turned off instrumentation in it in two cases:
1) Function is explicitly blacklisted by its mangled name.
This part is not changed.
2) Function is located in llvm::Module, whose identifier is
contained in the list of blacklisted sources. This is completely
wrong, as llvm::Module may not correspond to the actual source
file function is defined in. Also, function can be defined in
a header, in which case user had to blacklist the .cpp file
this header was #include'd into, not the header itself.
Such functions could cause other problems - for instance, if the
header was included in multiple source files, compiled
separately and linked into a single executable, we could end up
with both instrumented and non-instrumented version of the same
function participating in the same link.
After this change we will make blacklisting decision based on
the SourceLocation of a function definition. If a function is
not explicitly defined in the source file, (for example, the
function is compiler-generated and responsible for
initialization/destruction of a global variable), then it will
be blacklisted if the corresponding global variable is defined
in blacklisted source file, and will be instrumented otherwise.
After this commit, the active users of blacklist files may have
to revisit them. This is a backwards-incompatible change, but
I don't think it's possible or makes sense to support the
old incorrect behavior.
I plan to make similar change for blacklisting GlobalVariables
(which is ASan-specific).
llvm-svn: 219997
2014-10-17 08:20:19 +08:00
|
|
|
return SanitizerBL.isBlacklistedLocation(Loc);
|
|
|
|
// If location is unknown, this may be a compiler-generated function. Assume
|
|
|
|
// it's located in the main file.
|
|
|
|
auto &SM = Context.getSourceManager();
|
|
|
|
if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
|
|
|
|
return SanitizerBL.isBlacklistedFile(MainFile->getName());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-18 06:37:33 +08:00
|
|
|
bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV,
|
|
|
|
SourceLocation Loc, QualType Ty,
|
|
|
|
StringRef Category) const {
|
2015-06-19 20:19:07 +08:00
|
|
|
// For now globals can be blacklisted only in ASan and KASan.
|
|
|
|
if (!LangOpts.Sanitize.hasOneOf(
|
|
|
|
SanitizerKind::Address | SanitizerKind::KernelAddress))
|
2014-10-18 06:37:33 +08:00
|
|
|
return false;
|
|
|
|
const auto &SanitizerBL = getContext().getSanitizerBlacklist();
|
|
|
|
if (SanitizerBL.isBlacklistedGlobal(GV->getName(), Category))
|
|
|
|
return true;
|
|
|
|
if (SanitizerBL.isBlacklistedLocation(Loc, Category))
|
|
|
|
return true;
|
|
|
|
// Check global type.
|
|
|
|
if (!Ty.isNull()) {
|
|
|
|
// Drill down the array types: if global variable of a fixed type is
|
|
|
|
// blacklisted, we also don't instrument arrays of them.
|
|
|
|
while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
|
|
|
|
Ty = AT->getElementType();
|
|
|
|
Ty = Ty.getCanonicalType().getUnqualifiedType();
|
|
|
|
// We allow to blacklist only record types (classes, structs etc.)
|
|
|
|
if (Ty->isRecordType()) {
|
|
|
|
std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
|
|
|
|
if (SanitizerBL.isBlacklistedType(TypeStr, Category))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-10 09:19:48 +08:00
|
|
|
bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
|
2010-07-30 02:15:58 +08:00
|
|
|
// Never defer when EmitAllDecls is specified.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.EmitAllDecls)
|
2015-01-10 09:19:48 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return getContext().DeclMustBeEmitted(Global);
|
|
|
|
}
|
When a function or variable somehow depends on a type or declaration
that is in an anonymous namespace, give that function or variable
internal linkage.
This change models an oddity of the C++ standard, where names declared
in an anonymous namespace have external linkage but, because anonymous
namespace are really "uniquely-named" namespaces, the names cannot be
referenced from other translation units. That means that they have
external linkage for semantic analysis, but the only sensible
implementation for code generation is to give them internal
linkage. We now model this notion via the UniqueExternalLinkage
linkage type. There are several changes here:
- Extended NamedDecl::getLinkage() to produce UniqueExternalLinkage
when the declaration is in an anonymous namespace.
- Added Type::getLinkage() to determine the linkage of a type, which
is defined as the minimum linkage of the types (when we're dealing
with a compound type that is not a struct/class/union).
- Extended NamedDecl::getLinkage() to consider the linkage of the
template arguments and template parameters of function template
specializations and class template specializations.
- Taught code generation to rely on NamedDecl::getLinkage() when
determining the linkage of variables and functions, also
considering the linkage of the types of those variables and
functions (C++ only). Map UniqueExternalLinkage to internal
linkage, taking out the explicit checks for
isInAnonymousNamespace().
This fixes much of PR5792, which, as discovered by Anders Carlsson, is
actually the reason behind the pass-manager assertion that causes the
majority of clang-on-clang regression test failures. With this fix,
Clang-built-Clang+LLVM passes 88% of its regression tests (up from
67%). The specific numbers are:
LLVM:
Expected Passes : 4006
Expected Failures : 32
Unsupported Tests : 40
Unexpected Failures: 736
Clang:
Expected Passes : 1903
Expected Failures : 14
Unexpected Failures: 75
Overall:
Expected Passes : 5909
Expected Failures : 46
Unsupported Tests : 40
Unexpected Failures: 811
Still to do:
- Improve testing
- Check whether we should allow the presence of types with
InternalLinkage (in addition to UniqueExternalLinkage) given
variables/functions internal linkage in C++, as mentioned in
PR5792.
- Determine how expensive the getLinkage() calls are in practice;
consider caching the result in NamedDecl.
- Assess the feasibility of Chris's idea in comment #1 of PR5792.
llvm-svn: 95216
2010-02-03 17:33:45 +08:00
|
|
|
|
2015-01-10 09:19:48 +08:00
|
|
|
bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(Global))
|
|
|
|
if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
|
|
|
|
// Implicit template instantiations may change linkage if they are later
|
|
|
|
// explicitly instantiated, so they should not be emitted eagerly.
|
|
|
|
return false;
|
2016-07-02 09:32:16 +08:00
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(Global))
|
|
|
|
if (Context.getInlineVariableDefinitionKind(VD) ==
|
|
|
|
ASTContext::InlineVariableDefinitionKind::WeakUnknown)
|
|
|
|
// A definition of an inline constexpr static data member may change
|
|
|
|
// linkage later if it's redeclared outside the class.
|
|
|
|
return false;
|
2015-07-14 06:54:53 +08:00
|
|
|
// If OpenMP is enabled and threadprivates must be generated like TLS, delay
|
|
|
|
// codegen for global variables, because they may be marked as threadprivate.
|
|
|
|
if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
|
|
|
|
getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global))
|
|
|
|
return false;
|
2015-01-10 09:19:48 +08:00
|
|
|
|
|
|
|
return true;
|
2009-02-14 05:18:01 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
ConstantAddress CodeGenModule::GetAddrOfUuidDescriptor(
|
2012-10-11 18:13:44 +08:00
|
|
|
const CXXUuidofExpr* E) {
|
|
|
|
// Sema has verified that IIDSource has a __declspec(uuid()), and that its
|
|
|
|
// well-formed.
|
2016-03-28 11:19:50 +08:00
|
|
|
StringRef Uuid = E->getUuidStr();
|
2013-08-09 16:35:59 +08:00
|
|
|
std::string Name = "_GUID_" + Uuid.lower();
|
|
|
|
std::replace(Name.begin(), Name.end(), '-', '_');
|
2012-10-11 18:13:44 +08:00
|
|
|
|
2016-03-27 12:46:14 +08:00
|
|
|
// The UUID descriptor should be pointer aligned.
|
|
|
|
CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes);
|
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
|
|
|
|
2012-10-11 18:13:44 +08:00
|
|
|
// Look for an existing global.
|
|
|
|
if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
|
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 ConstantAddress(GV, Alignment);
|
2012-10-11 18:13:44 +08:00
|
|
|
|
2014-09-09 00:26:36 +08:00
|
|
|
llvm::Constant *Init = EmitUuidofInitializer(Uuid);
|
2012-10-11 18:13:44 +08:00
|
|
|
assert(Init && "failed to initialize as constant");
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = new llvm::GlobalVariable(
|
2013-08-16 03:59:14 +08:00
|
|
|
getModule(), Init->getType(),
|
2013-09-04 05:49:32 +08:00
|
|
|
/*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
|
2015-05-10 05:10:07 +08:00
|
|
|
if (supportsCOMDAT())
|
|
|
|
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
|
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 ConstantAddress(GV, Alignment);
|
2012-10-11 18:13:44 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
|
2010-03-05 02:17:24 +08:00
|
|
|
const AliasAttr *AA = VD->getAttr<AliasAttr>();
|
|
|
|
assert(AA && "No alias?");
|
|
|
|
|
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
|
|
|
CharUnits Alignment = getContext().getDeclAlign(VD);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
|
2010-03-05 02:17:24 +08:00
|
|
|
|
|
|
|
// See if there is already something with the target's name in the module.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
|
2012-10-17 01:45:27 +08:00
|
|
|
if (Entry) {
|
|
|
|
unsigned AS = getContext().getTargetAddressSpace(VD->getType());
|
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
|
|
|
auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
|
|
|
|
return ConstantAddress(Ptr, Alignment);
|
2012-10-17 01:45:27 +08:00
|
|
|
}
|
2010-03-05 02:17:24 +08:00
|
|
|
|
|
|
|
llvm::Constant *Aliasee;
|
|
|
|
if (isa<llvm::FunctionType>(DeclTy))
|
2012-10-06 07:12:53 +08:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
|
|
|
|
GlobalDecl(cast<FunctionDecl>(VD)),
|
2011-02-05 12:35:53 +08:00
|
|
|
/*ForVTable=*/false);
|
2010-03-05 02:17:24 +08:00
|
|
|
else
|
2010-03-20 07:29:14 +08:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::PointerType::getUnqual(DeclTy),
|
|
|
|
nullptr);
|
2012-10-17 01:45:27 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *F = cast<llvm::GlobalValue>(Aliasee);
|
2012-10-17 01:45:27 +08:00
|
|
|
F->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
WeakRefReferences.insert(F);
|
2010-03-05 02:17:24 +08:00
|
|
|
|
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 ConstantAddress(Aliasee, Alignment);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2009-05-13 05:21:08 +08:00
|
|
|
void CodeGenModule::EmitGlobal(GlobalDecl GD) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *Global = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-05 02:17:24 +08:00
|
|
|
// Weak references don't produce any output by themselves.
|
|
|
|
if (Global->hasAttr<WeakRefAttr>())
|
|
|
|
return;
|
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
// If this is an alias definition (which otherwise looks like a declaration)
|
|
|
|
// emit it now.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (Global->hasAttr<AliasAttr>())
|
2013-10-22 22:23:09 +08:00
|
|
|
return EmitAliasDefinition(GD);
|
2008-09-09 07:44:31 +08:00
|
|
|
|
2016-04-11 15:48:59 +08:00
|
|
|
// IFunc like an alias whose value is resolved at runtime by calling resolver.
|
|
|
|
if (Global->hasAttr<IFuncAttr>())
|
|
|
|
return emitIFuncDefinition(GD);
|
|
|
|
|
2011-10-07 02:29:46 +08:00
|
|
|
// If this is CUDA, be selective about which declarations we emit.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.CUDA) {
|
2015-03-20 02:58:18 +08:00
|
|
|
if (LangOpts.CUDAIsDevice) {
|
2011-10-07 02:29:46 +08:00
|
|
|
if (!Global->hasAttr<CUDADeviceAttr>() &&
|
|
|
|
!Global->hasAttr<CUDAGlobalAttr>() &&
|
|
|
|
!Global->hasAttr<CUDAConstantAttr>() &&
|
|
|
|
!Global->hasAttr<CUDASharedAttr>())
|
|
|
|
return;
|
|
|
|
} else {
|
2016-03-03 02:28:50 +08:00
|
|
|
// We need to emit host-side 'shadows' for all global
|
|
|
|
// device-side variables because the CUDA runtime needs their
|
|
|
|
// size and host-side address in order to provide access to
|
|
|
|
// their device-side incarnations.
|
|
|
|
|
|
|
|
// So device-only functions are the only things we skip.
|
|
|
|
if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
|
|
|
|
Global->hasAttr<CUDADeviceAttr>())
|
2011-10-07 02:29:46 +08:00
|
|
|
return;
|
2016-03-03 02:28:50 +08:00
|
|
|
|
|
|
|
assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
|
|
|
|
"Expected Variable or Function");
|
2011-10-07 02:29:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:22:22 +08:00
|
|
|
if (LangOpts.OpenMP) {
|
|
|
|
// If this is OpenMP device, check if it is legal to emit this global
|
|
|
|
// normally.
|
|
|
|
if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
|
|
|
|
return;
|
|
|
|
if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
|
|
|
|
if (MustBeEmitted(Global))
|
|
|
|
EmitOMPDeclareReduction(DRD);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 21:42:12 +08:00
|
|
|
|
2009-03-21 17:44:56 +08:00
|
|
|
// Ignore declarations, they will be emitted on their first use.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
|
2009-02-14 05:18:01 +08:00
|
|
|
// Forward declarations are emitted lazily on first use.
|
2011-07-18 13:26:13 +08:00
|
|
|
if (!FD->doesThisDeclarationHaveABody()) {
|
|
|
|
if (!FD->doesDeclarationForceExternallyVisibleDefinition())
|
|
|
|
return;
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2014-03-29 22:19:55 +08:00
|
|
|
|
|
|
|
// Compute the function info and LLVM type.
|
|
|
|
const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::Type *Ty = getTypes().GetFunctionType(FI);
|
|
|
|
|
|
|
|
GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
|
|
|
|
/*DontDefer=*/false);
|
2009-02-14 05:18:01 +08:00
|
|
|
return;
|
2011-07-18 13:26:13 +08:00
|
|
|
}
|
2009-02-14 04:29:50 +08:00
|
|
|
} else {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *VD = cast<VarDecl>(Global);
|
2008-07-30 07:18:29 +08:00
|
|
|
assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
|
2016-03-03 02:28:50 +08:00
|
|
|
// We need to emit device-side global CUDA variables even if a
|
|
|
|
// variable does not have a definition -- we still need to define
|
|
|
|
// host-side shadow for it.
|
|
|
|
bool MustEmitForCuda = LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
|
|
|
|
!VD->hasDefinition() &&
|
|
|
|
(VD->hasAttr<CUDAConstantAttr>() ||
|
|
|
|
VD->hasAttr<CUDADeviceAttr>());
|
|
|
|
if (!MustEmitForCuda &&
|
|
|
|
VD->isThisDeclarationADefinition() != VarDecl::Definition &&
|
2016-07-02 09:32:16 +08:00
|
|
|
!Context.isMSStaticDataMemberInlineDefinition(VD)) {
|
|
|
|
// If this declaration may have caused an inline variable definition to
|
|
|
|
// change linkage, make sure that it's emitted.
|
|
|
|
if (Context.getInlineVariableDefinitionKind(VD) ==
|
|
|
|
ASTContext::InlineVariableDefinitionKind::Strong)
|
|
|
|
GetAddrOfGlobalVar(VD);
|
2009-02-14 05:18:01 +08:00
|
|
|
return;
|
2016-07-02 09:32:16 +08:00
|
|
|
}
|
2008-04-20 14:29:50 +08:00
|
|
|
}
|
|
|
|
|
2015-01-10 09:19:48 +08:00
|
|
|
// Defer code generation to first use when possible, e.g. if this is an inline
|
|
|
|
// function. If the global must always be emitted, do it eagerly if possible
|
|
|
|
// to benefit from cache locality.
|
|
|
|
if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
|
2010-04-14 01:39:09 +08:00
|
|
|
// Emit the definition if it can't be deferred.
|
|
|
|
EmitGlobalDefinition(GD);
|
2008-07-30 07:18:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-07-16 07:40:35 +08:00
|
|
|
|
|
|
|
// If we're deferring emission of a C++ variable with an
|
|
|
|
// initializer, remember the order in which it appeared in the file.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
|
2010-07-16 07:40:35 +08:00
|
|
|
cast<VarDecl>(Global)->hasInit()) {
|
|
|
|
DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
|
2014-05-21 13:09:00 +08:00
|
|
|
CXXGlobalInits.push_back(nullptr);
|
2010-07-16 07:40:35 +08:00
|
|
|
}
|
2015-01-10 09:19:48 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2015-01-10 09:19:48 +08:00
|
|
|
if (llvm::GlobalValue *GV = GetGlobalValue(MangledName)) {
|
|
|
|
// The value has already been used and should therefore be emitted.
|
2013-12-09 22:59:08 +08:00
|
|
|
addDeferredDeclToEmit(GV, GD);
|
2015-01-10 09:19:48 +08:00
|
|
|
} else if (MustBeEmitted(Global)) {
|
|
|
|
// The value must be emitted, but cannot be emitted eagerly.
|
|
|
|
assert(!MayBeEmittedEagerly(Global));
|
|
|
|
addDeferredDeclToEmit(/*GV=*/nullptr, GD);
|
|
|
|
} else {
|
2010-04-14 01:39:09 +08:00
|
|
|
// Otherwise, remember that we saw a deferred decl with this name. The
|
|
|
|
// first use of the mangled name will cause it to move into
|
|
|
|
// DeferredDeclsToEmit.
|
|
|
|
DeferredDecls[MangledName] = GD;
|
|
|
|
}
|
2008-04-20 14:29:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 07:28:10 +08:00
|
|
|
// Check if T is a class type with a destructor that's not dllimport.
|
|
|
|
static bool HasNonDllImportDtor(QualType T) {
|
|
|
|
if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
|
|
|
|
if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
|
|
|
if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-27 04:41:06 +08:00
|
|
|
namespace {
|
|
|
|
struct FunctionIsDirectlyRecursive :
|
|
|
|
public RecursiveASTVisitor<FunctionIsDirectlyRecursive> {
|
|
|
|
const StringRef Name;
|
2011-12-19 22:41:01 +08:00
|
|
|
const Builtin::Context &BI;
|
2011-10-27 04:41:06 +08:00
|
|
|
bool Result;
|
2011-12-19 22:41:01 +08:00
|
|
|
FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) :
|
|
|
|
Name(N), BI(C), Result(false) {
|
2011-10-27 04:41:06 +08:00
|
|
|
}
|
|
|
|
typedef RecursiveASTVisitor<FunctionIsDirectlyRecursive> Base;
|
|
|
|
|
|
|
|
bool TraverseCallExpr(CallExpr *E) {
|
2011-12-19 22:41:01 +08:00
|
|
|
const FunctionDecl *FD = E->getDirectCallee();
|
|
|
|
if (!FD)
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2011-12-19 22:41:01 +08:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (Attr && Name == Attr->getLabel()) {
|
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned BuiltinID = FD->getBuiltinID();
|
2015-06-26 07:50:40 +08:00
|
|
|
if (!BuiltinID || !BI.isLibFunction(BuiltinID))
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2015-08-06 09:01:12 +08:00
|
|
|
StringRef BuiltinName = BI.getName(BuiltinID);
|
2012-01-18 11:41:19 +08:00
|
|
|
if (BuiltinName.startswith("__builtin_") &&
|
|
|
|
Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
|
2011-10-27 04:41:06 +08:00
|
|
|
Result = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2015-08-29 05:47:01 +08:00
|
|
|
|
2017-02-16 07:28:10 +08:00
|
|
|
// Make sure we're not referencing non-imported vars or functions.
|
2015-08-29 05:47:01 +08:00
|
|
|
struct DLLImportFunctionVisitor
|
|
|
|
: public RecursiveASTVisitor<DLLImportFunctionVisitor> {
|
|
|
|
bool SafeToInline = true;
|
|
|
|
|
2016-09-14 06:51:42 +08:00
|
|
|
bool shouldVisitImplicitCode() const { return true; }
|
|
|
|
|
2015-08-29 05:47:01 +08:00
|
|
|
bool VisitVarDecl(VarDecl *VD) {
|
2017-02-16 07:28:10 +08:00
|
|
|
if (VD->getTLSKind()) {
|
|
|
|
// A thread-local variable cannot be imported.
|
|
|
|
SafeToInline = false;
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A variable definition might imply a destructor call.
|
|
|
|
if (VD->isThisDeclarationADefinition())
|
|
|
|
SafeToInline = !HasNonDllImportDtor(VD->getType());
|
|
|
|
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
|
|
|
|
if (const auto *D = E->getTemporary()->getDestructor())
|
|
|
|
SafeToInline = D->hasAttr<DLLImportAttr>();
|
2015-08-29 05:47:01 +08:00
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *E) {
|
|
|
|
ValueDecl *VD = E->getDecl();
|
|
|
|
if (isa<FunctionDecl>(VD))
|
|
|
|
SafeToInline = VD->hasAttr<DLLImportAttr>();
|
|
|
|
else if (VarDecl *V = dyn_cast<VarDecl>(VD))
|
|
|
|
SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
2017-02-16 07:28:10 +08:00
|
|
|
|
2016-09-14 06:51:42 +08:00
|
|
|
bool VisitCXXConstructExpr(CXXConstructExpr *E) {
|
|
|
|
SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
2017-02-16 07:28:10 +08:00
|
|
|
|
2017-01-24 07:57:50 +08:00
|
|
|
bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
|
|
|
|
CXXMethodDecl *M = E->getMethodDecl();
|
|
|
|
if (!M) {
|
|
|
|
// Call through a pointer to member function. This is safe to inline.
|
|
|
|
SafeToInline = true;
|
|
|
|
} else {
|
|
|
|
SafeToInline = M->hasAttr<DLLImportAttr>();
|
|
|
|
}
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
2017-02-16 07:28:10 +08:00
|
|
|
|
2015-08-29 05:47:01 +08:00
|
|
|
bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
|
|
|
|
SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
2017-02-16 07:28:10 +08:00
|
|
|
|
2015-08-29 05:47:01 +08:00
|
|
|
bool VisitCXXNewExpr(CXXNewExpr *E) {
|
|
|
|
SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-10-27 04:41:06 +08:00
|
|
|
|
2011-12-19 22:41:01 +08:00
|
|
|
// isTriviallyRecursive - Check if this function calls another
|
|
|
|
// decl that, because of the asm attribute or the other decl being a builtin,
|
|
|
|
// ends up pointing to itself.
|
2011-10-27 04:41:06 +08:00
|
|
|
bool
|
2011-12-19 22:41:01 +08:00
|
|
|
CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
|
|
|
|
StringRef Name;
|
|
|
|
if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
|
2012-01-18 09:50:13 +08:00
|
|
|
// asm labels are a special kind of mangling we have to support.
|
2011-12-19 22:41:01 +08:00
|
|
|
AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
|
|
|
|
if (!Attr)
|
|
|
|
return false;
|
|
|
|
Name = Attr->getLabel();
|
|
|
|
} else {
|
|
|
|
Name = FD->getName();
|
|
|
|
}
|
2011-10-27 04:41:06 +08:00
|
|
|
|
2015-06-30 12:41:18 +08:00
|
|
|
FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
|
2011-12-19 22:41:01 +08:00
|
|
|
Walker.TraverseFunctionDecl(const_cast<FunctionDecl*>(FD));
|
2011-10-27 04:41:06 +08:00
|
|
|
return Walker.Result;
|
|
|
|
}
|
|
|
|
|
2016-09-14 05:08:20 +08:00
|
|
|
bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
|
2013-06-06 01:49:37 +08:00
|
|
|
if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
|
2011-10-27 04:41:06 +08:00
|
|
|
return true;
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *F = cast<FunctionDecl>(GD.getDecl());
|
2014-02-25 17:53:29 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
|
2011-10-27 04:41:06 +08:00
|
|
|
return false;
|
2015-08-29 05:47:01 +08:00
|
|
|
|
|
|
|
if (F->hasAttr<DLLImportAttr>()) {
|
|
|
|
// Check whether it would be safe to inline this dllimport function.
|
|
|
|
DLLImportFunctionVisitor Visitor;
|
|
|
|
Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
|
|
|
|
if (!Visitor.SafeToInline)
|
|
|
|
return false;
|
2016-09-14 05:08:20 +08:00
|
|
|
|
|
|
|
if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
|
|
|
|
// Implicit destructor invocations aren't captured in the AST, so the
|
|
|
|
// check above can't see them. Check for them manually here.
|
|
|
|
for (const Decl *Member : Dtor->getParent()->decls())
|
|
|
|
if (isa<FieldDecl>(Member))
|
|
|
|
if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
|
|
|
|
return false;
|
|
|
|
for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
|
|
|
|
if (HasNonDllImportDtor(B.getType()))
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-29 05:47:01 +08:00
|
|
|
}
|
|
|
|
|
2011-10-27 04:41:06 +08:00
|
|
|
// PR9614. Avoid cases where the source code is lying to us. An available
|
|
|
|
// externally function should have an equivalent function somewhere else,
|
|
|
|
// but a function that calls itself is clearly not equivalent to the real
|
|
|
|
// implementation.
|
|
|
|
// This happens in glibc's btowc and in some configure checks.
|
2011-12-19 22:41:01 +08:00
|
|
|
return !isTriviallyRecursive(F);
|
2011-10-27 04:41:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-10 00:01:03 +08:00
|
|
|
void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *D = cast<ValueDecl>(GD.getDecl());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-20 00:39:44 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
|
2009-10-27 22:32:27 +08:00
|
|
|
Context.getSourceManager(),
|
|
|
|
"Generating code for declaration");
|
|
|
|
|
2013-06-06 01:49:37 +08:00
|
|
|
if (isa<FunctionDecl>(D)) {
|
2010-07-13 14:02:28 +08:00
|
|
|
// At -O0, don't generate IR for functions with available_externally
|
|
|
|
// linkage.
|
2013-06-06 01:49:37 +08:00
|
|
|
if (!shouldEmitFunction(GD))
|
2010-07-13 14:02:28 +08:00
|
|
|
return;
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
|
2011-05-07 01:27:27 +08:00
|
|
|
// Make sure to emit the definition(s) before we emit the thunks.
|
|
|
|
// This is necessary for the generation of certain thunks.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CD = dyn_cast<CXXConstructorDecl>(Method))
|
2014-09-16 03:20:10 +08:00
|
|
|
ABI->emitCXXStructor(CD, getFromCtorType(GD.getCtorType()));
|
2014-05-09 08:08:36 +08:00
|
|
|
else if (const auto *DD = dyn_cast<CXXDestructorDecl>(Method))
|
2014-09-16 03:20:10 +08:00
|
|
|
ABI->emitCXXStructor(DD, getFromDtorType(GD.getDtorType()));
|
2011-05-07 01:27:27 +08:00
|
|
|
else
|
2013-12-10 00:01:03 +08:00
|
|
|
EmitGlobalFunctionDefinition(GD, GV);
|
2011-05-07 01:27:27 +08:00
|
|
|
|
2010-07-13 14:02:28 +08:00
|
|
|
if (Method->isVirtual())
|
|
|
|
getVTables().EmitThunks(GD);
|
2010-03-23 12:31:31 +08:00
|
|
|
|
2011-05-07 01:27:27 +08:00
|
|
|
return;
|
2010-07-13 14:02:28 +08:00
|
|
|
}
|
2010-04-14 01:57:11 +08:00
|
|
|
|
2013-12-10 00:01:03 +08:00
|
|
|
return EmitGlobalFunctionDefinition(GD, GV);
|
2010-07-13 14:02:28 +08:00
|
|
|
}
|
2014-05-09 08:08:36 +08:00
|
|
|
|
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(D))
|
2016-01-14 18:41:16 +08:00
|
|
|
return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
|
2010-04-14 01:39:09 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
|
|
|
|
llvm::Function *NewFn);
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
|
|
|
|
/// module, create and return an llvm Function with the specified type. If there
|
|
|
|
/// is something in the module with the specified name, return it potentially
|
|
|
|
/// bitcasted to the right type.
|
|
|
|
///
|
|
|
|
/// If D is non-null, it specifies a decl that correspond to this. This is used
|
|
|
|
/// to set the attributes on the function when it is first created.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::Constant *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::GetOrCreateLLVMFunction(StringRef MangledName,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2013-07-22 21:07:10 +08:00
|
|
|
GlobalDecl GD, bool ForVTable,
|
2014-11-01 13:42:23 +08:00
|
|
|
bool DontDefer, bool IsThunk,
|
2015-08-31 21:20:44 +08:00
|
|
|
llvm::AttributeSet ExtraAttrs,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition_t IsForDefinition) {
|
2013-07-22 21:07:10 +08:00
|
|
|
const Decl *D = GD.getDecl();
|
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 17:25:43 +08:00
|
|
|
if (Entry) {
|
2012-08-22 23:37:55 +08:00
|
|
|
if (WeakRefReferences.erase(Entry)) {
|
2013-07-22 21:07:10 +08:00
|
|
|
const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
|
2010-03-05 02:17:24 +08:00
|
|
|
if (FD && !FD->hasAttr<WeakAttr>())
|
2010-03-23 12:31:31 +08:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2014-08-29 08:16:06 +08:00
|
|
|
// Handle dropped DLL attributes.
|
|
|
|
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
|
|
|
|
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
|
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
// If there are two attempts to define the same mangled name, issue an
|
|
|
|
// error.
|
|
|
|
if (IsForDefinition && !Entry->isDeclaration()) {
|
|
|
|
GlobalDecl OtherGD;
|
2016-01-14 18:41:16 +08:00
|
|
|
// Check that GD is not yet in DiagnosedConflictingDefinitions is required
|
|
|
|
// to make sure that we issue an error only once.
|
2015-08-31 21:20:44 +08:00
|
|
|
if (lookupRepresentativeDecl(MangledName, OtherGD) &&
|
|
|
|
(GD.getCanonicalDecl().getDecl() !=
|
|
|
|
OtherGD.getCanonicalDecl().getDecl()) &&
|
|
|
|
DiagnosedConflictingDefinitions.insert(GD).second) {
|
|
|
|
getDiags().Report(D->getLocation(),
|
|
|
|
diag::err_duplicate_mangled_name);
|
|
|
|
getDiags().Report(OtherGD.getDecl()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
|
|
|
|
(Entry->getType()->getElementType() == Ty)) {
|
2009-03-21 17:25:43 +08:00
|
|
|
return Entry;
|
2015-08-31 21:20:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:25:43 +08:00
|
|
|
// Make sure the result is of the correct type.
|
2015-08-31 21:20:44 +08:00
|
|
|
// (If function is requested for a definition, we always need to create a new
|
|
|
|
// function, not just return a bitcast.)
|
|
|
|
if (!IsForDefinition)
|
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
|
2009-03-21 17:25:43 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-09 13:07:37 +08:00
|
|
|
// This function doesn't have a complete type (for example, the return
|
|
|
|
// type is an incomplete struct). Use a fake type instead, and make
|
|
|
|
// sure not to try to set attributes.
|
|
|
|
bool IsIncompleteFunction = false;
|
2010-04-28 08:00:30 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy;
|
2010-04-28 08:00:30 +08:00
|
|
|
if (isa<llvm::FunctionType>(Ty)) {
|
|
|
|
FTy = cast<llvm::FunctionType>(Ty);
|
|
|
|
} else {
|
2011-05-15 09:53:33 +08:00
|
|
|
FTy = llvm::FunctionType::get(VoidTy, false);
|
2009-11-09 13:07:37 +08:00
|
|
|
IsIncompleteFunction = true;
|
|
|
|
}
|
2015-08-31 21:20:44 +08:00
|
|
|
|
|
|
|
llvm::Function *F =
|
|
|
|
llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
|
|
|
|
Entry ? StringRef() : MangledName, &getModule());
|
|
|
|
|
|
|
|
// If we already created a function with the same mangled name (but different
|
|
|
|
// type) before, take its name and add it to the list of functions to be
|
|
|
|
// replaced with F at the end of CodeGen.
|
|
|
|
//
|
|
|
|
// This happens if there is a prototype for a function (e.g. "int f()") and
|
|
|
|
// then a definition of a different type (e.g. "int f(int x)").
|
|
|
|
if (Entry) {
|
|
|
|
F->takeName(Entry);
|
|
|
|
|
|
|
|
// This might be an implementation of a function without a prototype, in
|
|
|
|
// which case, try to do special replacement of calls which match the new
|
|
|
|
// prototype. The really key thing here is that we also potentially drop
|
|
|
|
// arguments from the call site so as to make a direct call, which makes the
|
|
|
|
// inliner happier and suppresses a number of optimizer warnings (!) about
|
|
|
|
// dropping arguments.
|
|
|
|
if (!Entry->use_empty()) {
|
|
|
|
ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F);
|
|
|
|
Entry->removeDeadConstantUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *BC = llvm::ConstantExpr::getBitCast(
|
|
|
|
F, Entry->getType()->getElementType()->getPointerTo());
|
|
|
|
addGlobalValReplacement(Entry, BC);
|
|
|
|
}
|
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
assert(F->getName() == MangledName && "name was uniqued!");
|
2013-07-22 21:07:10 +08:00
|
|
|
if (D)
|
2014-11-01 13:42:23 +08:00
|
|
|
SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
|
2013-01-31 08:30:05 +08:00
|
|
|
if (ExtraAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex)) {
|
|
|
|
llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeSet::FunctionIndex);
|
2013-01-23 08:21:06 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(VMContext,
|
|
|
|
llvm::AttributeSet::FunctionIndex,
|
|
|
|
B));
|
|
|
|
}
|
2009-11-09 13:07:37 +08:00
|
|
|
|
2013-12-09 12:29:47 +08:00
|
|
|
if (!DontDefer) {
|
|
|
|
// All MSVC dtors other than the base dtor are linkonce_odr and delegate to
|
|
|
|
// each other bottoming out with the base dtor. Therefore we emit non-base
|
|
|
|
// dtors on usage, even if there is no dtor definition in the TU.
|
|
|
|
if (D && isa<CXXDestructorDecl>(D) &&
|
|
|
|
getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
|
|
|
|
GD.getDtorType()))
|
2013-12-09 22:59:08 +08:00
|
|
|
addDeferredDeclToEmit(F, GD);
|
2013-12-09 12:29:47 +08:00
|
|
|
|
|
|
|
// This is the first use or definition of a mangled name. If there is a
|
|
|
|
// deferred decl with this name, remember that we need to emit it at the end
|
|
|
|
// of the file.
|
2014-06-06 06:10:59 +08:00
|
|
|
auto DDI = DeferredDecls.find(MangledName);
|
2013-12-09 12:29:47 +08:00
|
|
|
if (DDI != DeferredDecls.end()) {
|
|
|
|
// Move the potentially referenced deferred decl to the
|
|
|
|
// DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
|
|
|
|
// don't need it anymore).
|
2013-12-09 22:59:08 +08:00
|
|
|
addDeferredDeclToEmit(F, DDI->second);
|
2013-12-09 12:29:47 +08:00
|
|
|
DeferredDecls.erase(DDI);
|
|
|
|
|
|
|
|
// Otherwise, there are cases we have to worry about where we're
|
|
|
|
// using a declaration for which we must emit a definition but where
|
|
|
|
// we might not find a top-level definition:
|
|
|
|
// - member functions defined inline in their classes
|
|
|
|
// - friend functions defined inline in some class
|
|
|
|
// - special member functions with implicit definitions
|
|
|
|
// If we ever change our AST traversal to walk into class methods,
|
|
|
|
// this will be unnecessary.
|
|
|
|
//
|
|
|
|
// We also don't emit a definition for a function if it's going to be an
|
2014-08-01 09:56:39 +08:00
|
|
|
// entry in a vtable, unless it's already marked as used.
|
2013-12-09 12:29:47 +08:00
|
|
|
} else if (getLangOpts().CPlusPlus && D) {
|
|
|
|
// Look for a declaration that's lexically in a record.
|
2014-08-01 09:56:39 +08:00
|
|
|
for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
|
|
|
|
FD = FD->getPreviousDecl()) {
|
2013-12-09 12:29:47 +08:00
|
|
|
if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
|
2014-08-01 09:56:39 +08:00
|
|
|
if (FD->doesThisDeclarationHaveABody()) {
|
2013-12-09 22:59:08 +08:00
|
|
|
addDeferredDeclToEmit(F, GD.getWithDecl(FD));
|
2013-12-09 12:29:47 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-12-15 12:00:32 +08:00
|
|
|
}
|
2014-08-01 09:56:39 +08:00
|
|
|
}
|
2013-12-09 12:29:47 +08:00
|
|
|
}
|
2009-03-21 17:44:56 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-28 08:00:30 +08:00
|
|
|
// Make sure the result is of the requested type.
|
|
|
|
if (!IsIncompleteFunction) {
|
|
|
|
assert(F->getType()->getElementType() == Ty);
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2010-04-28 08:00:30 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(F, PTy);
|
2009-03-21 17:25:43 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// GetAddrOfFunction - Return the address of the given function. If Ty is
|
|
|
|
/// non-null, then this function will use the specified type if it has to
|
|
|
|
/// create it (this occurs when we see a definition of the function).
|
2009-05-13 05:21:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2013-12-09 12:29:47 +08:00
|
|
|
bool ForVTable,
|
2015-08-31 21:20:44 +08:00
|
|
|
bool DontDefer,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition_t IsForDefinition) {
|
2009-03-23 05:03:39 +08:00
|
|
|
// If there was no specific requested type, just convert it now.
|
2015-12-03 05:58:08 +08:00
|
|
|
if (!Ty) {
|
|
|
|
const auto *FD = cast<FunctionDecl>(GD.getDecl());
|
|
|
|
auto CanonTy = Context.getCanonicalType(FD->getType());
|
|
|
|
Ty = getTypes().ConvertFunctionType(CanonTy, FD);
|
|
|
|
}
|
2015-08-31 21:20:44 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2015-08-31 21:20:44 +08:00
|
|
|
return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
|
|
|
|
/*IsThunk=*/false, llvm::AttributeSet(),
|
|
|
|
IsForDefinition);
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2008-05-31 03:50:47 +08:00
|
|
|
|
2016-12-15 14:59:05 +08:00
|
|
|
static const FunctionDecl *
|
|
|
|
GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
|
|
|
|
TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
|
|
|
|
DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
|
|
|
|
|
|
|
|
IdentifierInfo &CII = C.Idents.get(Name);
|
|
|
|
for (const auto &Result : DC->lookup(&CII))
|
|
|
|
if (const auto FD = dyn_cast<FunctionDecl>(Result))
|
|
|
|
return FD;
|
|
|
|
|
|
|
|
if (!C.getLangOpts().CPlusPlus)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Demangle the premangled name from getTerminateFn()
|
|
|
|
IdentifierInfo &CXXII =
|
|
|
|
(Name == "_ZSt9terminatev" || Name == "\01?terminate@@YAXXZ")
|
|
|
|
? C.Idents.get("terminate")
|
|
|
|
: C.Idents.get(Name);
|
|
|
|
|
|
|
|
for (const auto &N : {"__cxxabiv1", "std"}) {
|
|
|
|
IdentifierInfo &NS = C.Idents.get(N);
|
|
|
|
for (const auto &Result : DC->lookup(&NS)) {
|
|
|
|
NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
|
|
|
|
if (auto LSD = dyn_cast<LinkageSpecDecl>(Result))
|
|
|
|
for (const auto &Result : LSD->lookup(&NS))
|
|
|
|
if ((ND = dyn_cast<NamespaceDecl>(Result)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ND)
|
|
|
|
for (const auto &Result : ND->lookup(&CXXII))
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(Result))
|
|
|
|
return FD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// CreateRuntimeFunction - Create a new runtime function with the specified
|
|
|
|
/// type and name.
|
|
|
|
llvm::Constant *
|
2016-12-15 14:59:05 +08:00
|
|
|
CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
|
|
|
|
llvm::AttributeSet ExtraAttrs,
|
|
|
|
bool Local) {
|
2013-12-09 12:29:47 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
|
2016-12-15 14:59:05 +08:00
|
|
|
/*DontDefer=*/false, /*IsThunk=*/false,
|
|
|
|
ExtraAttrs);
|
|
|
|
|
|
|
|
if (auto *F = dyn_cast<llvm::Function>(C)) {
|
|
|
|
if (F->empty()) {
|
2013-03-01 03:01:20 +08:00
|
|
|
F->setCallingConv(getRuntimeCC());
|
2016-12-15 14:59:05 +08:00
|
|
|
|
|
|
|
if (!Local && getTriple().isOSBinFormatCOFF() &&
|
|
|
|
!getCodeGenOpts().LTOVisibilityPublicStd) {
|
|
|
|
const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
|
|
|
|
if (!FD || FD->hasAttr<DLLImportAttr>()) {
|
|
|
|
F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
|
|
|
|
F->setLinkage(llvm::GlobalValue::ExternalLinkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
return C;
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2014-12-03 00:04:58 +08:00
|
|
|
/// CreateBuiltinFunction - Create a new builtin function with the specified
|
|
|
|
/// type and name.
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::CreateBuiltinFunction(llvm::FunctionType *FTy,
|
|
|
|
StringRef Name,
|
|
|
|
llvm::AttributeSet ExtraAttrs) {
|
|
|
|
llvm::Constant *C =
|
|
|
|
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
|
|
|
|
/*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
|
|
|
|
if (auto *F = dyn_cast<llvm::Function>(C))
|
|
|
|
if (F->empty())
|
|
|
|
F->setCallingConv(getBuiltinCC());
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2012-02-17 14:48:11 +08:00
|
|
|
/// isTypeConstant - Determine whether an object of this type can be emitted
|
|
|
|
/// as a constant.
|
|
|
|
///
|
|
|
|
/// If ExcludeCtor is true, the duration when the object's constructor runs
|
|
|
|
/// will not be considered. The caller will need to verify that the object is
|
|
|
|
/// not written to during its construction.
|
|
|
|
bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
|
|
|
|
if (!Ty.isConstant(Context) && !Ty->isReferenceType())
|
2009-12-12 05:23:03 +08:00
|
|
|
return false;
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Context.getLangOpts().CPlusPlus) {
|
2012-02-17 14:48:11 +08:00
|
|
|
if (const CXXRecordDecl *Record
|
|
|
|
= Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
|
|
|
|
return ExcludeCtor && !Record->hasMutableFields() &&
|
|
|
|
Record->hasTrivialDestructor();
|
2009-12-12 05:23:03 +08:00
|
|
|
}
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2009-12-12 05:23:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
|
|
|
|
/// create and return an llvm GlobalVariable with the specified type. If there
|
|
|
|
/// is something in the module with the specified name, return it potentially
|
|
|
|
/// bitcasted to the right type.
|
|
|
|
///
|
|
|
|
/// If D is non-null, it specifies a decl that correspond to this. This is used
|
|
|
|
/// to set the attributes on the global when it is first created.
|
2016-01-14 18:41:16 +08:00
|
|
|
///
|
|
|
|
/// If IsForDefinition is true, it is guranteed that an actual global with
|
|
|
|
/// type Ty will be returned, not conversion of a variable with the same
|
|
|
|
/// mangled name but some other type.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::Constant *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *Ty,
|
2016-01-14 18:41:16 +08:00
|
|
|
const VarDecl *D,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition_t IsForDefinition) {
|
2008-08-06 07:31:02 +08:00
|
|
|
// Lookup the entry, lazily creating it if necessary.
|
2010-03-20 07:29:14 +08:00
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2009-03-21 16:03:33 +08:00
|
|
|
if (Entry) {
|
2012-08-22 23:37:55 +08:00
|
|
|
if (WeakRefReferences.erase(Entry)) {
|
2010-03-05 02:17:24 +08:00
|
|
|
if (D && !D->hasAttr<WeakAttr>())
|
2010-03-23 12:31:31 +08:00
|
|
|
Entry->setLinkage(llvm::Function::ExternalLinkage);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2014-08-29 08:16:06 +08:00
|
|
|
// Handle dropped DLL attributes.
|
|
|
|
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
|
|
|
|
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
if (Entry->getType() == Ty)
|
2009-03-21 17:16:30 +08:00
|
|
|
return Entry;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-01-14 18:41:16 +08:00
|
|
|
// If there are two attempts to define the same mangled name, issue an
|
|
|
|
// error.
|
|
|
|
if (IsForDefinition && !Entry->isDeclaration()) {
|
|
|
|
GlobalDecl OtherGD;
|
|
|
|
const VarDecl *OtherD;
|
|
|
|
|
|
|
|
// Check that D is not yet in DiagnosedConflictingDefinitions is required
|
|
|
|
// to make sure that we issue an error only once.
|
2016-05-20 02:00:18 +08:00
|
|
|
if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
|
2016-01-14 18:41:16 +08:00
|
|
|
(D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
|
|
|
|
(OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
|
|
|
|
OtherD->hasInit() &&
|
|
|
|
DiagnosedConflictingDefinitions.insert(D).second) {
|
|
|
|
getDiags().Report(D->getLocation(),
|
|
|
|
diag::err_duplicate_mangled_name);
|
|
|
|
getDiags().Report(OtherGD.getDecl()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-21 16:03:33 +08:00
|
|
|
// Make sure the result is of the correct type.
|
2013-11-15 10:19:52 +08:00
|
|
|
if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
|
|
|
|
return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
|
|
|
|
|
2016-01-14 18:41:16 +08:00
|
|
|
// (If global is requested for a definition, we always need to create a new
|
|
|
|
// global, not just return a bitcast.)
|
|
|
|
if (!IsForDefinition)
|
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, Ty);
|
2009-03-21 16:03:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-12-09 22:59:08 +08:00
|
|
|
unsigned AddrSpace = GetGlobalVarAddressSpace(D, Ty->getAddressSpace());
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
getModule(), Ty->getElementType(), false,
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
|
2014-05-09 08:08:36 +08:00
|
|
|
llvm::GlobalVariable::NotThreadLocal, AddrSpace);
|
2013-12-09 22:59:08 +08:00
|
|
|
|
2016-01-14 18:41:16 +08:00
|
|
|
// If we already created a global with the same mangled name (but different
|
|
|
|
// type) before, take its name and remove it from its parent.
|
|
|
|
if (Entry) {
|
|
|
|
GV->takeName(Entry);
|
|
|
|
|
|
|
|
if (!Entry->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, Entry->getType());
|
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2009-03-21 17:44:56 +08:00
|
|
|
// This is the first use or definition of a mangled name. If there is a
|
|
|
|
// deferred decl with this name, remember that we need to emit it at the end
|
|
|
|
// of the file.
|
2014-06-06 06:10:59 +08:00
|
|
|
auto DDI = DeferredDecls.find(MangledName);
|
2009-03-21 17:44:56 +08:00
|
|
|
if (DDI != DeferredDecls.end()) {
|
|
|
|
// Move the potentially referenced deferred decl to the DeferredDeclsToEmit
|
|
|
|
// list, and remove it from DeferredDecls (since we don't need it anymore).
|
2013-12-09 22:59:08 +08:00
|
|
|
addDeferredDeclToEmit(GV, DDI->second);
|
2009-03-21 17:44:56 +08:00
|
|
|
DeferredDecls.erase(DDI);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 16:03:33 +08:00
|
|
|
// Handle things which are present even on external declarations.
|
2009-03-23 05:03:39 +08:00
|
|
|
if (D) {
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: This code is overly simple and should be merged with other global
|
|
|
|
// handling.
|
2012-02-17 14:48:11 +08:00
|
|
|
GV->setConstant(isTypeConstant(D->getType(), false));
|
2009-01-13 10:25:00 +08:00
|
|
|
|
2015-04-22 01:27:59 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
|
|
|
|
|
2014-04-26 01:07:16 +08:00
|
|
|
setLinkageAndVisibilityForGV(GV, D);
|
2009-04-20 05:05:03 +08:00
|
|
|
|
2013-04-20 00:42:07 +08:00
|
|
|
if (D->getTLSKind()) {
|
2013-04-22 16:06:17 +08:00
|
|
|
if (D->getTLSKind() == VarDecl::TLS_Dynamic)
|
2015-12-01 09:10:48 +08:00
|
|
|
CXXThreadLocals.push_back(D);
|
2012-06-28 16:01:44 +08:00
|
|
|
setTLSMode(GV, *D);
|
2013-04-20 00:42:07 +08:00
|
|
|
}
|
2013-11-21 08:15:56 +08:00
|
|
|
|
|
|
|
// If required by the ABI, treat declarations of static data members with
|
|
|
|
// inline initializers as definitions.
|
2014-07-18 04:25:23 +08:00
|
|
|
if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
|
2013-11-21 08:15:56 +08:00
|
|
|
EmitGlobalVarDefinition(D);
|
2014-07-18 04:25:23 +08:00
|
|
|
}
|
2014-05-02 17:33:30 +08:00
|
|
|
|
|
|
|
// Handle XCore specific ABI requirements.
|
2016-09-14 23:17:46 +08:00
|
|
|
if (getTriple().getArch() == llvm::Triple::xcore &&
|
2014-05-02 17:33:30 +08:00
|
|
|
D->getLanguageLinkage() == CLanguageLinkage &&
|
|
|
|
D->getType().isConstant(Context) &&
|
|
|
|
isExternallyVisible(D->getLinkageAndVisibility().getLinkage()))
|
|
|
|
GV->setSection(".cp.rodata");
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-05-21 05:08:35 +08:00
|
|
|
if (AddrSpace != Ty->getAddressSpace())
|
2013-11-15 10:19:52 +08:00
|
|
|
return llvm::ConstantExpr::getAddrSpaceCast(GV, Ty);
|
|
|
|
|
|
|
|
return GV;
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-02-21 08:24:10 +08:00
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfGlobal(GlobalDecl GD,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition_t IsForDefinition) {
|
2016-12-24 23:32:39 +08:00
|
|
|
const Decl *D = GD.getDecl();
|
|
|
|
if (isa<CXXConstructorDecl>(D))
|
|
|
|
return getAddrOfCXXStructor(cast<CXXConstructorDecl>(D),
|
2015-08-31 21:20:44 +08:00
|
|
|
getFromCtorType(GD.getCtorType()),
|
|
|
|
/*FnInfo=*/nullptr, /*FnType=*/nullptr,
|
|
|
|
/*DontDefer=*/false, IsForDefinition);
|
2016-12-24 23:32:39 +08:00
|
|
|
else if (isa<CXXDestructorDecl>(D))
|
|
|
|
return getAddrOfCXXStructor(cast<CXXDestructorDecl>(D),
|
2015-08-31 21:20:44 +08:00
|
|
|
getFromDtorType(GD.getDtorType()),
|
|
|
|
/*FnInfo=*/nullptr, /*FnType=*/nullptr,
|
|
|
|
/*DontDefer=*/false, IsForDefinition);
|
2016-12-24 23:32:39 +08:00
|
|
|
else if (isa<CXXMethodDecl>(D)) {
|
2015-08-31 21:20:44 +08:00
|
|
|
auto FInfo = &getTypes().arrangeCXXMethodDeclaration(
|
2016-12-24 23:32:39 +08:00
|
|
|
cast<CXXMethodDecl>(D));
|
2015-08-31 21:20:44 +08:00
|
|
|
auto Ty = getTypes().GetFunctionType(*FInfo);
|
|
|
|
return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
|
|
|
|
IsForDefinition);
|
2016-12-24 23:32:39 +08:00
|
|
|
} else if (isa<FunctionDecl>(D)) {
|
2015-08-31 21:20:44 +08:00
|
|
|
const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
|
|
|
|
return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
|
|
|
|
IsForDefinition);
|
|
|
|
} else
|
2016-12-24 23:32:39 +08:00
|
|
|
return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr,
|
2016-01-14 18:41:16 +08:00
|
|
|
IsForDefinition);
|
2015-08-31 21:20:44 +08:00
|
|
|
}
|
2009-02-21 08:24:10 +08:00
|
|
|
|
2011-01-30 02:20:20 +08:00
|
|
|
llvm::GlobalVariable *
|
2011-07-23 18:55:15 +08:00
|
|
|
CodeGenModule::CreateOrReplaceCXXRuntimeVariable(StringRef Name,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty,
|
2011-01-30 02:20:20 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
|
|
|
llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::GlobalVariable *OldGV = nullptr;
|
2011-01-30 02:20:20 +08:00
|
|
|
|
|
|
|
if (GV) {
|
|
|
|
// Check if the variable has the right type.
|
|
|
|
if (GV->getType()->getElementType() == Ty)
|
|
|
|
return GV;
|
|
|
|
|
|
|
|
// Because C++ name mangling, the only way we can end up with an already
|
|
|
|
// existing global with the same name is if it has been declared extern "C".
|
2012-10-11 18:13:44 +08:00
|
|
|
assert(GV->isDeclaration() && "Declaration has wrong type!");
|
2011-01-30 02:20:20 +08:00
|
|
|
OldGV = GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new variable.
|
|
|
|
GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
|
2014-05-21 13:09:00 +08:00
|
|
|
Linkage, nullptr, Name);
|
|
|
|
|
2011-01-30 02:20:20 +08:00
|
|
|
if (OldGV) {
|
|
|
|
// Replace occurrences of the old variable if needed.
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
|
|
|
|
if (!OldGV->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
2015-01-16 07:18:01 +08:00
|
|
|
|
2015-05-10 05:10:07 +08:00
|
|
|
if (supportsCOMDAT() && GV->isWeakForLinker() &&
|
|
|
|
!GV->hasAvailableExternallyLinkage())
|
|
|
|
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
|
2015-01-16 07:18:01 +08:00
|
|
|
|
2011-01-30 02:20:20 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
|
|
|
|
/// given global variable. If Ty is non-null and if the global doesn't exist,
|
2012-04-17 07:55:04 +08:00
|
|
|
/// then it will be created with the specified type instead of whatever the
|
2016-01-14 18:41:16 +08:00
|
|
|
/// normal requested type would be. If IsForDefinition is true, it is guranteed
|
|
|
|
/// that an actual global with type Ty will be returned, not conversion of a
|
|
|
|
/// variable with the same mangled name but some other type.
|
2009-03-23 05:03:39 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
|
2016-01-14 18:41:16 +08:00
|
|
|
llvm::Type *Ty,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition_t IsForDefinition) {
|
2009-03-23 05:03:39 +08:00
|
|
|
assert(D->hasGlobalStorage() && "Not a global variable");
|
|
|
|
QualType ASTTy = D->getType();
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!Ty)
|
2009-03-23 05:03:39 +08:00
|
|
|
Ty = getTypes().ConvertTypeForMem(ASTTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *PTy =
|
2011-03-19 06:38:29 +08:00
|
|
|
llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
|
2010-03-20 07:29:14 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(D);
|
2016-01-14 18:41:16 +08:00
|
|
|
return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition);
|
2009-03-23 05:03:39 +08:00
|
|
|
}
|
2009-03-05 01:31:19 +08:00
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// CreateRuntimeVariable - Create a new runtime global variable with the
|
|
|
|
/// specified type and name.
|
|
|
|
llvm::Constant *
|
2011-07-18 12:24:23 +08:00
|
|
|
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name) {
|
2014-05-23 07:33:27 +08:00
|
|
|
return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), nullptr);
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2009-04-16 06:08:45 +08:00
|
|
|
void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
|
|
|
|
assert(!D->getInit() && "Cannot emit definite definitions here!");
|
|
|
|
|
2016-01-14 18:41:16 +08:00
|
|
|
StringRef MangledName = getMangledName(D);
|
|
|
|
llvm::GlobalValue *GV = GetGlobalValue(MangledName);
|
|
|
|
|
|
|
|
// We already have a definition, not declaration, with the same mangled name.
|
|
|
|
// Emitting of declaration is not required (and actually overwrites emitted
|
|
|
|
// definition).
|
|
|
|
if (GV && !GV->isDeclaration())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we have not seen a reference to this variable yet, place it into the
|
|
|
|
// deferred declarations table to be emitted if needed later.
|
|
|
|
if (!MustBeEmitted(D) && !GV) {
|
2009-09-11 07:43:36 +08:00
|
|
|
DeferredDecls[MangledName] = D;
|
2009-04-16 06:08:45 +08:00
|
|
|
return;
|
2009-04-22 03:28:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The tentative definition is the only definition.
|
2015-12-05 09:52:14 +08:00
|
|
|
EmitGlobalVarDefinition(D);
|
2009-04-16 06:08:45 +08:00
|
|
|
}
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
|
2015-07-25 00:04:29 +08:00
|
|
|
return Context.toCharUnitsFromBits(
|
|
|
|
getDataLayout().getTypeStoreSizeInBits(Ty));
|
2010-01-26 21:48:07 +08:00
|
|
|
}
|
|
|
|
|
2012-05-21 05:08:35 +08:00
|
|
|
unsigned CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D,
|
|
|
|
unsigned AddrSpace) {
|
2016-05-20 02:00:18 +08:00
|
|
|
if (D && LangOpts.CUDA && LangOpts.CUDAIsDevice) {
|
2012-05-21 05:08:35 +08:00
|
|
|
if (D->hasAttr<CUDAConstantAttr>())
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_constant);
|
|
|
|
else if (D->hasAttr<CUDASharedAttr>())
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_shared);
|
|
|
|
else
|
|
|
|
AddrSpace = getContext().getTargetAddressSpace(LangAS::cuda_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddrSpace;
|
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
template<typename SomeDecl>
|
|
|
|
void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D,
|
|
|
|
llvm::GlobalValue *GV) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Must have 'used' attribute, or else inline assembly can't rely on
|
|
|
|
// the name existing.
|
|
|
|
if (!D->template hasAttr<UsedAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Must have internal linkage and an ordinary name.
|
2013-05-13 08:12:11 +08:00
|
|
|
if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
|
2013-04-06 13:00:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Must be in an extern "C" context. Entities declared directly within
|
|
|
|
// a record are not extern "C" even if the record is in such a context.
|
2013-10-17 23:37:26 +08:00
|
|
|
const SomeDecl *First = D->getFirstDecl();
|
2013-05-06 04:15:21 +08:00
|
|
|
if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
|
2013-04-06 13:00:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// OK, this is an internal linkage entity inside an extern "C" linkage
|
|
|
|
// specification. Make a note of that so we can give it the "expected"
|
|
|
|
// mangled name if nothing else is using that name.
|
2013-04-06 15:07:44 +08:00
|
|
|
std::pair<StaticExternCMap::iterator, bool> R =
|
|
|
|
StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
|
2013-04-06 13:00:46 +08:00
|
|
|
|
|
|
|
// If we have multiple internal linkage entities with the same name
|
|
|
|
// in extern "C" regions, none of them gets that name.
|
2013-04-06 15:07:44 +08:00
|
|
|
if (!R.second)
|
2014-05-21 13:09:00 +08:00
|
|
|
R.first->second = nullptr;
|
2013-04-06 13:00:46 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 06:13:53 +08:00
|
|
|
static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
|
|
|
|
if (!CGM.supportsCOMDAT())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (D.hasAttr<SelectAnyAttr>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
GVALinkage Linkage;
|
|
|
|
if (auto *VD = dyn_cast<VarDecl>(&D))
|
|
|
|
Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
|
|
|
|
else
|
|
|
|
Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
|
|
|
|
|
|
|
|
switch (Linkage) {
|
|
|
|
case GVA_Internal:
|
|
|
|
case GVA_AvailableExternally:
|
|
|
|
case GVA_StrongExternal:
|
|
|
|
return false;
|
|
|
|
case GVA_DiscardableODR:
|
|
|
|
case GVA_StrongODR:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llvm_unreachable("No such linkage");
|
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:08 +08:00
|
|
|
void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
|
|
|
|
llvm::GlobalObject &GO) {
|
|
|
|
if (!shouldBeInCOMDAT(*this, D))
|
|
|
|
return;
|
|
|
|
GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
|
|
|
|
}
|
|
|
|
|
2016-01-14 18:41:16 +08:00
|
|
|
/// Pass IsTentative as true if you want to create a tentative definition.
|
|
|
|
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
|
|
|
|
bool IsTentative) {
|
2016-07-29 03:26:30 +08:00
|
|
|
// OpenCL global variables of sampler type are translated to function calls,
|
|
|
|
// therefore no need to be translated.
|
2008-05-31 03:50:47 +08:00
|
|
|
QualType ASTTy = D->getType();
|
2016-07-29 03:26:30 +08:00
|
|
|
if (getLangOpts().OpenCL && ASTTy->isSamplerT())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::Constant *Init = nullptr;
|
2012-02-14 06:16:19 +08:00
|
|
|
CXXRecordDecl *RD = ASTTy->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
|
|
|
|
bool NeedsGlobalCtor = false;
|
|
|
|
bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
const VarDecl *InitDecl;
|
|
|
|
const Expr *InitExpr = D->getAnyInitializer(InitDecl);
|
2012-02-26 04:51:20 +08:00
|
|
|
|
2016-02-03 06:29:48 +08:00
|
|
|
// CUDA E.2.4.1 "__shared__ variables cannot have an initialization
|
|
|
|
// as part of their declaration." Sema has already checked for
|
|
|
|
// error cases, so we just need to set Init to UndefValue.
|
|
|
|
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
|
|
|
|
D->hasAttr<CUDASharedAttr>())
|
2015-08-22 13:49:28 +08:00
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
|
2016-02-03 06:29:48 +08:00
|
|
|
else if (!InitExpr) {
|
2008-05-31 04:39:54 +08:00
|
|
|
// This is a tentative definition; tentative definitions are
|
2009-04-16 06:08:45 +08:00
|
|
|
// implicitly initialized with { 0 }.
|
|
|
|
//
|
|
|
|
// Note that tentative definitions are only emitted at the end of
|
|
|
|
// a translation unit, so they should never have incomplete
|
|
|
|
// type. In addition, EmitTentativeDefinition makes sure that we
|
|
|
|
// never attempt to emit a tentative definition if a real one
|
|
|
|
// exists. A use may still exists, however, so we still may need
|
|
|
|
// to do a RAUW.
|
|
|
|
assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
|
2009-08-03 05:18:22 +08:00
|
|
|
Init = EmitNullConstant(D->getType());
|
2008-05-31 03:50:47 +08:00
|
|
|
} else {
|
2013-06-13 06:31:48 +08:00
|
|
|
initializedGlobalDecl = GlobalDecl(D);
|
|
|
|
Init = EmitConstantInit(*InitDecl);
|
2012-02-26 04:51:20 +08:00
|
|
|
|
2009-02-20 09:18:21 +08:00
|
|
|
if (!Init) {
|
2010-01-27 01:43:42 +08:00
|
|
|
QualType T = InitExpr->getType();
|
2010-05-06 04:15:55 +08:00
|
|
|
if (D->getType()->isReferenceType())
|
|
|
|
T = D->getType();
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus) {
|
2009-08-09 07:24:23 +08:00
|
|
|
Init = EmitNullConstant(T);
|
2012-02-14 06:16:19 +08:00
|
|
|
NeedsGlobalCtor = true;
|
2009-08-09 07:24:23 +08:00
|
|
|
} else {
|
|
|
|
ErrorUnsupported(D, "static initializer");
|
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(T));
|
|
|
|
}
|
2010-07-16 07:40:35 +08:00
|
|
|
} else {
|
|
|
|
// We don't need an initializer, so remove the entry for the delayed
|
2012-02-14 06:16:19 +08:00
|
|
|
// initializer position (just in case this entry was delayed) if we
|
|
|
|
// also don't need to register a destructor.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
|
2010-07-16 07:40:35 +08:00
|
|
|
DelayedCXXInitPosition.erase(D);
|
2009-02-20 09:18:21 +08:00
|
|
|
}
|
2007-07-14 08:23:28 +08:00
|
|
|
}
|
2007-10-27 00:31:40 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type* InitType = Init->getType();
|
2016-01-14 18:41:16 +08:00
|
|
|
llvm::Constant *Entry =
|
2016-12-01 07:25:13 +08:00
|
|
|
GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Strip off a bitcast if we got one back.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
2009-07-17 00:48:25 +08:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast ||
|
2013-11-15 10:19:52 +08:00
|
|
|
CE->getOpcode() == llvm::Instruction::AddrSpaceCast ||
|
|
|
|
// All zero index gep.
|
2009-07-17 00:48:25 +08:00
|
|
|
CE->getOpcode() == llvm::Instruction::GetElementPtr);
|
2009-03-21 17:16:30 +08:00
|
|
|
Entry = CE->getOperand(0);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Entry is now either a Function or GlobalVariable.
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// We have a definition after a declaration with the wrong type.
|
|
|
|
// We must make a new GlobalVariable* and update everything that used OldGV
|
|
|
|
// (a declaration or tentative definition) with the new GlobalVariable*
|
|
|
|
// (which will be a definition).
|
|
|
|
//
|
|
|
|
// This happens if there is a prototype for a global (e.g.
|
|
|
|
// "extern int x[];") and then a definition of a different type (e.g.
|
|
|
|
// "int x[10];"). This also happens when an initializer has a different type
|
|
|
|
// from the type of the global (this happens with unions).
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!GV ||
|
2009-03-21 17:16:30 +08:00
|
|
|
GV->getType()->getElementType() != InitType ||
|
2011-03-19 06:38:29 +08:00
|
|
|
GV->getType()->getAddressSpace() !=
|
2012-05-21 05:08:35 +08:00
|
|
|
GetGlobalVarAddressSpace(D, getContext().getTargetAddressSpace(ASTTy))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
// Move the old entry aside so that we'll create a new one.
|
2011-07-23 18:55:15 +08:00
|
|
|
Entry->setName(StringRef());
|
2009-02-19 13:36:41 +08:00
|
|
|
|
2009-03-21 17:16:30 +08:00
|
|
|
// Make a new global with the correct type, this is now guaranteed to work.
|
2016-01-14 18:41:16 +08:00
|
|
|
GV = cast<llvm::GlobalVariable>(
|
2016-12-01 07:25:13 +08:00
|
|
|
GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)));
|
2009-03-21 17:25:43 +08:00
|
|
|
|
2008-05-31 03:50:47 +08:00
|
|
|
// Replace all uses of the old global with the new global
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2009-07-30 02:54:39 +08:00
|
|
|
llvm::ConstantExpr::getBitCast(GV, Entry->getType());
|
2009-03-21 17:16:30 +08:00
|
|
|
Entry->replaceAllUsesWith(NewPtrForOldDecl);
|
2008-05-31 03:50:47 +08:00
|
|
|
|
|
|
|
// Erase the old global, since it is no longer used.
|
2009-03-21 17:16:30 +08:00
|
|
|
cast<llvm::GlobalValue>(Entry)->eraseFromParent();
|
2008-05-31 03:50:47 +08:00
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
MaybeHandleStaticInExternC(D, GV);
|
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, GV);
|
2008-04-19 12:17:09 +08:00
|
|
|
|
2016-03-03 02:28:50 +08:00
|
|
|
// Set the llvm linkage type as appropriate.
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
getLLVMLinkageVarDefinition(D, GV->isConstant());
|
|
|
|
|
2015-08-22 13:49:28 +08:00
|
|
|
// CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
|
|
|
|
// the device. [...]"
|
|
|
|
// CUDA B.2.2 "The __constant__ qualifier, optionally used together with
|
|
|
|
// __device__, declares a variable that: [...]
|
|
|
|
// Is accessible from all the threads within the grid and from the host
|
|
|
|
// through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
|
|
|
|
// / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
|
2016-03-03 02:28:50 +08:00
|
|
|
if (GV && LangOpts.CUDA) {
|
|
|
|
if (LangOpts.CUDAIsDevice) {
|
|
|
|
if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>())
|
|
|
|
GV->setExternallyInitialized(true);
|
|
|
|
} else {
|
|
|
|
// Host-side shadows of external declarations of device-side
|
|
|
|
// global variables become internal definitions. These have to
|
|
|
|
// be internal in order to prevent name conflicts with global
|
|
|
|
// host variables with the same name in a different TUs.
|
|
|
|
if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()) {
|
|
|
|
Linkage = llvm::GlobalValue::InternalLinkage;
|
|
|
|
|
|
|
|
// Shadow variables and their properties must be registered
|
|
|
|
// with CUDA runtime.
|
|
|
|
unsigned Flags = 0;
|
|
|
|
if (!D->hasDefinition())
|
|
|
|
Flags |= CGCUDARuntime::ExternDeviceVar;
|
|
|
|
if (D->hasAttr<CUDAConstantAttr>())
|
|
|
|
Flags |= CGCUDARuntime::ConstantDeviceVar;
|
|
|
|
getCUDARuntime().registerDeviceVar(*GV, Flags);
|
|
|
|
} else if (D->hasAttr<CUDASharedAttr>())
|
|
|
|
// __shared__ variables are odd. Shadows do get created, but
|
|
|
|
// they are not registered with the CUDA runtime, so they
|
|
|
|
// can't really be used to access their device-side
|
|
|
|
// counterparts. It's not clear yet whether it's nvcc's bug or
|
|
|
|
// a feature, but we've got to do the same for compatibility.
|
|
|
|
Linkage = llvm::GlobalValue::InternalLinkage;
|
|
|
|
}
|
2015-08-22 13:49:28 +08:00
|
|
|
}
|
2007-07-13 13:13:43 +08:00
|
|
|
GV->setInitializer(Init);
|
2009-08-05 13:20:29 +08:00
|
|
|
|
|
|
|
// If it is safe to mark the global 'constant', do so now.
|
2012-02-17 14:48:11 +08:00
|
|
|
GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
|
|
|
|
isTypeConstant(D->getType(), true));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-10-17 04:52:46 +08:00
|
|
|
// If it is in a read-only section, mark it 'constant'.
|
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
|
|
|
|
const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
|
|
|
|
if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
|
|
|
|
GV->setConstant(true);
|
|
|
|
}
|
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
|
2012-02-17 14:48:11 +08:00
|
|
|
|
2014-06-11 12:08:55 +08:00
|
|
|
|
2015-11-12 06:42:31 +08:00
|
|
|
// On Darwin, if the normal linkage of a C++ thread_local variable is
|
|
|
|
// LinkOnce or Weak, we keep the normal linkage to prevent multiple
|
|
|
|
// copies within a linkage unit; otherwise, the backing variable has
|
|
|
|
// internal linkage and all accesses should just be calls to the
|
2014-06-11 12:08:55 +08:00
|
|
|
// Itanium-specified entry point, which has the normal linkage of the
|
2015-11-12 06:42:31 +08:00
|
|
|
// variable. This is to preserve the ability to change the implementation
|
|
|
|
// behind the scenes.
|
2014-10-16 06:38:23 +08:00
|
|
|
if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
|
2015-11-12 07:08:18 +08:00
|
|
|
Context.getTargetInfo().getTriple().isOSDarwin() &&
|
2015-11-12 06:42:31 +08:00
|
|
|
!llvm::GlobalVariable::isLinkOnceLinkage(Linkage) &&
|
|
|
|
!llvm::GlobalVariable::isWeakLinkage(Linkage))
|
2014-10-16 06:38:23 +08:00
|
|
|
Linkage = llvm::GlobalValue::InternalLinkage;
|
2014-06-11 12:08:55 +08:00
|
|
|
|
2010-10-28 00:21:54 +08:00
|
|
|
GV->setLinkage(Linkage);
|
2014-01-14 23:23:53 +08:00
|
|
|
if (D->hasAttr<DLLImportAttr>())
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
|
|
|
|
else if (D->hasAttr<DLLExportAttr>())
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
|
2014-11-03 22:24:45 +08:00
|
|
|
else
|
|
|
|
GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
|
2013-11-21 08:15:56 +08:00
|
|
|
|
2016-12-15 16:09:08 +08:00
|
|
|
if (Linkage == llvm::GlobalVariable::CommonLinkage) {
|
2010-10-28 00:21:54 +08:00
|
|
|
// common vars aren't constant even if declared const.
|
|
|
|
GV->setConstant(false);
|
2016-12-15 16:09:08 +08:00
|
|
|
// Tentative definition of global variables may be initialized with
|
|
|
|
// non-zero null pointers. In this case they should have weak linkage
|
|
|
|
// since common linkage must have zero initializer and must not have
|
|
|
|
// explicit section therefore cannot have non-zero initial value.
|
|
|
|
if (!GV->getInitializer()->isNullValue())
|
|
|
|
GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
|
|
|
|
}
|
2010-10-28 00:21:54 +08:00
|
|
|
|
2014-05-06 04:21:03 +08:00
|
|
|
setNonAliasAttributes(D, GV);
|
2010-10-28 00:21:54 +08:00
|
|
|
|
2014-10-16 06:38:23 +08:00
|
|
|
if (D->getTLSKind() && !GV->isThreadLocal()) {
|
|
|
|
if (D->getTLSKind() == VarDecl::TLS_Dynamic)
|
2015-12-01 09:10:48 +08:00
|
|
|
CXXThreadLocals.push_back(D);
|
2014-10-16 06:38:23 +08:00
|
|
|
setTLSMode(GV, *D);
|
|
|
|
}
|
|
|
|
|
2015-01-16 05:36:08 +08:00
|
|
|
maybeSetTrivialComdat(*D, *GV);
|
2015-01-13 06:13:53 +08:00
|
|
|
|
2010-11-06 17:44:32 +08:00
|
|
|
// Emit the initializer function if necessary.
|
2012-02-14 06:16:19 +08:00
|
|
|
if (NeedsGlobalCtor || NeedsGlobalDtor)
|
|
|
|
EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
|
2010-11-06 17:44:32 +08:00
|
|
|
|
2014-08-02 05:35:28 +08:00
|
|
|
SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
|
2012-08-21 14:53:28 +08:00
|
|
|
|
2010-10-28 00:21:54 +08:00
|
|
|
// Emit global variable debug information.
|
2011-10-14 05:45:18 +08:00
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
2016-02-02 19:06:51 +08:00
|
|
|
if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
|
2012-05-04 15:39:27 +08:00
|
|
|
DI->EmitGlobalVariable(GV, D);
|
2010-10-28 00:21:54 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 08:01:13 +08:00
|
|
|
static bool isVarDeclStrongDefinition(const ASTContext &Context,
|
2015-04-16 07:04:24 +08:00
|
|
|
CodeGenModule &CGM, const VarDecl *D,
|
|
|
|
bool NoCommon) {
|
2014-04-11 00:53:16 +08:00
|
|
|
// Don't give variables common linkage if -fno-common was specified unless it
|
|
|
|
// was overridden by a NoCommon attribute.
|
|
|
|
if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// C11 6.9.2/2:
|
|
|
|
// A declaration of an identifier for an object that has file scope without
|
|
|
|
// an initializer, and without a storage-class specifier or with the
|
|
|
|
// storage-class specifier static, constitutes a tentative definition.
|
|
|
|
if (D->getInit() || D->hasExternalStorage())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// A variable cannot be both common and exist in a section.
|
|
|
|
if (D->hasAttr<SectionAttr>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Thread local vars aren't considered common linkage.
|
|
|
|
if (D->getTLSKind())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Tentative definitions marked with WeakImportAttr are true definitions.
|
|
|
|
if (D->hasAttr<WeakImportAttr>())
|
|
|
|
return true;
|
|
|
|
|
2015-04-16 07:04:24 +08:00
|
|
|
// A variable cannot be both common and exist in a comdat.
|
|
|
|
if (shouldBeInCOMDAT(CGM, *D))
|
|
|
|
return true;
|
|
|
|
|
2016-07-01 05:02:40 +08:00
|
|
|
// Declarations with a required alignment do not have common linkage in MSVC
|
2014-08-05 08:01:13 +08:00
|
|
|
// mode.
|
2015-10-08 12:53:31 +08:00
|
|
|
if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
|
2015-02-03 16:49:32 +08:00
|
|
|
if (D->hasAttr<AlignedAttr>())
|
|
|
|
return true;
|
|
|
|
QualType VarType = D->getType();
|
|
|
|
if (Context.isAlignmentRequired(VarType))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const auto *RT = VarType->getAs<RecordType>()) {
|
|
|
|
const RecordDecl *RD = RT->getDecl();
|
|
|
|
for (const FieldDecl *FD : RD->fields()) {
|
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
|
|
|
if (FD->hasAttr<AlignedAttr>())
|
|
|
|
return true;
|
|
|
|
if (Context.isAlignmentRequired(FD->getType()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 08:01:13 +08:00
|
|
|
|
2014-04-11 00:53:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 03:54:53 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
|
2014-05-28 09:52:23 +08:00
|
|
|
const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
|
2009-10-15 05:36:34 +08:00
|
|
|
if (Linkage == GVA_Internal)
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::Function::InternalLinkage;
|
2014-04-29 06:17:59 +08:00
|
|
|
|
|
|
|
if (D->hasAttr<WeakAttr>()) {
|
|
|
|
if (IsConstantVariable)
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
2009-08-05 13:20:29 +08:00
|
|
|
else
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::WeakAnyLinkage;
|
2014-04-29 06:17:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are guaranteed to have a strong definition somewhere else,
|
|
|
|
// so we can use available_externally linkage.
|
|
|
|
if (Linkage == GVA_AvailableExternally)
|
2017-01-30 13:00:26 +08:00
|
|
|
return llvm::GlobalValue::AvailableExternallyLinkage;
|
2014-04-29 06:17:59 +08:00
|
|
|
|
|
|
|
// Note that Apple's kernel linker doesn't support symbol
|
|
|
|
// coalescing, so we need to avoid linkonce and weak linkages there.
|
|
|
|
// Normally, this means we just map to internal, but for explicit
|
|
|
|
// instantiations we'll map to external.
|
|
|
|
|
|
|
|
// In C++, the compiler has to emit a definition in every translation unit
|
|
|
|
// that references the function. We should use linkonce_odr because
|
|
|
|
// a) if all references in this translation unit are optimized away, we
|
|
|
|
// don't need to codegen it. b) if the function persists, it needs to be
|
|
|
|
// merged with other definitions. c) C++ has the ODR, so we know the
|
|
|
|
// definition is dependable.
|
|
|
|
if (Linkage == GVA_DiscardableODR)
|
2014-05-16 06:07:49 +08:00
|
|
|
return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
|
2014-04-29 06:17:59 +08:00
|
|
|
: llvm::Function::InternalLinkage;
|
|
|
|
|
|
|
|
// An explicit instantiation of a template has weak linkage, since
|
|
|
|
// explicit instantiations can occur in multiple translation units
|
|
|
|
// and must all be equivalent. However, we are not allowed to
|
|
|
|
// throw away these explicit instantiations.
|
2016-07-01 02:41:33 +08:00
|
|
|
//
|
|
|
|
// We don't currently support CUDA device code spread out across multiple TUs,
|
|
|
|
// so say that CUDA templates are either external (for kernels) or internal.
|
|
|
|
// This lets llvm perform aggressive inter-procedural optimizations.
|
|
|
|
if (Linkage == GVA_StrongODR) {
|
|
|
|
if (Context.getLangOpts().AppleKext)
|
|
|
|
return llvm::Function::ExternalLinkage;
|
|
|
|
if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice)
|
|
|
|
return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
|
|
|
|
: llvm::Function::InternalLinkage;
|
|
|
|
return llvm::Function::WeakODRLinkage;
|
|
|
|
}
|
2014-04-29 06:17:59 +08:00
|
|
|
|
|
|
|
// C++ doesn't have tentative definitions and thus cannot have common
|
|
|
|
// linkage.
|
|
|
|
if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
|
2015-04-16 07:04:24 +08:00
|
|
|
!isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
|
2014-08-05 08:01:13 +08:00
|
|
|
CodeGenOpts.NoCommon))
|
2014-04-11 00:53:16 +08:00
|
|
|
return llvm::GlobalVariable::CommonLinkage;
|
|
|
|
|
2014-04-29 06:17:59 +08:00
|
|
|
// selectany symbols are externally visible, so use weak instead of
|
|
|
|
// linkonce. MSVC optimizes away references to const selectany globals, so
|
|
|
|
// all definitions should be the same and ODR linkage should be used.
|
|
|
|
// http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
|
|
|
|
if (D->hasAttr<SelectAnyAttr>())
|
|
|
|
return llvm::GlobalVariable::WeakODRLinkage;
|
|
|
|
|
|
|
|
// Otherwise, we have strong external linkage.
|
|
|
|
assert(Linkage == GVA_StrongExternal);
|
2010-10-28 00:21:54 +08:00
|
|
|
return llvm::GlobalVariable::ExternalLinkage;
|
2007-07-13 13:13:43 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2014-04-29 06:17:59 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
|
|
|
|
const VarDecl *VD, bool IsConstant) {
|
|
|
|
GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD);
|
2014-05-28 09:52:23 +08:00
|
|
|
return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
|
2014-04-29 06:17:59 +08:00
|
|
|
}
|
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
/// Replace the uses of a function that was declared with a non-proto type.
|
|
|
|
/// We want to silently drop extra arguments from call sites
|
|
|
|
static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
|
|
|
|
llvm::Function *newFn) {
|
|
|
|
// Fast path.
|
|
|
|
if (old->use_empty()) return;
|
|
|
|
|
|
|
|
llvm::Type *newRetTy = newFn->getReturnType();
|
|
|
|
SmallVector<llvm::Value*, 4> newArgs;
|
2015-12-16 05:27:59 +08:00
|
|
|
SmallVector<llvm::OperandBundleDef, 1> newBundles;
|
2012-12-13 06:21:47 +08:00
|
|
|
|
|
|
|
for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
|
|
|
|
ui != ue; ) {
|
|
|
|
llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
|
2014-03-09 11:16:50 +08:00
|
|
|
llvm::User *user = use->getUser();
|
2012-12-13 06:21:47 +08:00
|
|
|
|
|
|
|
// Recognize and replace uses of bitcasts. Most calls to
|
|
|
|
// unprototyped functions will use bitcasts.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
|
2012-12-13 06:21:47 +08:00
|
|
|
if (bitcast->getOpcode() == llvm::Instruction::BitCast)
|
|
|
|
replaceUsesOfNonProtoConstant(bitcast, newFn);
|
2009-05-05 14:16:31 +08:00
|
|
|
continue;
|
2012-12-13 06:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recognize calls to the function.
|
|
|
|
llvm::CallSite callSite(user);
|
|
|
|
if (!callSite) continue;
|
2014-03-09 11:16:50 +08:00
|
|
|
if (!callSite.isCallee(&*use)) continue;
|
2009-05-05 14:16:31 +08:00
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
// If the return types don't match exactly, then we can't
|
|
|
|
// transform this call unless it's dead.
|
|
|
|
if (callSite->getType() != newRetTy && !callSite->use_empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the call site's attribute list.
|
2013-01-27 10:46:53 +08:00
|
|
|
SmallVector<llvm::AttributeSet, 8> newAttrs;
|
2012-12-13 06:21:47 +08:00
|
|
|
llvm::AttributeSet oldAttrs = callSite.getAttributes();
|
|
|
|
|
|
|
|
// Collect any return attributes from the call.
|
2013-01-22 05:57:40 +08:00
|
|
|
if (oldAttrs.hasAttributes(llvm::AttributeSet::ReturnIndex))
|
2013-01-22 06:45:00 +08:00
|
|
|
newAttrs.push_back(
|
2013-01-27 10:46:53 +08:00
|
|
|
llvm::AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getRetAttributes()));
|
2012-12-13 06:21:47 +08:00
|
|
|
|
|
|
|
// If the function was passed too few arguments, don't transform.
|
|
|
|
unsigned newNumArgs = newFn->arg_size();
|
|
|
|
if (callSite.arg_size() < newNumArgs) continue;
|
|
|
|
|
|
|
|
// If extra arguments were passed, we silently drop them.
|
|
|
|
// If any of the types mismatch, we don't transform.
|
|
|
|
unsigned argNo = 0;
|
|
|
|
bool dontTransform = false;
|
|
|
|
for (llvm::Function::arg_iterator ai = newFn->arg_begin(),
|
|
|
|
ae = newFn->arg_end(); ai != ae; ++ai, ++argNo) {
|
|
|
|
if (callSite.getArgument(argNo)->getType() != ai->getType()) {
|
|
|
|
dontTransform = true;
|
2009-05-05 14:16:31 +08:00
|
|
|
break;
|
|
|
|
}
|
2011-08-03 08:43:55 +08:00
|
|
|
|
|
|
|
// Add any parameter attributes.
|
2013-01-23 14:15:10 +08:00
|
|
|
if (oldAttrs.hasAttributes(argNo + 1))
|
|
|
|
newAttrs.
|
2013-01-27 10:46:53 +08:00
|
|
|
push_back(llvm::
|
|
|
|
AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getParamAttributes(argNo + 1)));
|
2009-05-05 14:16:31 +08:00
|
|
|
}
|
2012-12-13 06:21:47 +08:00
|
|
|
if (dontTransform)
|
2009-05-05 14:16:31 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-19 05:26:07 +08:00
|
|
|
if (oldAttrs.hasAttributes(llvm::AttributeSet::FunctionIndex))
|
2013-01-27 10:46:53 +08:00
|
|
|
newAttrs.push_back(llvm::AttributeSet::get(newFn->getContext(),
|
|
|
|
oldAttrs.getFnAttributes()));
|
2011-08-03 08:43:55 +08:00
|
|
|
|
2009-05-05 14:16:31 +08:00
|
|
|
// Okay, we can transform this. Create the new call instruction and copy
|
|
|
|
// over the required information.
|
2012-12-13 06:21:47 +08:00
|
|
|
newArgs.append(callSite.arg_begin(), callSite.arg_begin() + argNo);
|
|
|
|
|
2015-12-16 05:27:59 +08:00
|
|
|
// Copy over any operand bundles.
|
|
|
|
callSite.getOperandBundlesAsDefs(newBundles);
|
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
llvm::CallSite newCall;
|
|
|
|
if (callSite.isCall()) {
|
2015-12-16 05:27:59 +08:00
|
|
|
newCall = llvm::CallInst::Create(newFn, newArgs, newBundles, "",
|
2012-12-13 06:21:47 +08:00
|
|
|
callSite.getInstruction());
|
|
|
|
} else {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *oldInvoke = cast<llvm::InvokeInst>(callSite.getInstruction());
|
2012-12-13 06:21:47 +08:00
|
|
|
newCall = llvm::InvokeInst::Create(newFn,
|
|
|
|
oldInvoke->getNormalDest(),
|
|
|
|
oldInvoke->getUnwindDest(),
|
2015-12-16 05:27:59 +08:00
|
|
|
newArgs, newBundles, "",
|
2012-12-13 06:21:47 +08:00
|
|
|
callSite.getInstruction());
|
|
|
|
}
|
|
|
|
newArgs.clear(); // for the next iteration
|
|
|
|
|
|
|
|
if (!newCall->getType()->isVoidTy())
|
|
|
|
newCall->takeName(callSite.getInstruction());
|
|
|
|
newCall.setAttributes(
|
|
|
|
llvm::AttributeSet::get(newFn->getContext(), newAttrs));
|
|
|
|
newCall.setCallingConv(callSite.getCallingConv());
|
2009-05-05 14:16:31 +08:00
|
|
|
|
|
|
|
// Finally, remove the old call, replacing any uses with the new one.
|
2012-12-13 06:21:47 +08:00
|
|
|
if (!callSite->use_empty())
|
|
|
|
callSite->replaceAllUsesWith(newCall.getInstruction());
|
2009-10-14 01:02:04 +08:00
|
|
|
|
2010-04-01 14:31:43 +08:00
|
|
|
// Copy debug location attached to CI.
|
2015-03-31 04:01:41 +08:00
|
|
|
if (callSite->getDebugLoc())
|
2012-12-13 06:21:47 +08:00
|
|
|
newCall->setDebugLoc(callSite->getDebugLoc());
|
2015-12-16 05:27:59 +08:00
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
callSite->eraseFromParent();
|
2009-05-05 14:16:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 06:21:47 +08:00
|
|
|
/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
|
|
|
|
/// implement a function with no prototype, e.g. "int foo() {}". If there are
|
|
|
|
/// existing call uses of the old function in the module, this adjusts them to
|
|
|
|
/// call the new function directly.
|
|
|
|
///
|
|
|
|
/// This is not just a cleanup: the always_inline pass requires direct calls to
|
|
|
|
/// functions to be able to inline them. If there is a bitcast in the way, it
|
|
|
|
/// won't inline them. Instcombine normally deletes these calls, but it isn't
|
|
|
|
/// run at -O0.
|
|
|
|
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
|
|
|
|
llvm::Function *NewFn) {
|
|
|
|
// If we're redefining a global as a function, don't transform it.
|
|
|
|
if (!isa<llvm::Function>(Old)) return;
|
|
|
|
|
|
|
|
replaceUsesOfNonProtoConstant(Old, NewFn);
|
|
|
|
}
|
|
|
|
|
2012-03-08 23:51:03 +08:00
|
|
|
void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) {
|
2016-07-11 12:28:21 +08:00
|
|
|
auto DK = VD->isThisDeclarationADefinition();
|
|
|
|
if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
|
|
|
|
return;
|
|
|
|
|
2012-03-08 23:51:03 +08:00
|
|
|
TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind();
|
|
|
|
// If we have a definition, this might be a deferred decl. If the
|
|
|
|
// instantiation is explicit, make sure we emit it at the end.
|
|
|
|
if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition)
|
|
|
|
GetAddrOfGlobalVar(VD);
|
2013-02-24 08:05:01 +08:00
|
|
|
|
|
|
|
EmitTopLevelDecl(VD);
|
2012-03-05 18:54:55 +08:00
|
|
|
}
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2013-12-10 00:01:03 +08:00
|
|
|
void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
|
|
|
|
llvm::GlobalValue *GV) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *D = cast<FunctionDecl>(GD.getDecl());
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2011-03-09 16:12:35 +08:00
|
|
|
// Compute the function info and LLVM type.
|
2012-02-17 11:33:10 +08:00
|
|
|
const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
|
2011-03-09 12:27:21 +08:00
|
|
|
|
2009-05-13 04:58:15 +08:00
|
|
|
// Get or create the prototype for the function.
|
2015-08-31 21:20:44 +08:00
|
|
|
if (!GV || (GV->getType()->getElementType() != Ty))
|
|
|
|
GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
|
|
|
|
/*DontDefer=*/true,
|
2016-12-01 07:25:13 +08:00
|
|
|
ForDefinition));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-08-31 21:20:44 +08:00
|
|
|
// Already emitted.
|
|
|
|
if (!GV->isDeclaration())
|
2015-04-15 16:44:40 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-03 05:04:24 +08:00
|
|
|
// We need to set linkage and visibility on the function before
|
|
|
|
// generating code for it because various parts of IR generation
|
|
|
|
// want to propagate this information down (e.g. to local static
|
|
|
|
// declarations).
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Fn = cast<llvm::Function>(GV);
|
2013-06-06 01:49:37 +08:00
|
|
|
setFunctionLinkage(GD, Fn);
|
2015-05-29 01:44:56 +08:00
|
|
|
setFunctionDLLStorageClass(GD, Fn);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2014-05-08 23:26:12 +08:00
|
|
|
// FIXME: this is redundant with part of setFunctionDefinitionAttributes
|
2011-01-30 03:39:23 +08:00
|
|
|
setGlobalVisibility(Fn, D);
|
2010-11-03 05:04:24 +08:00
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
MaybeHandleStaticInExternC(D, Fn);
|
|
|
|
|
2015-01-16 05:36:08 +08:00
|
|
|
maybeSetTrivialComdat(*D, *Fn);
|
2015-01-13 06:13:53 +08:00
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
CodeGenFunction(*this).GenerateCode(D, Fn, FI);
|
2008-08-12 01:36:14 +08:00
|
|
|
|
2014-05-08 23:26:12 +08:00
|
|
|
setFunctionDefinitionAttributes(D, Fn);
|
2009-04-14 16:05:55 +08:00
|
|
|
SetLLVMFunctionAttributesForDefinition(D, Fn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
|
2008-09-09 07:44:31 +08:00
|
|
|
AddGlobalCtor(Fn, CA->getPriority());
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
|
2008-09-09 07:44:31 +08:00
|
|
|
AddGlobalDtor(Fn, DA->getPriority());
|
2011-09-10 06:41:49 +08:00
|
|
|
if (D->hasAttr<AnnotateAttr>())
|
|
|
|
AddGlobalAnnotations(D, Fn);
|
2008-07-30 07:18:29 +08:00
|
|
|
}
|
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *D = cast<ValueDecl>(GD.getDecl());
|
2009-06-30 10:34:44 +08:00
|
|
|
const AliasAttr *AA = D->getAttr<AliasAttr>();
|
2009-03-23 05:47:11 +08:00
|
|
|
assert(AA && "Not an alias?");
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(GD);
|
2010-03-20 07:29:14 +08:00
|
|
|
|
Don't crash on a self-alias declaration
We were crashing in CodeGen given input like this:
int self_alias(void) __attribute__((weak, alias("self_alias")));
such a self-alias is invalid, but instead of diagnosing the situation, we'd
proceed to produce IR for both the function declaration and the alias. Because
we already had a function named 'self_alias', the alias could not be named the
same thing, and so LLVM would pick a different name ('self_alias1' for example)
for that value. When we later called CodeGenModule::checkAliases, we'd look up
the IR value corresponding to the alias name, find the function declaration
instead, and then assert in a cast to llvm::GlobalAlias. The easiest way to prevent
this is simply to avoid creating the wrongly-named alias value in the first
place and issue the diagnostic there (instead of in checkAliases). We detect a
related cycle case in CodeGenModule::EmitAliasDefinition already, so this just
adds a second such check.
Even though the other test cases for this 'alias definition is part of a cycle'
diagnostic are in test/Sema/attr-alias-elf.c, I've added a separate regression
test for this case. This is because I can't add this check to
test/Sema/attr-alias-elf.c without disturbing the other test cases in that
file. In order to avoid construction of the bad IR values, this diagnostic
is emitted from within CodeGenModule::EmitAliasDefinition (and the relevant
declaration is not added to the Aliases vector). The other cycle checks are
done within the CodeGenModule::checkAliases function based on the Aliases
vector, called from CodeGenModule::Release. However, if there have been errors
earlier, HandleTranslationUnit does not call Release, and so checkAliases is
never called, and so none of the other diagnostics would be produced.
Fixes PR23509.
llvm-svn: 246882
2015-09-05 05:49:21 +08:00
|
|
|
if (AA->getAliasee() == MangledName) {
|
2016-04-11 15:48:59 +08:00
|
|
|
Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
|
Don't crash on a self-alias declaration
We were crashing in CodeGen given input like this:
int self_alias(void) __attribute__((weak, alias("self_alias")));
such a self-alias is invalid, but instead of diagnosing the situation, we'd
proceed to produce IR for both the function declaration and the alias. Because
we already had a function named 'self_alias', the alias could not be named the
same thing, and so LLVM would pick a different name ('self_alias1' for example)
for that value. When we later called CodeGenModule::checkAliases, we'd look up
the IR value corresponding to the alias name, find the function declaration
instead, and then assert in a cast to llvm::GlobalAlias. The easiest way to prevent
this is simply to avoid creating the wrongly-named alias value in the first
place and issue the diagnostic there (instead of in checkAliases). We detect a
related cycle case in CodeGenModule::EmitAliasDefinition already, so this just
adds a second such check.
Even though the other test cases for this 'alias definition is part of a cycle'
diagnostic are in test/Sema/attr-alias-elf.c, I've added a separate regression
test for this case. This is because I can't add this check to
test/Sema/attr-alias-elf.c without disturbing the other test cases in that
file. In order to avoid construction of the bad IR values, this diagnostic
is emitted from within CodeGenModule::EmitAliasDefinition (and the relevant
declaration is not added to the Aliases vector). The other cycle checks are
done within the CodeGenModule::checkAliases function based on the Aliases
vector, called from CodeGenModule::Release. However, if there have been errors
earlier, HandleTranslationUnit does not call Release, and so checkAliases is
never called, and so none of the other diagnostics would be produced.
Fixes PR23509.
llvm-svn: 246882
2015-09-05 05:49:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
// If there is a definition in the module, then it wins over the alias.
|
|
|
|
// This is dubious, but allow it to be safe. Just ignore the alias.
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-10-23 03:26:13 +08:00
|
|
|
Aliases.push_back(GD);
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
|
2009-03-23 05:47:11 +08:00
|
|
|
|
|
|
|
// Create a reference to the named value. This ensures that it is emitted
|
|
|
|
// if a deferred decl.
|
|
|
|
llvm::Constant *Aliasee;
|
|
|
|
if (isa<llvm::FunctionType>(DeclTy))
|
2012-10-06 07:12:53 +08:00
|
|
|
Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
|
2011-02-05 12:35:53 +08:00
|
|
|
/*ForVTable=*/false);
|
2009-03-23 05:47:11 +08:00
|
|
|
else
|
2010-03-20 07:29:14 +08:00
|
|
|
Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::PointerType::getUnqual(DeclTy),
|
2014-10-16 06:38:23 +08:00
|
|
|
/*D=*/nullptr);
|
2009-03-23 05:47:11 +08:00
|
|
|
|
|
|
|
// Create the new alias itself, but don't set a name yet.
|
2014-05-18 05:30:14 +08:00
|
|
|
auto *GA = llvm::GlobalAlias::create(
|
2015-09-15 02:38:22 +08:00
|
|
|
DeclTy, 0, llvm::Function::ExternalLinkage, "", Aliasee, &getModule());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
if (Entry) {
|
2014-05-17 03:35:48 +08:00
|
|
|
if (GA->getAliasee() == Entry) {
|
2016-04-11 15:48:59 +08:00
|
|
|
Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
|
2014-05-17 03:35:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-20 07:29:14 +08:00
|
|
|
assert(Entry->isDeclaration());
|
|
|
|
|
2009-03-23 05:47:11 +08:00
|
|
|
// If there is a declaration in the module, then we had an extern followed
|
|
|
|
// by the alias, as in:
|
|
|
|
// extern int test6();
|
|
|
|
// ...
|
|
|
|
// int test6() __attribute__((alias("test7")));
|
|
|
|
//
|
|
|
|
// Remove it and replace uses of it with the alias.
|
2010-03-20 07:29:14 +08:00
|
|
|
GA->takeName(Entry);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-30 02:54:39 +08:00
|
|
|
Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
|
2009-03-23 05:47:11 +08:00
|
|
|
Entry->getType()));
|
|
|
|
Entry->eraseFromParent();
|
2010-03-20 07:29:14 +08:00
|
|
|
} else {
|
2010-06-23 00:16:50 +08:00
|
|
|
GA->setName(MangledName);
|
2009-03-23 05:47:11 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-14 16:05:55 +08:00
|
|
|
// Set attributes which are particular to an alias; this is a
|
|
|
|
// specialization of the attributes which may be set on a global
|
|
|
|
// variable/function.
|
2014-10-08 08:00:09 +08:00
|
|
|
if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
|
|
|
|
D->isWeakImported()) {
|
2009-04-14 16:05:55 +08:00
|
|
|
GA->setLinkage(llvm::Function::WeakAnyLinkage);
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:38:23 +08:00
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(D))
|
|
|
|
if (VD->getTLSKind())
|
|
|
|
setTLSMode(GA, *VD);
|
|
|
|
|
2014-10-08 08:00:09 +08:00
|
|
|
setAliasAttributes(D, GA);
|
2009-03-23 05:47:11 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 15:48:59 +08:00
|
|
|
void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
|
|
|
|
const auto *D = cast<ValueDecl>(GD.getDecl());
|
|
|
|
const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
|
|
|
|
assert(IFA && "Not an ifunc?");
|
|
|
|
|
|
|
|
StringRef MangledName = getMangledName(GD);
|
|
|
|
|
|
|
|
if (IFA->getResolver() == MangledName) {
|
|
|
|
Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report an error if some definition overrides ifunc.
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
|
|
|
if (Entry && !Entry->isDeclaration()) {
|
|
|
|
GlobalDecl OtherGD;
|
|
|
|
if (lookupRepresentativeDecl(MangledName, OtherGD) &&
|
|
|
|
DiagnosedConflictingDefinitions.insert(GD).second) {
|
|
|
|
Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name);
|
|
|
|
Diags.Report(OtherGD.getDecl()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Aliases.push_back(GD);
|
|
|
|
|
|
|
|
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
|
|
|
|
llvm::Constant *Resolver =
|
|
|
|
GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD,
|
|
|
|
/*ForVTable=*/false);
|
|
|
|
llvm::GlobalIFunc *GIF =
|
|
|
|
llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,
|
|
|
|
"", Resolver, &getModule());
|
|
|
|
if (Entry) {
|
|
|
|
if (GIF->getResolver() == Entry) {
|
|
|
|
Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(Entry->isDeclaration());
|
|
|
|
|
|
|
|
// If there is a declaration in the module, then we had an extern followed
|
|
|
|
// by the ifunc, as in:
|
|
|
|
// extern int test();
|
|
|
|
// ...
|
|
|
|
// int test() __attribute__((ifunc("resolver")));
|
|
|
|
//
|
|
|
|
// Remove it and replace uses of it with the ifunc.
|
|
|
|
GIF->takeName(Entry);
|
|
|
|
|
|
|
|
Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF,
|
|
|
|
Entry->getType()));
|
|
|
|
Entry->eraseFromParent();
|
|
|
|
} else
|
|
|
|
GIF->setName(MangledName);
|
|
|
|
|
|
|
|
SetCommonAttributes(D, GIF);
|
|
|
|
}
|
|
|
|
|
2011-07-15 01:45:50 +08:00
|
|
|
llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
|
2011-07-24 01:14:25 +08:00
|
|
|
ArrayRef<llvm::Type*> Tys) {
|
2011-07-12 22:06:48 +08:00
|
|
|
return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
|
2011-07-15 01:45:50 +08:00
|
|
|
Tys);
|
2007-12-18 08:25:38 +08:00
|
|
|
}
|
2007-08-31 12:31:45 +08:00
|
|
|
|
2015-04-06 06:47:07 +08:00
|
|
|
static llvm::StringMapEntry<llvm::GlobalVariable *> &
|
|
|
|
GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
|
|
|
|
const StringLiteral *Literal, bool TargetIsLSB,
|
|
|
|
bool &IsUTF16, unsigned &StringLength) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef String = Literal->getString();
|
2010-08-17 20:54:38 +08:00
|
|
|
unsigned NumBytes = String.size();
|
2009-07-24 06:52:48 +08:00
|
|
|
|
2009-09-22 18:03:52 +08:00
|
|
|
// Check for simple case.
|
|
|
|
if (!Literal->containsNonAsciiOrNull()) {
|
|
|
|
StringLength = NumBytes;
|
2014-11-19 11:06:06 +08:00
|
|
|
return *Map.insert(std::make_pair(String, nullptr)).first;
|
2009-09-22 18:03:52 +08:00
|
|
|
}
|
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
// Otherwise, convert the UTF8 literals into a string of shorts.
|
|
|
|
IsUTF16 = true;
|
|
|
|
|
2016-09-30 08:38:45 +08:00
|
|
|
SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
|
|
|
|
const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
|
|
|
|
llvm::UTF16 *ToPtr = &ToBuf[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-09-30 08:38:45 +08:00
|
|
|
(void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
|
|
|
|
ToPtr + NumBytes, llvm::strictConversion);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 07:41:22 +08:00
|
|
|
// ConvertUTF8toUTF16 returns the length in ToPtr.
|
2009-07-24 06:52:48 +08:00
|
|
|
StringLength = ToPtr - &ToBuf[0];
|
2009-07-24 07:41:22 +08:00
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
// Add an explicit null.
|
|
|
|
*ToPtr = 0;
|
2014-11-19 11:06:06 +08:00
|
|
|
return *Map.insert(std::make_pair(
|
|
|
|
StringRef(reinterpret_cast<const char *>(ToBuf.data()),
|
|
|
|
(StringLength + 1) * 2),
|
|
|
|
nullptr)).first;
|
2009-07-24 06:52:48 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
ConstantAddress
|
2009-07-24 06:52:48 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) {
|
|
|
|
unsigned StringLength = 0;
|
|
|
|
bool isUTF16 = false;
|
2015-04-06 06:47:07 +08:00
|
|
|
llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
|
|
|
|
GetConstantCFStringEntry(CFConstantStringMap, Literal,
|
|
|
|
getDataLayout().isLittleEndian(), isUTF16,
|
|
|
|
StringLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
if (auto *C = Entry.second)
|
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 ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
2008-08-24 02:37:06 +08:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2016-05-31 00:23:07 +08:00
|
|
|
|
2009-07-17 00:48:25 +08:00
|
|
|
// If we don't already have it, get __CFConstantStringClassReference.
|
2007-08-21 08:21:21 +08:00
|
|
|
if (!CFConstantStringClassRef) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::ArrayType::get(Ty, 0);
|
2016-05-31 00:23:07 +08:00
|
|
|
llvm::Constant *GV =
|
|
|
|
CreateRuntimeVariable(Ty, "__CFConstantStringClassReference");
|
2016-06-01 12:22:24 +08:00
|
|
|
|
2016-09-14 23:17:46 +08:00
|
|
|
if (getTriple().isOSBinFormatCOFF()) {
|
2016-06-01 12:22:24 +08:00
|
|
|
IdentifierInfo &II = getContext().Idents.get(GV->getName());
|
|
|
|
TranslationUnitDecl *TUDecl = getContext().getTranslationUnitDecl();
|
|
|
|
DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
|
|
|
|
llvm::GlobalValue *CGV = cast<llvm::GlobalValue>(GV);
|
|
|
|
|
|
|
|
const VarDecl *VD = nullptr;
|
|
|
|
for (const auto &Result : DC->lookup(&II))
|
|
|
|
if ((VD = dyn_cast<VarDecl>(Result)))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!VD || !VD->hasAttr<DLLExportAttr>()) {
|
|
|
|
CGV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
|
|
|
|
CGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
|
|
|
|
} else {
|
|
|
|
CGV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
|
|
|
|
CGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-24 02:37:06 +08:00
|
|
|
// Decay array -> ptr
|
2016-09-11 09:25:15 +08:00
|
|
|
CFConstantStringClassRef =
|
|
|
|
llvm::ConstantExpr::getGetElementPtr(Ty, GV, Zeros);
|
2016-05-31 00:23:07 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 02:54:24 +08:00
|
|
|
QualType CFTy = getContext().getCFConstantStringType();
|
2008-08-24 02:37:06 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
|
2008-11-16 02:54:24 +08:00
|
|
|
|
2016-11-29 06:18:27 +08:00
|
|
|
ConstantInitBuilder Builder(*this);
|
2016-11-19 16:17:24 +08:00
|
|
|
auto Fields = Builder.beginStruct(STy);
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Class pointer.
|
2016-11-19 16:17:24 +08:00
|
|
|
Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Flags.
|
2016-11-19 16:17:24 +08:00
|
|
|
Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
|
2009-08-16 13:55:31 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// String pointer.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *C = nullptr;
|
2012-03-30 08:26:17 +08:00
|
|
|
if (isUTF16) {
|
2015-09-27 11:44:08 +08:00
|
|
|
auto Arr = llvm::makeArrayRef(
|
2014-11-19 11:06:06 +08:00
|
|
|
reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
|
|
|
|
Entry.first().size() / 2);
|
2012-03-30 08:26:17 +08:00
|
|
|
C = llvm::ConstantDataArray::get(VMContext, Arr);
|
|
|
|
} else {
|
2014-11-19 11:06:06 +08:00
|
|
|
C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
|
2012-03-30 08:26:17 +08:00
|
|
|
}
|
2009-04-03 08:57:44 +08:00
|
|
|
|
2012-01-10 16:46:39 +08:00
|
|
|
// Note: -fwritable-strings doesn't make the backing store strings of
|
|
|
|
// CFStrings writable. (See <rdar://problem/10657500>)
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV =
|
2014-01-21 10:57:56 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage, C, ".str");
|
2016-06-15 05:02:05 +08:00
|
|
|
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2013-05-07 00:23:57 +08:00
|
|
|
// Don't enforce the target's minimum global alignment, since the only use
|
|
|
|
// of the string is via this class initializer.
|
2016-05-31 00:23:07 +08:00
|
|
|
CharUnits Align = isUTF16
|
|
|
|
? getContext().getTypeAlignInChars(getContext().ShortTy)
|
|
|
|
: getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
|
|
|
|
|
|
|
// FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
|
|
|
|
// Without it LLVM can merge the string with a non unnamed_addr one during
|
|
|
|
// LTO. Doing that changes the section it ends in, which surprises ld64.
|
2016-09-14 23:17:46 +08:00
|
|
|
if (getTriple().isOSBinFormatMachO())
|
2016-05-31 00:23:07 +08:00
|
|
|
GV->setSection(isUTF16 ? "__TEXT,__ustring"
|
|
|
|
: "__TEXT,__cstring,cstring_literals");
|
2012-03-30 08:26:17 +08:00
|
|
|
|
|
|
|
// String.
|
2016-11-19 16:17:24 +08:00
|
|
|
llvm::Constant *Str =
|
2015-05-08 01:27:56 +08:00
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
|
2009-08-16 13:55:31 +08:00
|
|
|
|
2012-03-30 08:26:17 +08:00
|
|
|
if (isUTF16)
|
|
|
|
// Cast the UTF16 string to the correct type.
|
2016-11-19 16:17:24 +08:00
|
|
|
Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy);
|
|
|
|
Fields.add(Str);
|
2012-03-30 08:26:17 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// String length.
|
2016-11-19 16:17:24 +08:00
|
|
|
auto Ty = getTypes().ConvertType(getContext().LongTy);
|
|
|
|
Fields.addInt(cast<llvm::IntegerType>(Ty), StringLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
CharUnits Alignment = getPointerAlign();
|
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// The struct.
|
2016-11-19 16:17:24 +08:00
|
|
|
GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
|
|
|
|
/*isConstant=*/false,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage);
|
2016-09-14 23:17:46 +08:00
|
|
|
switch (getTriple().getObjectFormat()) {
|
2016-05-31 00:23:07 +08:00
|
|
|
case llvm::Triple::UnknownObjectFormat:
|
|
|
|
llvm_unreachable("unknown file format");
|
|
|
|
case llvm::Triple::COFF:
|
|
|
|
case llvm::Triple::ELF:
|
2017-01-18 05:46:38 +08:00
|
|
|
case llvm::Triple::Wasm:
|
2016-07-09 09:59:51 +08:00
|
|
|
GV->setSection("cfstring");
|
2016-05-31 00:23:07 +08:00
|
|
|
break;
|
|
|
|
case llvm::Triple::MachO:
|
|
|
|
GV->setSection("__DATA,__cfstring");
|
|
|
|
break;
|
|
|
|
}
|
2014-11-19 11:06:06 +08:00
|
|
|
Entry.second = GV;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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 ConstantAddress(GV, Alignment);
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2011-08-10 01:23:49 +08:00
|
|
|
QualType CodeGenModule::getObjCFastEnumerationStateType() {
|
|
|
|
if (ObjCFastEnumerationStateType.isNull()) {
|
2013-12-17 09:22:38 +08:00
|
|
|
RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
|
2011-08-10 01:23:49 +08:00
|
|
|
D->startDefinition();
|
|
|
|
|
|
|
|
QualType FieldTypes[] = {
|
|
|
|
Context.UnsignedLongTy,
|
|
|
|
Context.getPointerType(Context.getObjCIdType()),
|
|
|
|
Context.getPointerType(Context.UnsignedLongTy),
|
|
|
|
Context.getConstantArrayType(Context.UnsignedLongTy,
|
|
|
|
llvm::APInt(32, 5), ArrayType::Normal, 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
FieldDecl *Field = FieldDecl::Create(Context,
|
|
|
|
D,
|
|
|
|
SourceLocation(),
|
2014-05-21 13:09:00 +08:00
|
|
|
SourceLocation(), nullptr,
|
|
|
|
FieldTypes[i], /*TInfo=*/nullptr,
|
|
|
|
/*BitWidth=*/nullptr,
|
2011-08-10 01:23:49 +08:00
|
|
|
/*Mutable=*/false,
|
2012-06-10 11:12:00 +08:00
|
|
|
ICIS_NoInit);
|
2011-08-10 01:23:49 +08:00
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
ObjCFastEnumerationStateType = Context.getTagDeclType(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjCFastEnumerationStateType;
|
|
|
|
}
|
|
|
|
|
2011-11-01 10:23:42 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) {
|
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
|
|
|
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2012-02-07 06:47:00 +08:00
|
|
|
if (E->getCharByteWidth() == 1) {
|
|
|
|
SmallString<64> Str(E->getString());
|
|
|
|
|
|
|
|
// Resize the string to the right size, which is indicated by its type.
|
|
|
|
const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
|
|
|
|
Str.resize(CAT->getSize().getZExtValue());
|
|
|
|
return llvm::ConstantDataArray::getString(VMContext, Str, false);
|
|
|
|
}
|
2014-05-09 08:08:36 +08:00
|
|
|
|
|
|
|
auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
|
2012-02-05 10:30:40 +08:00
|
|
|
llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
2012-02-07 06:52:04 +08:00
|
|
|
|
|
|
|
// Wide strings have either 2-byte or 4-byte elements.
|
|
|
|
if (ElemTy->getPrimitiveSizeInBits() == 16) {
|
|
|
|
SmallVector<uint16_t, 32> Elements;
|
|
|
|
Elements.reserve(NumElements);
|
|
|
|
|
|
|
|
for(unsigned i = 0, e = E->getLength(); i != e; ++i)
|
|
|
|
Elements.push_back(E->getCodeUnit(i));
|
|
|
|
Elements.resize(NumElements);
|
|
|
|
return llvm::ConstantDataArray::get(VMContext, Elements);
|
2012-02-05 10:30:40 +08:00
|
|
|
}
|
|
|
|
|
2012-02-07 06:52:04 +08:00
|
|
|
assert(ElemTy->getPrimitiveSizeInBits() == 32);
|
|
|
|
SmallVector<uint32_t, 32> Elements;
|
|
|
|
Elements.reserve(NumElements);
|
|
|
|
|
|
|
|
for(unsigned i = 0, e = E->getLength(); i != e; ++i)
|
|
|
|
Elements.push_back(E->getCodeUnit(i));
|
|
|
|
Elements.resize(NumElements);
|
|
|
|
return llvm::ConstantDataArray::get(VMContext, Elements);
|
2011-11-01 10:23:42 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
static llvm::GlobalVariable *
|
|
|
|
GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
|
|
|
|
CodeGenModule &CGM, StringRef GlobalName,
|
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
|
|
|
CharUnits Alignment) {
|
2014-06-05 03:56:57 +08:00
|
|
|
// OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
|
|
|
|
unsigned AddrSpace = 0;
|
|
|
|
if (CGM.getLangOpts().OpenCL)
|
|
|
|
AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant);
|
|
|
|
|
2015-01-17 04:32:35 +08:00
|
|
|
llvm::Module &M = CGM.getModule();
|
2014-06-05 03:56:57 +08:00
|
|
|
// Create a global variable for this string
|
|
|
|
auto *GV = new llvm::GlobalVariable(
|
2015-01-17 04:32:35 +08:00
|
|
|
M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
|
|
|
|
nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
|
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
|
|
|
GV->setAlignment(Alignment.getQuantity());
|
2016-06-15 05:02:05 +08:00
|
|
|
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
2015-05-10 05:10:07 +08:00
|
|
|
if (GV->isWeakForLinker()) {
|
|
|
|
assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
|
|
|
|
GV->setComdat(M.getOrInsertComdat(GV->getName()));
|
|
|
|
}
|
2015-01-17 04:32:35 +08:00
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2008-08-14 07:20:05 +08:00
|
|
|
/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
|
|
|
|
/// constant array for the given string literal.
|
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
|
|
|
ConstantAddress
|
2014-10-09 16:45:04 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
|
|
|
|
StringRef Name) {
|
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
|
|
|
CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType());
|
2014-05-10 00:28:56 +08:00
|
|
|
|
2014-07-30 05:20:12 +08:00
|
|
|
llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
|
|
|
|
llvm::GlobalVariable **Entry = nullptr;
|
2014-03-25 05:43:36 +08:00
|
|
|
if (!LangOpts.WritableStrings) {
|
2014-07-30 05:20:12 +08:00
|
|
|
Entry = &ConstantStringMap[C];
|
|
|
|
if (auto GV = *Entry) {
|
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
|
|
|
if (Alignment.getQuantity() > GV->getAlignment())
|
|
|
|
GV->setAlignment(Alignment.getQuantity());
|
|
|
|
return ConstantAddress(GV, Alignment);
|
2014-03-25 05:43:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
SmallString<256> MangledNameBuffer;
|
|
|
|
StringRef GlobalVariableName;
|
|
|
|
llvm::GlobalValue::LinkageTypes LT;
|
2014-03-25 05:43:36 +08:00
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
// Mangle the string literal if the ABI allows for it. However, we cannot
|
|
|
|
// do this if we are compiling with ASan or -fwritable-strings because they
|
|
|
|
// rely on strings having normal linkage.
|
2014-11-08 06:29:38 +08:00
|
|
|
if (!LangOpts.WritableStrings &&
|
|
|
|
!LangOpts.Sanitize.has(SanitizerKind::Address) &&
|
2014-06-05 03:56:57 +08:00
|
|
|
getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {
|
|
|
|
llvm::raw_svector_ostream Out(MangledNameBuffer);
|
|
|
|
getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
|
2014-03-25 05:43:36 +08:00
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
LT = llvm::GlobalValue::LinkOnceODRLinkage;
|
|
|
|
GlobalVariableName = MangledNameBuffer;
|
|
|
|
} else {
|
|
|
|
LT = llvm::GlobalValue::PrivateLinkage;
|
2014-10-09 16:45:04 +08:00
|
|
|
GlobalVariableName = Name;
|
2009-11-16 13:55:46 +08:00
|
|
|
}
|
2011-11-01 10:23:42 +08:00
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
|
|
|
|
if (Entry)
|
2014-07-30 05:20:12 +08:00
|
|
|
*Entry = GV;
|
2014-07-03 00:54:41 +08:00
|
|
|
|
2014-10-18 06:37:33 +08:00
|
|
|
SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
|
|
|
|
QualType());
|
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 ConstantAddress(GV, Alignment);
|
2008-08-14 07:20:05 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
|
|
|
|
/// array for the given ObjCEncodeExpr node.
|
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
|
|
|
ConstantAddress
|
2009-02-25 06:18:39 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) {
|
|
|
|
std::string Str;
|
|
|
|
getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
2009-03-08 04:17:55 +08:00
|
|
|
|
|
|
|
return GetAddrOfConstantCString(Str);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 04:25:57 +08:00
|
|
|
/// GetAddrOfConstantCString - Returns a pointer to a character array containing
|
|
|
|
/// the literal and a terminating '\0' character.
|
2008-08-14 07:20:05 +08:00
|
|
|
/// The result has pointer to array type.
|
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
|
|
|
ConstantAddress CodeGenModule::GetAddrOfConstantCString(
|
|
|
|
const std::string &Str, const char *GlobalName) {
|
2014-06-05 04:25:57 +08:00
|
|
|
StringRef StrWithNull(Str.c_str(), Str.size() + 1);
|
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
|
|
|
CharUnits Alignment =
|
|
|
|
getContext().getAlignOfGlobalVarInChars(getContext().CharTy);
|
2013-05-07 00:23:57 +08:00
|
|
|
|
2014-07-30 05:20:12 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
|
|
|
|
|
2009-04-01 07:42:16 +08:00
|
|
|
// Don't share any string literals if strings aren't constant.
|
2014-07-30 05:20:12 +08:00
|
|
|
llvm::GlobalVariable **Entry = nullptr;
|
2014-06-05 03:56:57 +08:00
|
|
|
if (!LangOpts.WritableStrings) {
|
2014-07-30 05:20:12 +08:00
|
|
|
Entry = &ConstantStringMap[C];
|
|
|
|
if (auto GV = *Entry) {
|
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
|
|
|
if (Alignment.getQuantity() > GV->getAlignment())
|
|
|
|
GV->setAlignment(Alignment.getQuantity());
|
|
|
|
return ConstantAddress(GV, Alignment);
|
2011-08-04 09:03:22 +08:00
|
|
|
}
|
|
|
|
}
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2014-06-05 03:56:57 +08:00
|
|
|
// Get the default prefix if a name wasn't specified.
|
|
|
|
if (!GlobalName)
|
|
|
|
GlobalName = ".str";
|
2007-11-28 13:34:05 +08:00
|
|
|
// Create a global variable for this.
|
2014-06-05 03:56:57 +08:00
|
|
|
auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
|
|
|
|
GlobalName, Alignment);
|
|
|
|
if (Entry)
|
2014-07-30 05:20:12 +08:00
|
|
|
*Entry = GV;
|
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 ConstantAddress(GV, Alignment);
|
2007-11-28 13:34:05 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
|
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
|
|
|
ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
|
2013-06-14 11:07:01 +08:00
|
|
|
const MaterializeTemporaryExpr *E, const Expr *Init) {
|
2013-06-05 08:46:14 +08:00
|
|
|
assert((E->getStorageDuration() == SD_Static ||
|
|
|
|
E->getStorageDuration() == SD_Thread) && "not a global temporary");
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *VD = cast<VarDecl>(E->getExtendingDecl());
|
2013-06-05 08:46:14 +08:00
|
|
|
|
|
|
|
// If we're not materializing a subobject of the temporary, keep the
|
|
|
|
// cv-qualifiers from the type of the MaterializeTemporaryExpr.
|
2013-06-14 11:07:01 +08:00
|
|
|
QualType MaterializedType = Init->getType();
|
|
|
|
if (Init == E->GetTemporaryExpr())
|
|
|
|
MaterializedType = E->getType();
|
2013-06-05 08:46:14 +08:00
|
|
|
|
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
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
|
|
|
|
|
2015-08-14 07:50:15 +08:00
|
|
|
if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E])
|
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 ConstantAddress(Slot, Align);
|
2013-06-05 08:46:14 +08:00
|
|
|
|
|
|
|
// FIXME: If an externally-visible declaration extends multiple temporaries,
|
|
|
|
// we need to give each temporary the same name in every translation unit (and
|
|
|
|
// we also need to make the temporaries externally-visible).
|
|
|
|
SmallString<256> Name;
|
|
|
|
llvm::raw_svector_ostream Out(Name);
|
2014-05-02 01:50:17 +08:00
|
|
|
getCXXABI().getMangleContext().mangleReferenceTemporary(
|
|
|
|
VD, E->getManglingNumber(), Out);
|
2013-06-05 08:46:14 +08:00
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
APValue *Value = nullptr;
|
2013-06-05 08:46:14 +08:00
|
|
|
if (E->getStorageDuration() == SD_Static) {
|
2013-06-14 11:07:01 +08:00
|
|
|
// We might have a cached constant initializer for this temporary. Note
|
|
|
|
// that this might have a different value from the value computed by
|
|
|
|
// evaluating the initializer if the surrounding constant expression
|
|
|
|
// modifies the temporary.
|
2013-06-05 08:46:14 +08:00
|
|
|
Value = getContext().getMaterializedTemporaryValue(E, false);
|
|
|
|
if (Value && Value->isUninit())
|
2014-05-21 13:09:00 +08:00
|
|
|
Value = nullptr;
|
2013-06-05 08:46:14 +08:00
|
|
|
}
|
|
|
|
|
2013-06-14 11:07:01 +08:00
|
|
|
// Try evaluating it now, it might have a constant initializer.
|
|
|
|
Expr::EvalResult EvalResult;
|
|
|
|
if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
|
|
|
|
!EvalResult.hasSideEffects())
|
|
|
|
Value = &EvalResult.Val;
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *InitialValue = nullptr;
|
2013-06-14 11:07:01 +08:00
|
|
|
bool Constant = false;
|
|
|
|
llvm::Type *Type;
|
2013-06-05 08:46:14 +08:00
|
|
|
if (Value) {
|
|
|
|
// The temporary has a constant initializer, use it.
|
2014-05-21 13:09:00 +08:00
|
|
|
InitialValue = EmitConstantValue(*Value, MaterializedType, nullptr);
|
2013-06-14 11:07:01 +08:00
|
|
|
Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
|
|
|
|
Type = InitialValue->getType();
|
2013-06-05 08:46:14 +08:00
|
|
|
} else {
|
2013-06-14 11:07:01 +08:00
|
|
|
// No initializer, the initialization will be provided when we
|
2013-06-05 08:46:14 +08:00
|
|
|
// initialize the declaration which performed lifetime extension.
|
2013-06-14 11:07:01 +08:00
|
|
|
Type = getTypes().ConvertTypeForMem(MaterializedType);
|
2013-06-05 08:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a global variable for this lifetime-extended temporary.
|
2014-04-29 06:17:59 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
getLLVMLinkageVarDefinition(VD, Constant);
|
2015-02-20 03:25:17 +08:00
|
|
|
if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
|
|
|
|
const VarDecl *InitVD;
|
|
|
|
if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
|
|
|
|
isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
|
|
|
|
// Temporaries defined inside a class get linkonce_odr linkage because the
|
|
|
|
// class can be defined in multipe translation units.
|
|
|
|
Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
|
|
|
|
} else {
|
|
|
|
// There is no need for this temporary to have external linkage if the
|
|
|
|
// VarDecl has external linkage.
|
|
|
|
Linkage = llvm::GlobalVariable::InternalLinkage;
|
|
|
|
}
|
|
|
|
}
|
2014-04-29 06:17:59 +08:00
|
|
|
unsigned AddrSpace = GetGlobalVarAddressSpace(
|
|
|
|
VD, getContext().getTargetAddressSpace(MaterializedType));
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = new llvm::GlobalVariable(
|
2014-04-29 06:17:59 +08:00
|
|
|
getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
|
|
|
|
/*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
|
|
|
|
AddrSpace);
|
2014-04-29 14:18:53 +08:00
|
|
|
setGlobalVisibility(GV, VD);
|
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
|
|
|
GV->setAlignment(Align.getQuantity());
|
2015-05-10 05:10:07 +08:00
|
|
|
if (supportsCOMDAT() && GV->isWeakForLinker())
|
|
|
|
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
|
2013-06-05 08:46:14 +08:00
|
|
|
if (VD->getTLSKind())
|
|
|
|
setTLSMode(GV, *VD);
|
2015-08-14 07:50:15 +08:00
|
|
|
MaterializedGlobalTemporaryMap[E] = GV;
|
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 ConstantAddress(GV, Align);
|
2013-06-05 08:46:14 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
/// EmitObjCPropertyImplementations - Emit information for synthesized
|
|
|
|
/// properties for an implementation.
|
2009-09-09 23:08:12 +08:00
|
|
|
void CodeGenModule::EmitObjCPropertyImplementations(const
|
2008-08-26 16:29:31 +08:00
|
|
|
ObjCImplementationDecl *D) {
|
2014-03-14 23:02:45 +08:00
|
|
|
for (const auto *PID : D->property_impls()) {
|
2008-08-26 16:29:31 +08:00
|
|
|
// Dynamic is just for type-checking.
|
|
|
|
if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
|
|
|
|
ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
|
|
|
|
// Determine which methods need to be implemented, some may have
|
2012-10-11 00:42:25 +08:00
|
|
|
// been overridden. Note that ::isPropertyAccessor is not the method
|
2008-08-26 16:29:31 +08:00
|
|
|
// we want, that just indicates if the decl came from a
|
|
|
|
// property. What we want to know is if the method is defined in
|
|
|
|
// this implementation.
|
2009-06-30 10:36:12 +08:00
|
|
|
if (!D->getInstanceMethod(PD->getGetterName()))
|
2008-12-10 04:23:04 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCGetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 16:29:31 +08:00
|
|
|
if (!PD->isReadOnly() &&
|
2009-06-30 10:36:12 +08:00
|
|
|
!D->getInstanceMethod(PD->getSetterName()))
|
2008-12-10 04:23:04 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCSetter(
|
|
|
|
const_cast<ObjCImplementationDecl *>(D), PID);
|
2008-08-26 16:29:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:05:39 +08:00
|
|
|
static bool needsDestructMethod(ObjCImplementationDecl *impl) {
|
2011-07-22 10:08:32 +08:00
|
|
|
const ObjCInterfaceDecl *iface = impl->getClassInterface();
|
|
|
|
for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
|
2011-03-22 15:05:39 +08:00
|
|
|
ivar; ivar = ivar->getNextIvar())
|
|
|
|
if (ivar->getType().isDestructedType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-13 06:37:43 +08:00
|
|
|
static bool AllTrivialInitializers(CodeGenModule &CGM,
|
|
|
|
ObjCImplementationDecl *D) {
|
|
|
|
CodeGenFunction CGF(CGM);
|
|
|
|
for (ObjCImplementationDecl::init_iterator B = D->init_begin(),
|
|
|
|
E = D->init_end(); B != E; ++B) {
|
|
|
|
CXXCtorInitializer *CtorInitExp = *B;
|
|
|
|
Expr *Init = CtorInitExp->getInit();
|
|
|
|
if (!CGF.isTrivialInitializer(Init))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-29 05:28:56 +08:00
|
|
|
/// EmitObjCIvarInitializations - Emit information for ivar initialization
|
|
|
|
/// for an implementation.
|
|
|
|
void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
|
2011-03-22 15:05:39 +08:00
|
|
|
// We might need a .cxx_destruct even if we don't have any ivar initializers.
|
|
|
|
if (needsDestructMethod(D)) {
|
|
|
|
IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
|
|
|
|
Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
|
|
|
|
ObjCMethodDecl *DTORMethod =
|
|
|
|
ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(),
|
2014-05-21 13:09:00 +08:00
|
|
|
cxxSelector, getContext().VoidTy, nullptr, D,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isInstance=*/true, /*isVariadic=*/false,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isDefined=*/false, ObjCMethodDecl::Required);
|
2011-03-22 15:05:39 +08:00
|
|
|
D->addInstanceMethod(DTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
|
2012-10-17 12:53:31 +08:00
|
|
|
D->setHasDestructors(true);
|
2011-03-22 15:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the implementation doesn't have any ivar initializers, we don't need
|
|
|
|
// a .cxx_construct.
|
2014-11-13 06:37:43 +08:00
|
|
|
if (D->getNumIvarInitializers() == 0 ||
|
|
|
|
AllTrivialInitializers(*this, D))
|
2010-04-29 05:28:56 +08:00
|
|
|
return;
|
|
|
|
|
2011-03-22 15:05:39 +08:00
|
|
|
IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
|
|
|
|
Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
|
2010-04-29 05:28:56 +08:00
|
|
|
// The constructor returns 'self'.
|
|
|
|
ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(),
|
|
|
|
D->getLocation(),
|
2011-10-03 14:36:36 +08:00
|
|
|
D->getLocation(),
|
|
|
|
cxxSelector,
|
2014-05-21 13:09:00 +08:00
|
|
|
getContext().getObjCIdType(),
|
|
|
|
nullptr, D, /*isInstance=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isVariadic=*/false,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/true,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isImplicitlyDeclared=*/true,
|
|
|
|
/*isDefined=*/false,
|
2010-04-29 05:28:56 +08:00
|
|
|
ObjCMethodDecl::Required);
|
|
|
|
D->addInstanceMethod(CTORMethod);
|
|
|
|
CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
|
2012-10-17 12:53:31 +08:00
|
|
|
D->setHasNonZeroConstructors(true);
|
2010-04-29 05:28:56 +08:00
|
|
|
}
|
|
|
|
|
2009-04-02 13:55:18 +08:00
|
|
|
// EmitLinkageSpec - Emit all declarations in a linkage spec.
|
|
|
|
void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
|
2009-08-02 04:48:04 +08:00
|
|
|
if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
|
|
|
|
LSD->getLanguage() != LinkageSpecDecl::lang_cxx) {
|
2009-04-02 13:55:18 +08:00
|
|
|
ErrorUnsupported(LSD, "linkage spec");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-09 07:14:54 +08:00
|
|
|
EmitDeclContext(LSD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
|
|
|
|
for (auto *I : DC->decls()) {
|
|
|
|
// Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
|
|
|
|
// are themselves considered "top-level", so EmitTopLevelDecl on an
|
|
|
|
// ObjCImplDecl does not recursively visit them. We need to do that in
|
|
|
|
// case they're nested inside another construct (LinkageSpecDecl /
|
|
|
|
// ExportDecl) that does stop them from being considered "top-level".
|
2014-03-08 03:56:05 +08:00
|
|
|
if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
|
2014-03-14 03:03:34 +08:00
|
|
|
for (auto *M : OID->methods())
|
|
|
|
EmitTopLevelDecl(M);
|
2012-10-27 04:22:11 +08:00
|
|
|
}
|
2016-09-09 07:14:54 +08:00
|
|
|
|
2014-03-08 03:56:05 +08:00
|
|
|
EmitTopLevelDecl(I);
|
2012-10-27 04:22:11 +08:00
|
|
|
}
|
2009-04-02 13:55:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
/// EmitTopLevelDecl - Emit code for a single top level declaration.
|
|
|
|
void CodeGenModule::EmitTopLevelDecl(Decl *D) {
|
2009-06-30 01:30:29 +08:00
|
|
|
// Ignore dependent declarations.
|
|
|
|
if (D->getDeclContext() && D->getDeclContext()->isDependentContext())
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
switch (D->getKind()) {
|
2009-08-25 21:14:46 +08:00
|
|
|
case Decl::CXXConversion:
|
2009-04-05 04:47:02 +08:00
|
|
|
case Decl::CXXMethod:
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Function:
|
2009-06-30 01:30:29 +08:00
|
|
|
// Skip function templates
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-06-30 01:30:29 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<FunctionDecl>(D));
|
2014-08-05 02:41:51 +08:00
|
|
|
// Always provide some coverage mapping
|
|
|
|
// even for the functions that aren't emitted.
|
|
|
|
AddDeferredUnusedCoverageMapping(D);
|
2009-09-11 07:43:36 +08:00
|
|
|
break;
|
2013-08-06 09:03:05 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Var:
|
2016-07-23 07:36:59 +08:00
|
|
|
case Decl::Decomposition:
|
2013-08-06 09:03:05 +08:00
|
|
|
// Skip variable templates
|
|
|
|
if (cast<VarDecl>(D)->getDescribedVarTemplate())
|
|
|
|
return;
|
|
|
|
case Decl::VarTemplateSpecialization:
|
2009-09-11 07:43:36 +08:00
|
|
|
EmitGlobal(cast<VarDecl>(D));
|
2016-08-15 09:33:41 +08:00
|
|
|
if (auto *DD = dyn_cast<DecompositionDecl>(D))
|
|
|
|
for (auto *B : DD->bindings())
|
|
|
|
if (auto *HD = B->getHoldingVar())
|
|
|
|
EmitGlobal(HD);
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2011-04-12 09:01:22 +08:00
|
|
|
// Indirect fields from global anonymous structs and unions can be
|
|
|
|
// ignored; only the actual variable requires IR gen support.
|
|
|
|
case Decl::IndirectField:
|
|
|
|
break;
|
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
// C++ Decls
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::Namespace:
|
2016-09-09 07:14:54 +08:00
|
|
|
EmitDeclContext(cast<NamespaceDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2016-06-25 08:15:56 +08:00
|
|
|
case Decl::CXXRecord:
|
|
|
|
// Emit any static data members, they may be definitions.
|
|
|
|
for (auto *I : cast<CXXRecordDecl>(D)->decls())
|
|
|
|
if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
|
|
|
|
EmitTopLevelDecl(I);
|
|
|
|
break;
|
2009-06-20 08:51:54 +08:00
|
|
|
// No code generation needed.
|
2009-11-17 17:33:40 +08:00
|
|
|
case Decl::UsingShadow:
|
2009-06-30 04:59:39 +08:00
|
|
|
case Decl::ClassTemplate:
|
2013-08-06 09:03:05 +08:00
|
|
|
case Decl::VarTemplate:
|
|
|
|
case Decl::VarTemplatePartialSpecialization:
|
2009-06-30 04:59:39 +08:00
|
|
|
case Decl::FunctionTemplate:
|
2011-05-06 05:57:07 +08:00
|
|
|
case Decl::TypeAliasTemplate:
|
2011-06-05 13:04:23 +08:00
|
|
|
case Decl::Block:
|
2013-02-23 01:15:32 +08:00
|
|
|
case Decl::Empty:
|
2009-06-20 08:51:54 +08:00
|
|
|
break;
|
2014-02-16 05:03:07 +08:00
|
|
|
case Decl::Using: // using X; [C++]
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitUsingDecl(cast<UsingDecl>(*D));
|
|
|
|
return;
|
2013-05-21 06:50:41 +08:00
|
|
|
case Decl::NamespaceAlias:
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
|
|
|
|
return;
|
2013-04-26 13:41:06 +08:00
|
|
|
case Decl::UsingDirective: // using namespace X; [C++]
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
|
|
|
|
return;
|
2009-04-15 23:55:24 +08:00
|
|
|
case Decl::CXXConstructor:
|
2009-11-24 13:16:24 +08:00
|
|
|
// Skip function templates
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() ||
|
|
|
|
cast<FunctionDecl>(D)->isLateTemplateParsed())
|
2009-11-24 13:16:24 +08:00
|
|
|
return;
|
|
|
|
|
2013-08-05 01:30:04 +08:00
|
|
|
getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
|
2009-04-15 23:55:24 +08:00
|
|
|
break;
|
2009-04-17 09:58:57 +08:00
|
|
|
case Decl::CXXDestructor:
|
2011-04-23 06:18:13 +08:00
|
|
|
if (cast<FunctionDecl>(D)->isLateTemplateParsed())
|
|
|
|
return;
|
[ms-cxxabi] Emit linkonce complete dtors in TUs that need them
Based on Peter Collingbourne's destructor patches.
Prior to this change, clang was considering ?1 to be the complete
destructor and the base destructor, which was wrong. This lead to
crashes when clang tried to emit two LLVM functions with the same name.
In this ABI, TUs with non-inline dtors might not emit a complete
destructor. They are emitted as inline thunks in TUs that need them,
and they always delegate to the base dtors of the complete class and its
virtual bases. This change uses the DeferredDecls machinery to emit
complete dtors as needed.
Currently in clang try body destructors can catch exceptions thrown by
virtual base destructors. In the Microsoft C++ ABI, clang may not have
the destructor definition, in which case clang won't wrap the virtual
virtual base destructor calls in a try-catch. Diagnosing this in user
code is TODO.
Finally, for classes that don't use virtual inheritance, MSVC always
calls the base destructor (?1) directly. This is a useful code size
optimization that avoids emitting lots of extra thunks or aliases.
Implementing it also means our existing tests continue to pass, and is
consistent with MSVC's output.
We can do the same for Itanium by tweaking GetAddrOfCXXDestructor, but
it will require further testing.
Reviewers: rjmccall
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1066
llvm-svn: 186828
2013-07-22 21:51:44 +08:00
|
|
|
getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
|
2009-04-17 09:58:57 +08:00
|
|
|
break;
|
2009-06-12 05:22:55 +08:00
|
|
|
|
|
|
|
case Decl::StaticAssert:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
2009-04-15 23:55:24 +08:00
|
|
|
// Objective-C Decls
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-19 06:33:24 +08:00
|
|
|
// Forward declarations, no (immediate) code generation.
|
2009-03-22 02:06:45 +08:00
|
|
|
case Decl::ObjCInterface:
|
2012-07-20 06:22:55 +08:00
|
|
|
case Decl::ObjCCategory:
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-04-01 10:36:43 +08:00
|
|
|
|
2012-01-02 05:23:57 +08:00
|
|
|
case Decl::ObjCProtocol: {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Proto = cast<ObjCProtocolDecl>(D);
|
2012-01-02 05:23:57 +08:00
|
|
|
if (Proto->isThisDeclarationADefinition())
|
|
|
|
ObjCRuntime->GenerateProtocol(Proto);
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2012-01-02 05:23:57 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCCategoryImpl:
|
2008-08-26 16:29:31 +08:00
|
|
|
// Categories have properties but don't support synthesize so we
|
|
|
|
// can ignore them here.
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
case Decl::ObjCImplementation: {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *OMD = cast<ObjCImplementationDecl>(D);
|
2008-08-26 16:29:31 +08:00
|
|
|
EmitObjCPropertyImplementations(OMD);
|
2010-04-29 05:28:56 +08:00
|
|
|
EmitObjCIvarInitializations(OMD);
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime->GenerateClass(OMD);
|
2012-04-11 13:56:05 +08:00
|
|
|
// Emit global variable debug information.
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
2016-02-02 19:06:51 +08:00
|
|
|
if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
|
2012-12-04 02:28:12 +08:00
|
|
|
DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
|
|
|
|
OMD->getClassInterface()), OMD->getLocation());
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-08-16 07:26:23 +08:00
|
|
|
case Decl::ObjCMethod: {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *OMD = cast<ObjCMethodDecl>(D);
|
2008-08-16 07:26:23 +08:00
|
|
|
// If this is not a prototype, emit the body.
|
2009-06-30 10:35:26 +08:00
|
|
|
if (OMD->getBody())
|
2008-08-16 07:26:23 +08:00
|
|
|
CodeGenFunction(*this).GenerateObjCMethod(OMD);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
case Decl::ObjCCompatibleAlias:
|
2012-02-01 02:59:20 +08:00
|
|
|
ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
2016-03-03 01:28:48 +08:00
|
|
|
case Decl::PragmaComment: {
|
|
|
|
const auto *PCD = cast<PragmaCommentDecl>(D);
|
|
|
|
switch (PCD->getCommentKind()) {
|
|
|
|
case PCK_Unknown:
|
|
|
|
llvm_unreachable("unexpected pragma comment kind");
|
|
|
|
case PCK_Linker:
|
|
|
|
AppendLinkerOptions(PCD->getArg());
|
|
|
|
break;
|
|
|
|
case PCK_Lib:
|
|
|
|
AddDependentLib(PCD->getArg());
|
|
|
|
break;
|
|
|
|
case PCK_Compiler:
|
|
|
|
case PCK_ExeStr:
|
|
|
|
case PCK_User:
|
|
|
|
break; // We ignore all of these.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-03 03:28:54 +08:00
|
|
|
case Decl::PragmaDetectMismatch: {
|
|
|
|
const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
|
|
|
|
AddDetectMismatch(PDMD->getName(), PDMD->getValue());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-02 13:55:18 +08:00
|
|
|
case Decl::LinkageSpec:
|
|
|
|
EmitLinkageSpec(cast<LinkageSpecDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Decl::FileScopeAsm: {
|
2015-04-28 02:52:00 +08:00
|
|
|
// File-scope asm is ignored during device-side CUDA compilation.
|
|
|
|
if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
|
|
|
|
break;
|
2016-01-06 21:42:12 +08:00
|
|
|
// File-scope asm is ignored during device-side OpenMP compilation.
|
|
|
|
if (LangOpts.OpenMPIsDevice)
|
|
|
|
break;
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *AD = cast<FileScopeAsmDecl>(D);
|
2015-03-13 08:54:30 +08:00
|
|
|
getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
|
2008-08-16 07:26:23 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-15 01:21:00 +08:00
|
|
|
case Decl::Import: {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Import = cast<ImportDecl>(D);
|
2013-01-15 01:21:00 +08:00
|
|
|
|
2016-07-21 03:10:16 +08:00
|
|
|
// If we've already imported this module, we're done.
|
|
|
|
if (!ImportedModules.insert(Import->getImportedModule()))
|
2015-08-19 10:30:28 +08:00
|
|
|
break;
|
2013-01-15 01:21:00 +08:00
|
|
|
|
2016-07-21 03:10:16 +08:00
|
|
|
// Emit debug information for direct imports.
|
|
|
|
if (!Import->getImportedOwningModule()) {
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitImportDecl(*Import);
|
|
|
|
}
|
|
|
|
|
2016-10-15 02:55:44 +08:00
|
|
|
// Find all of the submodules and emit the module initializers.
|
|
|
|
llvm::SmallPtrSet<clang::Module *, 16> Visited;
|
|
|
|
SmallVector<clang::Module *, 16> Stack;
|
|
|
|
Visited.insert(Import->getImportedModule());
|
|
|
|
Stack.push_back(Import->getImportedModule());
|
|
|
|
|
|
|
|
while (!Stack.empty()) {
|
|
|
|
clang::Module *Mod = Stack.pop_back_val();
|
|
|
|
if (!EmittedModuleInitializers.insert(Mod).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto *D : Context.getModuleInitializers(Mod))
|
|
|
|
EmitTopLevelDecl(D);
|
|
|
|
|
|
|
|
// Visit the submodules of this module.
|
|
|
|
for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(),
|
|
|
|
SubEnd = Mod->submodule_end();
|
|
|
|
Sub != SubEnd; ++Sub) {
|
|
|
|
// Skip explicit children; they need to be explicitly imported to emit
|
|
|
|
// the initializers.
|
|
|
|
if ((*Sub)->IsExplicit)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Visited.insert(*Sub).second)
|
|
|
|
Stack.push_back(*Sub);
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 01:21:00 +08:00
|
|
|
break;
|
2014-03-04 07:48:23 +08:00
|
|
|
}
|
|
|
|
|
2016-09-09 07:14:54 +08:00
|
|
|
case Decl::Export:
|
|
|
|
EmitDeclContext(cast<ExportDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2014-11-11 12:05:39 +08:00
|
|
|
case Decl::OMPThreadPrivate:
|
|
|
|
EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2014-03-04 07:48:23 +08:00
|
|
|
case Decl::ClassTemplateSpecialization: {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
|
2014-03-04 07:48:23 +08:00
|
|
|
if (DebugInfo &&
|
2014-12-17 07:49:18 +08:00
|
|
|
Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition &&
|
|
|
|
Spec->hasDefinition())
|
2014-03-04 07:48:23 +08:00
|
|
|
DebugInfo->completeTemplateDefinition(*Spec);
|
2014-12-02 02:59:10 +08:00
|
|
|
break;
|
2014-03-04 07:48:23 +08:00
|
|
|
}
|
2013-01-15 01:21:00 +08:00
|
|
|
|
2016-03-03 13:21:39 +08:00
|
|
|
case Decl::OMPDeclareReduction:
|
|
|
|
EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
|
|
|
|
break;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
default:
|
2009-05-16 15:57:57 +08:00
|
|
|
// Make sure we handled everything we should, every other kind is a
|
|
|
|
// non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
|
|
|
|
// function. Need to recode Decl::Kind to do that easily.
|
2008-08-16 07:26:23 +08:00
|
|
|
assert(isa<TypeDecl>(D) && "Unsupported decl kind");
|
2014-12-02 02:59:10 +08:00
|
|
|
break;
|
2008-08-16 07:26:23 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-07 07:57:41 +08:00
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) {
|
|
|
|
// Do we need to generate coverage mapping?
|
|
|
|
if (!CodeGenOpts.CoverageMapping)
|
|
|
|
return;
|
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::ObjCMethod:
|
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor: {
|
2015-07-28 08:41:51 +08:00
|
|
|
if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
|
2014-08-05 02:41:51 +08:00
|
|
|
return;
|
|
|
|
auto I = DeferredEmptyCoverageMappingDecls.find(D);
|
|
|
|
if (I == DeferredEmptyCoverageMappingDecls.end())
|
|
|
|
DeferredEmptyCoverageMappingDecls[D] = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) {
|
|
|
|
// Do we need to generate coverage mapping?
|
|
|
|
if (!CodeGenOpts.CoverageMapping)
|
|
|
|
return;
|
|
|
|
if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (Fn->isTemplateInstantiation())
|
|
|
|
ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
|
|
|
|
}
|
|
|
|
auto I = DeferredEmptyCoverageMappingDecls.find(D);
|
|
|
|
if (I == DeferredEmptyCoverageMappingDecls.end())
|
|
|
|
DeferredEmptyCoverageMappingDecls[D] = false;
|
|
|
|
else
|
|
|
|
I->second = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
|
2014-08-09 07:41:24 +08:00
|
|
|
std::vector<const Decl *> DeferredDecls;
|
2015-04-15 09:21:42 +08:00
|
|
|
for (const auto &I : DeferredEmptyCoverageMappingDecls) {
|
2014-08-05 02:41:51 +08:00
|
|
|
if (!I.second)
|
|
|
|
continue;
|
2014-08-09 07:41:24 +08:00
|
|
|
DeferredDecls.push_back(I.first);
|
|
|
|
}
|
|
|
|
// Sort the declarations by their location to make sure that the tests get a
|
|
|
|
// predictable order for the coverage mapping for the unused declarations.
|
|
|
|
if (CodeGenOpts.DumpCoverageMapping)
|
|
|
|
std::sort(DeferredDecls.begin(), DeferredDecls.end(),
|
|
|
|
[] (const Decl *LHS, const Decl *RHS) {
|
|
|
|
return LHS->getLocStart() < RHS->getLocStart();
|
|
|
|
});
|
|
|
|
for (const auto *D : DeferredDecls) {
|
2014-08-05 02:41:51 +08:00
|
|
|
switch (D->getKind()) {
|
|
|
|
case Decl::CXXConversion:
|
|
|
|
case Decl::CXXMethod:
|
|
|
|
case Decl::Function:
|
|
|
|
case Decl::ObjCMethod: {
|
|
|
|
CodeGenPGO PGO(*this);
|
|
|
|
GlobalDecl GD(cast<FunctionDecl>(D));
|
|
|
|
PGO.emitEmptyCounterMapping(D, getMangledName(GD),
|
|
|
|
getFunctionLinkage(GD));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXConstructor: {
|
|
|
|
CodeGenPGO PGO(*this);
|
|
|
|
GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
|
|
|
|
PGO.emitEmptyCounterMapping(D, getMangledName(GD),
|
|
|
|
getFunctionLinkage(GD));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Decl::CXXDestructor: {
|
|
|
|
CodeGenPGO PGO(*this);
|
|
|
|
GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
|
|
|
|
PGO.emitEmptyCounterMapping(D, getMangledName(GD),
|
|
|
|
getFunctionLinkage(GD));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-07 07:57:41 +08:00
|
|
|
/// Turns the given pointer into a constant.
|
|
|
|
static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
|
|
|
|
const void *Ptr) {
|
|
|
|
uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
|
2010-07-07 07:57:41 +08:00
|
|
|
return llvm::ConstantInt::get(i64, PtrInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitGlobalDeclMetadata(CodeGenModule &CGM,
|
|
|
|
llvm::NamedMDNode *&GlobalMetadata,
|
|
|
|
GlobalDecl D,
|
|
|
|
llvm::GlobalValue *Addr) {
|
|
|
|
if (!GlobalMetadata)
|
|
|
|
GlobalMetadata =
|
|
|
|
CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
|
|
|
|
|
|
|
|
// TODO: should we report variant information for ctors/dtors?
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
|
|
|
|
llvm::ConstantAsMetadata::get(GetPointerConstant(
|
|
|
|
CGM.getLLVMContext(), D.getDecl()))};
|
2011-04-22 03:59:12 +08:00
|
|
|
GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
|
2010-07-07 07:57:41 +08:00
|
|
|
}
|
|
|
|
|
2013-04-06 13:00:46 +08:00
|
|
|
/// For each function which is declared within an extern "C" region and marked
|
|
|
|
/// as 'used', but has internal linkage, create an alias from the unmangled
|
|
|
|
/// name to the mangled name if possible. People expect to be able to refer
|
|
|
|
/// to such functions with an unmangled name from inline assembly within the
|
|
|
|
/// same translation unit.
|
|
|
|
void CodeGenModule::EmitStaticExternCAliases() {
|
2016-01-26 06:36:37 +08:00
|
|
|
// Don't do anything if we're generating CUDA device code -- the NVPTX
|
|
|
|
// assembly target doesn't support aliases.
|
|
|
|
if (Context.getTargetInfo().getTriple().isNVPTX())
|
|
|
|
return;
|
2015-06-11 20:33:25 +08:00
|
|
|
for (auto &I : StaticExternCValues) {
|
|
|
|
IdentifierInfo *Name = I.first;
|
|
|
|
llvm::GlobalValue *Val = I.second;
|
2013-04-06 15:07:44 +08:00
|
|
|
if (Val && !getModule().getNamedValue(Name->getName()))
|
2014-06-03 10:42:01 +08:00
|
|
|
addUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val));
|
2013-04-06 15:07:44 +08:00
|
|
|
}
|
2013-04-06 13:00:46 +08:00
|
|
|
}
|
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
|
|
|
|
GlobalDecl &Result) const {
|
|
|
|
auto Res = Manglings.find(MangledName);
|
|
|
|
if (Res == Manglings.end())
|
|
|
|
return false;
|
|
|
|
Result = Res->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-07 07:57:41 +08:00
|
|
|
/// Emits metadata nodes associating all the global values in the
|
|
|
|
/// current module with the Decls they came from. This is useful for
|
|
|
|
/// projects using IR gen as a subroutine.
|
|
|
|
///
|
|
|
|
/// Since there's currently no way to associate an MDNode directly
|
|
|
|
/// with an llvm::GlobalValue, we create a global named metadata
|
|
|
|
/// with the name 'clang.global.decl.ptrs'.
|
|
|
|
void CodeGenModule::EmitDeclMetadata() {
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::NamedMDNode *GlobalMetadata = nullptr;
|
2010-07-07 07:57:41 +08:00
|
|
|
|
2014-07-03 17:30:29 +08:00
|
|
|
for (auto &I : MangledDeclNames) {
|
|
|
|
llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
|
2015-11-06 07:18:44 +08:00
|
|
|
// Some mangled names don't necessarily have an associated GlobalValue
|
|
|
|
// in this module, e.g. if we mangled it for DebugInfo.
|
|
|
|
if (Addr)
|
|
|
|
EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
|
2010-07-07 07:57:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits metadata nodes for all the local variables in the current
|
|
|
|
/// function.
|
|
|
|
void CodeGenFunction::EmitDeclMetadata() {
|
|
|
|
if (LocalDeclMap.empty()) return;
|
|
|
|
|
|
|
|
llvm::LLVMContext &Context = getLLVMContext();
|
|
|
|
|
|
|
|
// Find the unique metadata ID for this name.
|
|
|
|
unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::NamedMDNode *GlobalMetadata = nullptr;
|
2010-07-07 07:57:41 +08:00
|
|
|
|
2014-07-03 17:30:29 +08:00
|
|
|
for (auto &I : LocalDeclMap) {
|
|
|
|
const Decl *D = I.first;
|
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
|
|
|
llvm::Value *Addr = I.second.getPointer();
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
|
2010-07-07 07:57:41 +08:00
|
|
|
llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D);
|
2014-12-10 02:39:32 +08:00
|
|
|
Alloca->setMetadata(
|
|
|
|
DeclPtrKind, llvm::MDNode::get(
|
|
|
|
Context, llvm::ValueAsMetadata::getConstant(DAddr)));
|
2014-05-09 08:08:36 +08:00
|
|
|
} else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
|
2010-07-07 07:57:41 +08:00
|
|
|
GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
|
|
|
|
EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-16 08:00:15 +08:00
|
|
|
|
2013-10-17 03:28:50 +08:00
|
|
|
void CodeGenModule::EmitVersionIdentMetadata() {
|
|
|
|
llvm::NamedMDNode *IdentMetadata =
|
|
|
|
TheModule.getOrInsertNamedMetadata("llvm.ident");
|
|
|
|
std::string Version = getClangFullVersion();
|
|
|
|
llvm::LLVMContext &Ctx = TheModule.getContext();
|
|
|
|
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
|
2013-10-17 03:28:50 +08:00
|
|
|
IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
|
|
|
|
}
|
|
|
|
|
2014-07-03 17:30:33 +08:00
|
|
|
void CodeGenModule::EmitTargetMetadata() {
|
2014-08-02 04:39:36 +08:00
|
|
|
// Warning, new MangledDeclNames may be appended within this loop.
|
|
|
|
// We rely on MapVector insertions adding new elements to the end
|
|
|
|
// of the container.
|
|
|
|
// FIXME: Move this loop into the one target that needs it, and only
|
|
|
|
// loop over those declarations for which we couldn't emit the target
|
|
|
|
// metadata when we emitted the declaration.
|
|
|
|
for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
|
2014-08-02 06:17:28 +08:00
|
|
|
auto Val = *(MangledDeclNames.begin() + I);
|
2014-08-02 04:39:36 +08:00
|
|
|
const Decl *D = Val.first.getDecl()->getMostRecentDecl();
|
|
|
|
llvm::GlobalValue *GV = GetGlobalValue(Val.second);
|
2014-07-03 17:30:33 +08:00
|
|
|
getTargetCodeGenInfo().emitTargetMD(D, GV, *this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-05 08:08:20 +08:00
|
|
|
void CodeGenModule::EmitCoverageFile() {
|
2016-09-01 07:04:32 +08:00
|
|
|
if (getCodeGenOpts().CoverageDataFile.empty() &&
|
|
|
|
getCodeGenOpts().CoverageNotesFile.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (!CUNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
|
|
|
|
llvm::LLVMContext &Ctx = TheModule.getContext();
|
|
|
|
auto *CoverageDataFile =
|
|
|
|
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
|
|
|
|
auto *CoverageNotesFile =
|
|
|
|
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
|
|
|
|
for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
|
|
|
|
llvm::MDNode *CU = CUNode->getOperand(i);
|
|
|
|
llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
|
|
|
|
GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
|
2011-05-05 04:46:58 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 18:13:44 +08:00
|
|
|
|
2014-09-09 00:26:36 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) {
|
2012-10-11 18:13:44 +08:00
|
|
|
// Sema has checked that all uuid strings are of the form
|
|
|
|
// "12345678-1234-1234-1234-1234567890ab".
|
|
|
|
assert(Uuid.size() == 36);
|
2013-08-16 03:59:14 +08:00
|
|
|
for (unsigned i = 0; i < 36; ++i) {
|
|
|
|
if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-');
|
|
|
|
else assert(isHexDigit(Uuid[i]));
|
2012-10-11 18:13:44 +08:00
|
|
|
}
|
2013-08-16 03:59:14 +08:00
|
|
|
|
2014-09-09 00:11:15 +08:00
|
|
|
// The starts of all bytes of Field3 in Uuid. Field 3 is "1234-1234567890ab".
|
2013-08-16 03:59:14 +08:00
|
|
|
const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 };
|
|
|
|
|
|
|
|
llvm::Constant *Field3[8];
|
|
|
|
for (unsigned Idx = 0; Idx < 8; ++Idx)
|
|
|
|
Field3[Idx] = llvm::ConstantInt::get(
|
|
|
|
Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16);
|
|
|
|
|
|
|
|
llvm::Constant *Fields[4] = {
|
|
|
|
llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16),
|
|
|
|
llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16),
|
|
|
|
llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16),
|
|
|
|
llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3)
|
|
|
|
};
|
|
|
|
|
|
|
|
return llvm::ConstantStruct::getAnon(Fields);
|
2012-10-11 18:13:44 +08:00
|
|
|
}
|
2014-07-07 14:20:47 +08:00
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
|
|
|
|
bool ForEH) {
|
|
|
|
// Return a bogus pointer if RTTI is disabled, unless it's for EH.
|
|
|
|
// FIXME: should we even be calling this method if RTTI is disabled
|
|
|
|
// and it's not for EH?
|
|
|
|
if (!ForEH && !getLangOpts().RTTI)
|
|
|
|
return llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
|
|
|
|
if (ForEH && Ty->isObjCObjectPointerType() &&
|
|
|
|
LangOpts.ObjCRuntime.isGNUFamily())
|
|
|
|
return ObjCRuntime->GetEHType(Ty);
|
|
|
|
|
2015-03-18 04:35:00 +08:00
|
|
|
return getCXXABI().getAddrOfRTTIDescriptor(Ty);
|
2014-07-07 14:20:47 +08:00
|
|
|
}
|
|
|
|
|
2014-11-11 12:05:39 +08:00
|
|
|
void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) {
|
|
|
|
for (auto RefExpr : D->varlists()) {
|
|
|
|
auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
|
|
|
|
bool PerformInit =
|
|
|
|
VD->getAnyInitializer() &&
|
|
|
|
!VD->getAnyInitializer()->isConstantInitializer(getContext(),
|
|
|
|
/*ForRef=*/false);
|
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
|
|
|
|
|
|
|
Address Addr(GetAddrOfGlobalVar(VD), getContext().getDeclAlign(VD));
|
2015-02-25 16:32:46 +08:00
|
|
|
if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
|
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
|
|
|
VD, Addr, RefExpr->getLocStart(), PerformInit))
|
2014-11-11 12:05:39 +08:00
|
|
|
CXXGlobalInits.push_back(InitFunction);
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 03:08:05 +08:00
|
|
|
|
2015-09-09 07:01:30 +08:00
|
|
|
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
|
|
|
|
llvm::Metadata *&InternalId = MetadataIdMap[T.getCanonicalType()];
|
|
|
|
if (InternalId)
|
|
|
|
return InternalId;
|
|
|
|
|
|
|
|
if (isExternallyVisible(T->getLinkage())) {
|
|
|
|
std::string OutName;
|
|
|
|
llvm::raw_string_ostream Out(OutName);
|
|
|
|
getCXXABI().getMangleContext().mangleTypeName(T, Out);
|
|
|
|
|
|
|
|
InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
|
|
|
|
} else {
|
|
|
|
InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
|
|
|
|
llvm::ArrayRef<llvm::Metadata *>());
|
|
|
|
}
|
|
|
|
|
|
|
|
return InternalId;
|
|
|
|
}
|
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
/// Returns whether this module needs the "all-vtables" type identifier.
|
|
|
|
bool CodeGenModule::NeedAllVtablesTypeId() const {
|
2016-02-04 06:18:55 +08:00
|
|
|
// Returns true if at least one of vtable-based CFI checkers is enabled and
|
|
|
|
// is not in the trapping mode.
|
|
|
|
return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) &&
|
|
|
|
!CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) ||
|
|
|
|
(LangOpts.Sanitize.has(SanitizerKind::CFINVCall) &&
|
|
|
|
!CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) ||
|
|
|
|
(LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) &&
|
|
|
|
!CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) ||
|
|
|
|
(LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) &&
|
|
|
|
!CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast)));
|
|
|
|
}
|
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable,
|
|
|
|
CharUnits Offset,
|
|
|
|
const CXXRecordDecl *RD) {
|
2015-12-16 07:00:20 +08:00
|
|
|
llvm::Metadata *MD =
|
|
|
|
CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
|
2016-06-25 05:21:46 +08:00
|
|
|
VTable->addTypeMetadata(Offset.getQuantity(), MD);
|
2015-12-16 07:00:20 +08:00
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
if (CodeGenOpts.SanitizeCfiCrossDso)
|
|
|
|
if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
|
|
|
|
VTable->addTypeMetadata(Offset.getQuantity(),
|
|
|
|
llvm::ConstantAsMetadata::get(CrossDsoTypeId));
|
2016-02-04 06:18:55 +08:00
|
|
|
|
2016-06-25 05:21:46 +08:00
|
|
|
if (NeedAllVtablesTypeId()) {
|
2016-02-04 06:18:55 +08:00
|
|
|
llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables");
|
2016-06-25 05:21:46 +08:00
|
|
|
VTable->addTypeMetadata(Offset.getQuantity(), MD);
|
2016-02-04 06:18:55 +08:00
|
|
|
}
|
2015-06-18 03:08:05 +08:00
|
|
|
}
|
2015-11-12 07:05:08 +08:00
|
|
|
|
|
|
|
// Fills in the supplied string map with the set of target features for the
|
|
|
|
// passed in function.
|
|
|
|
void CodeGenModule::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
|
|
|
|
const FunctionDecl *FD) {
|
|
|
|
StringRef TargetCPU = Target.getTargetOpts().CPU;
|
|
|
|
if (const auto *TD = FD->getAttr<TargetAttr>()) {
|
|
|
|
// If we have a TargetAttr build up the feature map based on that.
|
|
|
|
TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
|
|
|
|
|
|
|
|
// Make a copy of the features as passed on the command line into the
|
|
|
|
// beginning of the additional features from the function to override.
|
|
|
|
ParsedAttr.first.insert(ParsedAttr.first.begin(),
|
|
|
|
Target.getTargetOpts().FeaturesAsWritten.begin(),
|
|
|
|
Target.getTargetOpts().FeaturesAsWritten.end());
|
|
|
|
|
|
|
|
if (ParsedAttr.second != "")
|
|
|
|
TargetCPU = ParsedAttr.second;
|
|
|
|
|
|
|
|
// Now populate the feature map, first with the TargetCPU which is either
|
|
|
|
// the default or a new one from the target attribute string. Then we'll use
|
|
|
|
// the passed in features (FeaturesAsWritten) along with the new ones from
|
|
|
|
// the attribute.
|
|
|
|
Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU, ParsedAttr.first);
|
|
|
|
} else {
|
|
|
|
Target.initFeatureMap(FeatureMap, getDiags(), TargetCPU,
|
|
|
|
Target.getTargetOpts().Features);
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 08:31:22 +08:00
|
|
|
|
|
|
|
llvm::SanitizerStatReport &CodeGenModule::getSanStats() {
|
|
|
|
if (!SanStats)
|
|
|
|
SanStats = llvm::make_unique<llvm::SanitizerStatReport>(&getModule());
|
|
|
|
|
|
|
|
return *SanStats;
|
|
|
|
}
|
2016-07-29 03:26:30 +08:00
|
|
|
llvm::Value *
|
|
|
|
CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E,
|
|
|
|
CodeGenFunction &CGF) {
|
|
|
|
llvm::Constant *C = EmitConstantExpr(E, E->getType(), &CGF);
|
|
|
|
auto SamplerT = getOpenCLRuntime().getSamplerType();
|
|
|
|
auto FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false);
|
|
|
|
return CGF.Builder.CreateCall(CreateRuntimeFunction(FTy,
|
|
|
|
"__translate_sampler_initializer"),
|
|
|
|
{C});
|
|
|
|
}
|