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"
|
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"
|
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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-10-23 03:26:13 +08:00
|
|
|
#include "clang/Sema/SemaDiagnostic.h"
|
2012-02-26 04:51:20 +08:00
|
|
|
#include "llvm/ADT/APSInt.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"
|
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-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)),
|
2015-06-30 10:26:03 +08:00
|
|
|
VMContext(M.getContext()), TBAA(nullptr), TheTargetCodeGenInfo(nullptr),
|
|
|
|
Types(*this), VTables(*this), ObjCRuntime(nullptr),
|
|
|
|
OpenCLRuntime(nullptr), OpenMPRuntime(nullptr), CUDARuntime(nullptr),
|
|
|
|
DebugInfo(nullptr), ARCData(nullptr),
|
2014-05-21 13:09:00 +08:00
|
|
|
NoObjCARCExceptionsMetadata(nullptr), RRData(nullptr), PGOReader(nullptr),
|
|
|
|
CFConstantStringClassRef(nullptr), ConstantStringClassRef(nullptr),
|
|
|
|
NSConstantStringType(nullptr), NSConcreteGlobalBlock(nullptr),
|
|
|
|
NSConcreteStackBlock(nullptr), BlockObjectAssign(nullptr),
|
|
|
|
BlockObjectDispose(nullptr), BlockDescriptorType(nullptr),
|
|
|
|
GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr),
|
2014-10-16 06:17:27 +08:00
|
|
|
LifetimeEndFn(nullptr), 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 =
|
|
|
|
C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
|
|
|
|
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
|
|
|
|
IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits);
|
|
|
|
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))
|
|
|
|
TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts, getLangOpts(),
|
2013-12-29 05:58:40 +08:00
|
|
|
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.
|
2012-10-24 04:05:01 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() != CodeGenOptions::NoDebugInfo ||
|
2012-04-27 15:24:20 +08:00
|
|
|
CodeGenOpts.EmitGcovArcs ||
|
2011-04-22 07:44:07 +08:00
|
|
|
CodeGenOpts.EmitGcovNotes)
|
|
|
|
DebugInfo = new CGDebugInfo(*this);
|
2011-02-08 16:22:06 +08:00
|
|
|
|
|
|
|
Block.GlobalUniqueCount = 0;
|
2011-02-15 17:22:45 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (C.getLangOpts().ObjCAutoRefCount)
|
2011-06-16 07:02:42 +08:00
|
|
|
ARCData = new ARCEntrypoints();
|
|
|
|
RRData = new RREntrypoints();
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-04-19 05:52:00 +08:00
|
|
|
if (!CodeGenOpts.InstrProfileInput.empty()) {
|
2015-02-17 05:29:05 +08:00
|
|
|
auto ReaderOrErr =
|
|
|
|
llvm::IndexedInstrProfReader::create(CodeGenOpts.InstrProfileInput);
|
|
|
|
if (std::error_code EC = ReaderOrErr.getError()) {
|
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");
|
|
|
|
getDiags().Report(DiagID) << CodeGenOpts.InstrProfileInput
|
|
|
|
<< EC.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
|
|
|
}
|
|
|
|
|
|
|
|
CodeGenModule::~CodeGenModule() {
|
2011-07-28 04:29:46 +08:00
|
|
|
delete ObjCRuntime;
|
2011-09-20 05:14:35 +08:00
|
|
|
delete OpenCLRuntime;
|
2014-05-06 18:08:46 +08:00
|
|
|
delete OpenMPRuntime;
|
2011-10-07 02:29:37 +08:00
|
|
|
delete CUDARuntime;
|
2011-10-08 13:28:26 +08:00
|
|
|
delete TheTargetCodeGenInfo;
|
2010-10-16 02:04:46 +08:00
|
|
|
delete TBAA;
|
2008-08-06 02:50:11 +08:00
|
|
|
delete DebugInfo;
|
2011-06-16 07:02:42 +08:00
|
|
|
delete ARCData;
|
|
|
|
delete RRData;
|
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:
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime = CreateGNUObjCRuntime(*this);
|
2012-06-20 14:18:46 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case ObjCRuntime::FragileMacOSX:
|
|
|
|
case ObjCRuntime::MacOSX:
|
|
|
|
case ObjCRuntime::iOS:
|
2011-07-28 04:29:46 +08:00
|
|
|
ObjCRuntime = 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() {
|
|
|
|
OpenCLRuntime = new CGOpenCLRuntime(*this);
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:08:46 +08:00
|
|
|
void CodeGenModule::createOpenMPRuntime() {
|
|
|
|
OpenMPRuntime = new CGOpenMPRuntime(*this);
|
|
|
|
}
|
|
|
|
|
2011-10-07 02:29:37 +08:00
|
|
|
void CodeGenModule::createCUDARuntime() {
|
|
|
|
CUDARuntime = CreateNVCUDARuntime(*this);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
OldF->getParent()->getFunctionList().insertAfter(OldF, NewF);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
static const llvm::GlobalObject *getAliasedGlobal(const llvm::GlobalAlias &GA) {
|
|
|
|
llvm::SmallPtrSet<const llvm::GlobalAlias*, 4> Visited;
|
|
|
|
const llvm::Constant *C = &GA;
|
|
|
|
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.
|
|
|
|
auto *GA2 = dyn_cast<llvm::GlobalAlias>(C);
|
|
|
|
if (!GA2)
|
|
|
|
return nullptr;
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!Visited.insert(GA2).second)
|
2014-06-03 10:42:01 +08:00
|
|
|
return nullptr;
|
|
|
|
C = GA2->getAliasee();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2013-10-23 03:26:13 +08:00
|
|
|
const AliasAttr *AA = D->getAttr<AliasAttr>();
|
|
|
|
StringRef MangledName = getMangledName(GD);
|
|
|
|
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Alias = cast<llvm::GlobalAlias>(Entry);
|
2014-06-03 10:42:01 +08:00
|
|
|
const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
|
|
|
|
if (!GV) {
|
|
|
|
Error = true;
|
|
|
|
Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
|
|
|
|
} else if (GV->isDeclaration()) {
|
2014-03-13 23:47:50 +08:00
|
|
|
Error = true;
|
2014-05-06 03:33:09 +08:00
|
|
|
Diags.Report(AA->getLocation(), diag::err_alias_to_undefined);
|
2013-10-23 03:26:13 +08:00
|
|
|
}
|
2014-03-27 23:27:20 +08:00
|
|
|
|
2014-06-03 10:42:01 +08:00
|
|
|
llvm::Constant *Aliasee = Alias->getAliasee();
|
|
|
|
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)
|
|
|
|
<< AliasSection;
|
|
|
|
}
|
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.
|
|
|
|
if (auto GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
|
|
|
|
if (GA->mayBeOverridden()) {
|
|
|
|
Diags.Report(AA->getLocation(), diag::warn_alias_to_weak_alias)
|
|
|
|
<< GV->getName() << GA->getName();
|
|
|
|
Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
|
|
|
|
GA->getAliasee(), Alias->getType());
|
|
|
|
Alias->setAliasee(Aliasee);
|
|
|
|
}
|
|
|
|
}
|
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);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Alias = cast<llvm::GlobalAlias>(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);
|
|
|
|
}
|
2014-06-26 09:45:07 +08:00
|
|
|
if (PGOReader && 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();
|
2014-03-07 06:15:10 +08:00
|
|
|
emitLLVMUsed();
|
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);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-18 14:17:20 +08:00
|
|
|
if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
|
|
|
|
llvm::PICLevel::Level PL = llvm::PICLevel::Default;
|
|
|
|
switch (PLevel) {
|
|
|
|
case 0: break;
|
|
|
|
case 1: PL = llvm::PICLevel::Small; break;
|
|
|
|
case 2: PL = llvm::PICLevel::Large; break;
|
|
|
|
default: llvm_unreachable("Invalid PIC Level");
|
|
|
|
}
|
|
|
|
|
|
|
|
getModule().setPICLevel(PL);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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::getTBAAStructTypeInfo(QualType QTy) {
|
|
|
|
if (!TBAA)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-04-05 05:53:22 +08:00
|
|
|
return TBAA->getTBAAStructTypeInfo(QTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2010-10-15 07:06:10 +08:00
|
|
|
void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst,
|
2013-04-12 07:02:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2014-06-06 06:10:59 +08:00
|
|
|
StringRef &FoundStr = MangledDeclNames[GD.getCanonicalDecl()];
|
|
|
|
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.");
|
|
|
|
Str = II->getName();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
|
|
|
|
// 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.
|
2015-06-11 20:33:25 +08:00
|
|
|
SmallVector<llvm::Constant *, 8> Ctors;
|
|
|
|
for (const auto &I : Fns) {
|
2012-02-07 06:16:34 +08:00
|
|
|
llvm::Constant *S[] = {
|
2015-06-11 20:33:25 +08:00
|
|
|
llvm::ConstantInt::get(Int32Ty, I.Priority, false),
|
|
|
|
llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy),
|
|
|
|
(I.AssociatedData
|
|
|
|
? llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)
|
|
|
|
: llvm::Constant::getNullValue(VoidPtrTy))};
|
2009-07-28 06:29:56 +08:00
|
|
|
Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S));
|
2008-08-01 08:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ctors.empty()) {
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size());
|
2009-07-09 03:05:04 +08:00
|
|
|
new llvm::GlobalVariable(TheModule, AT, false,
|
2008-03-19 13:24:56 +08:00
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
2009-07-29 02:33:04 +08:00
|
|
|
llvm::ConstantArray::get(AT, Ctors),
|
2009-07-09 03:05:04 +08:00
|
|
|
GlobalName);
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
2013-02-22 08:13:35 +08:00
|
|
|
ConstructAttributeList(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
|
|
|
|
2011-08-23 07:55:33 +08:00
|
|
|
if (D->hasAttr<NakedAttr>()) {
|
|
|
|
// 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>() &&
|
2013-05-11 20:45:37 +08:00
|
|
|
!F->getAttributes().hasAttribute(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::Attribute::NoInline)) {
|
|
|
|
// (noinline wins over always_inline, and we can't specify both in IR)
|
|
|
|
B.addAttribute(llvm::Attribute::AlwaysInline);
|
2011-08-23 07:55:33 +08:00
|
|
|
}
|
2010-09-30 02:20:25 +08:00
|
|
|
|
2013-05-25 04:18:15 +08:00
|
|
|
if (D->hasAttr<ColdAttr>()) {
|
2014-12-12 04:14:04 +08:00
|
|
|
if (!D->hasAttr<OptimizeNoneAttr>())
|
|
|
|
B.addAttribute(llvm::Attribute::OptimizeForSize);
|
2013-05-25 04:18:15 +08:00
|
|
|
B.addAttribute(llvm::Attribute::Cold);
|
|
|
|
}
|
2012-05-13 05:10:52 +08:00
|
|
|
|
2012-11-02 07:55:47 +08:00
|
|
|
if (D->hasAttr<MinSizeAttr>())
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::MinSize);
|
2012-09-29 06:46:07 +08:00
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.getStackProtector() == LangOptions::SSPOn)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::StackProtect);
|
2014-02-11 09:35:14 +08:00
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
|
|
|
|
B.addAttribute(llvm::Attribute::StackProtectStrong);
|
2012-03-11 15:00:24 +08:00
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
|
2013-05-11 20:45:37 +08:00
|
|
|
B.addAttribute(llvm::Attribute::StackProtectReq);
|
2013-01-18 19:30:38 +08:00
|
|
|
|
2013-05-11 20:45:37 +08:00
|
|
|
F->addAttributes(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::AttributeSet::get(
|
|
|
|
F->getContext(), llvm::AttributeSet::FunctionIndex, B));
|
|
|
|
|
2014-11-25 04:51:42 +08:00
|
|
|
if (D->hasAttr<OptimizeNoneAttr>()) {
|
|
|
|
// OptimizeNone implies noinline; we should not be inlining such functions.
|
|
|
|
F->addFnAttr(llvm::Attribute::OptimizeNone);
|
|
|
|
F->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
|
|
|
|
// OptimizeNone wins over OptimizeForSize, MinSize, AlwaysInline.
|
2014-12-12 04:14:04 +08:00
|
|
|
assert(!F->hasFnAttribute(llvm::Attribute::OptimizeForSize) &&
|
|
|
|
"OptimizeNone and OptimizeForSize on same function!");
|
2014-12-16 02:57:28 +08:00
|
|
|
assert(!F->hasFnAttribute(llvm::Attribute::MinSize) &&
|
|
|
|
"OptimizeNone and MinSize on same function!");
|
|
|
|
assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
|
|
|
|
"OptimizeNone and AlwaysInline on same function!");
|
2014-11-25 04:51:42 +08:00
|
|
|
|
|
|
|
// Attribute 'inlinehint' has no effect on 'optnone' functions.
|
|
|
|
// Explicitly remove it from the set of function attributes.
|
|
|
|
F->removeFnAttr(llvm::Attribute::InlineHint);
|
|
|
|
}
|
|
|
|
|
2013-05-11 20:45:37 +08:00
|
|
|
if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
|
|
|
|
F->setUnnamedAddr(true);
|
2014-05-09 08:08:36 +08:00
|
|
|
else if (const auto *MD = dyn_cast<CXXMethodDecl>(D))
|
2013-05-11 20:45:37 +08:00
|
|
|
if (MD->isVirtual())
|
|
|
|
F->setUnnamedAddr(true);
|
|
|
|
|
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);
|
|
|
|
}
|
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) {
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *ND = dyn_cast<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
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (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
|
|
|
|
2009-06-30 10:34:44 +08:00
|
|
|
if (const SectionAttr *SA = D->getAttr<SectionAttr>())
|
2014-05-14 02:45:53 +08:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) &&
|
2014-04-01 09:38:16 +08:00
|
|
|
!(getTarget().getTriple().isiOS() &&
|
|
|
|
getTarget().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
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (FD->isReplaceableGlobalAllocationFunction())
|
|
|
|
F->addAttribute(llvm::AttributeSet::FunctionIndex,
|
|
|
|
llvm::Attribute::NoBuiltin);
|
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.
|
2015-06-11 20:33:25 +08:00
|
|
|
for (Module *M : ImportedModules)
|
|
|
|
if (Visited.insert(M).second)
|
|
|
|
Stack.push_back(M);
|
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();
|
|
|
|
|
|
|
|
// Emitting a v-table doesn't directly cause more v-tables to
|
|
|
|
// become deferred, although it can cause functions to be
|
|
|
|
// emitted that then need those v-tables.
|
|
|
|
assert(DeferredVTables.empty());
|
|
|
|
}
|
2010-03-10 10:19:29 +08:00
|
|
|
|
2015-01-22 08:24:57 +08:00
|
|
|
// Stop if we're out of both deferred v-tables and deferred declarations.
|
|
|
|
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;
|
|
|
|
llvm::GlobalValue *GV = G.GV;
|
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.
|
|
|
|
// FIXME: Support for variables is not implemented yet.
|
|
|
|
if (isa<FunctionDecl>(D.getDecl()))
|
|
|
|
GV = cast<llvm::GlobalValue>(GetAddrOfGlobal(D, /*IsForDefinition=*/true));
|
|
|
|
else
|
|
|
|
if (!GV)
|
|
|
|
GV = GetGlobalValue(getMangledName(D));
|
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.
|
2015-01-10 09:19:48 +08:00
|
|
|
if (GV && !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);
|
|
|
|
gv->setUnnamedAddr(true);
|
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.
|
|
|
|
if (!Loc.isInvalid())
|
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2012-10-11 18:13:44 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfUuidDescriptor(
|
|
|
|
const CXXUuidofExpr* E) {
|
|
|
|
// Sema has verified that IIDSource has a __declspec(uuid()), and that its
|
|
|
|
// well-formed.
|
2013-08-13 14:32:20 +08:00
|
|
|
StringRef Uuid = E->getUuidAsStringRef(Context);
|
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
|
|
|
|
|
|
|
// Look for an existing global.
|
|
|
|
if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
|
|
|
|
return GV;
|
|
|
|
|
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()));
|
2012-10-11 18:13:44 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-03-05 02:17:24 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
|
|
|
|
const AliasAttr *AA = VD->getAttr<AliasAttr>();
|
|
|
|
assert(AA && "No alias?");
|
|
|
|
|
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());
|
|
|
|
return llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
|
|
|
|
}
|
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
|
|
|
|
|
|
|
return Aliasee;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
if (!Global->hasAttr<CUDAHostAttr>() && (
|
|
|
|
Global->hasAttr<CUDADeviceAttr>() ||
|
|
|
|
Global->hasAttr<CUDAConstantAttr>() ||
|
|
|
|
Global->hasAttr<CUDASharedAttr>()))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.");
|
|
|
|
|
2014-07-18 04:25:23 +08:00
|
|
|
if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
|
|
|
|
!Context.isMSStaticDataMemberInlineDefinition(VD))
|
2009-02-14 05:18:01 +08:00
|
|
|
return;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
struct DLLImportFunctionVisitor
|
|
|
|
: public RecursiveASTVisitor<DLLImportFunctionVisitor> {
|
|
|
|
bool SafeToInline = true;
|
|
|
|
|
|
|
|
bool VisitVarDecl(VarDecl *VD) {
|
|
|
|
// A thread-local variable cannot be imported.
|
|
|
|
SafeToInline = !VD->getTLSKind();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we're not referencing non-imported vars or functions.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
|
|
|
|
SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
|
|
|
|
return SafeToInline;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-06-06 01:49:37 +08:00
|
|
|
CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-05-10 07:16:27 +08:00
|
|
|
/// If the type for the method's class was generated by
|
|
|
|
/// CGDebugInfo::createContextChain(), the cache contains only a
|
|
|
|
/// limited DIType without any declarations. Since EmitFunctionStart()
|
|
|
|
/// needs to find the canonical declaration for each method, we need
|
|
|
|
/// to construct the complete type prior to emitting the method.
|
|
|
|
void CodeGenModule::CompleteDIClassType(const CXXMethodDecl* D) {
|
|
|
|
if (!D->isInstance())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *ThisPtr = cast<PointerType>(D->getThisType(getContext()));
|
2013-05-10 07:16:27 +08:00
|
|
|
DI->getOrCreateRecordType(ThisPtr->getPointeeType(), D->getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2013-05-10 07:16:27 +08:00
|
|
|
CompleteDIClassType(Method);
|
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))
|
2010-04-14 01:57:11 +08:00
|
|
|
return EmitGlobalVarDefinition(VD);
|
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,
|
|
|
|
bool 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;
|
|
|
|
// Check that GD is not yet in ExplicitDefinitions is required to make
|
|
|
|
// sure that we issue an error only once.
|
|
|
|
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,
|
|
|
|
bool IsForDefinition) {
|
2009-03-23 05:03:39 +08:00
|
|
|
// If there was no specific requested type, just convert it now.
|
|
|
|
if (!Ty)
|
2009-09-11 07:38:47 +08:00
|
|
|
Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType());
|
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
|
|
|
|
2009-03-23 05:03:39 +08:00
|
|
|
/// CreateRuntimeFunction - Create a new runtime function with the specified
|
|
|
|
/// type and name.
|
|
|
|
llvm::Constant *
|
2011-07-18 12:24:23 +08:00
|
|
|
CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name,
|
2013-01-31 08:30:05 +08:00
|
|
|
llvm::AttributeSet ExtraAttrs) {
|
2013-12-09 12:29:47 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
|
2014-11-01 13:42:23 +08:00
|
|
|
/*DontDefer=*/false, /*IsThunk=*/false, ExtraAttrs);
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *F = dyn_cast<llvm::Function>(C))
|
2013-03-01 03:01:20 +08:00
|
|
|
if (F->empty())
|
|
|
|
F->setCallingConv(getRuntimeCC());
|
|
|
|
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.
|
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,
|
2014-05-23 07:33:27 +08:00
|
|
|
const VarDecl *D) {
|
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
|
|
|
|
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);
|
|
|
|
|
2009-07-30 02:54:39 +08:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
CXXThreadLocals.push_back(std::make_pair(D, GV));
|
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.
|
|
|
|
if (getTarget().getTriple().getArch() == llvm::Triple::xcore &&
|
|
|
|
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,
|
|
|
|
bool IsForDefinition) {
|
|
|
|
if (isa<CXXConstructorDecl>(GD.getDecl()))
|
|
|
|
return getAddrOfCXXStructor(cast<CXXConstructorDecl>(GD.getDecl()),
|
|
|
|
getFromCtorType(GD.getCtorType()),
|
|
|
|
/*FnInfo=*/nullptr, /*FnType=*/nullptr,
|
|
|
|
/*DontDefer=*/false, IsForDefinition);
|
|
|
|
else if (isa<CXXDestructorDecl>(GD.getDecl()))
|
|
|
|
return getAddrOfCXXStructor(cast<CXXDestructorDecl>(GD.getDecl()),
|
|
|
|
getFromDtorType(GD.getDtorType()),
|
|
|
|
/*FnInfo=*/nullptr, /*FnType=*/nullptr,
|
|
|
|
/*DontDefer=*/false, IsForDefinition);
|
|
|
|
else if (isa<CXXMethodDecl>(GD.getDecl())) {
|
|
|
|
auto FInfo = &getTypes().arrangeCXXMethodDeclaration(
|
|
|
|
cast<CXXMethodDecl>(GD.getDecl()));
|
|
|
|
auto Ty = getTypes().GetFunctionType(*FInfo);
|
|
|
|
return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
|
|
|
|
IsForDefinition);
|
|
|
|
} else if (isa<FunctionDecl>(GD.getDecl())) {
|
|
|
|
const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD);
|
|
|
|
llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
|
|
|
|
return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
|
|
|
|
IsForDefinition);
|
|
|
|
} else
|
|
|
|
return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
|
|
|
|
}
|
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
|
2009-03-23 05:03:39 +08:00
|
|
|
/// normal requested type would be.
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty) {
|
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);
|
2010-03-20 07:29:14 +08:00
|
|
|
return GetOrCreateLLVMGlobal(MangledName, PTy, D);
|
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!");
|
|
|
|
|
2015-01-10 09:19:48 +08:00
|
|
|
if (!MustBeEmitted(D)) {
|
2009-04-22 03:28:58 +08:00
|
|
|
// If we have not seen a reference to this variable yet, place it
|
|
|
|
// into the deferred declarations table to be emitted if needed
|
|
|
|
// later.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef MangledName = getMangledName(D);
|
2010-03-20 07:29:14 +08:00
|
|
|
if (!GetGlobalValue(MangledName)) {
|
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.
|
2009-04-16 06:08:45 +08:00
|
|
|
EmitGlobalVarDefinition(D);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-03-20 02:58:18 +08:00
|
|
|
if (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()));
|
|
|
|
}
|
|
|
|
|
2008-07-30 07:18:29 +08:00
|
|
|
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) {
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *Init = nullptr;
|
2008-05-31 03:50:47 +08:00
|
|
|
QualType ASTTy = D->getType();
|
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
|
|
|
|
2015-08-22 13:49:28 +08:00
|
|
|
// CUDA E.2.4.1 "__shared__ variables cannot have an initialization as part
|
|
|
|
// of their declaration."
|
|
|
|
if (getLangOpts().CPlusPlus && getLangOpts().CUDAIsDevice
|
|
|
|
&& D->hasAttr<CUDASharedAttr>()) {
|
|
|
|
if (InitExpr) {
|
|
|
|
Error(D->getLocation(),
|
|
|
|
"__shared__ variable cannot have an initialization.");
|
|
|
|
}
|
|
|
|
Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
|
|
|
|
} 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();
|
2009-03-21 17:16:30 +08:00
|
|
|
llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType);
|
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.
|
|
|
|
GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType));
|
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
|
|
|
|
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())."
|
|
|
|
if (GV && LangOpts.CUDA && LangOpts.CUDAIsDevice &&
|
|
|
|
(D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>())) {
|
|
|
|
GV->setExternallyInitialized(true);
|
|
|
|
}
|
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
|
|
|
|
2007-07-13 13:13:43 +08:00
|
|
|
// Set the llvm linkage type as appropriate.
|
2014-04-29 06:17:59 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage =
|
|
|
|
getLLVMLinkageVarDefinition(D, GV->isConstant());
|
2014-06-11 12:08:55 +08:00
|
|
|
|
|
|
|
// On Darwin, the backing variable for a C++11 thread_local variable always
|
|
|
|
// has internal linkage; all accesses should just be calls to the
|
|
|
|
// Itanium-specified entry point, which has the normal linkage of the
|
|
|
|
// variable.
|
2014-10-16 06:38:23 +08:00
|
|
|
if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
|
|
|
|
Context.getTargetInfo().getTriple().isMacOSX())
|
|
|
|
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
|
|
|
|
2010-10-28 00:21:54 +08:00
|
|
|
if (Linkage == llvm::GlobalVariable::CommonLinkage)
|
|
|
|
// common vars aren't constant even if declared const.
|
|
|
|
GV->setConstant(false);
|
|
|
|
|
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)
|
|
|
|
CXXThreadLocals.push_back(std::make_pair(D, GV));
|
|
|
|
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())
|
2012-10-24 04:05:01 +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;
|
|
|
|
|
2014-08-05 08:01:13 +08:00
|
|
|
// Declarations with a required alignment do not have common linakge in MSVC
|
|
|
|
// mode.
|
2015-02-03 16:49:32 +08:00
|
|
|
if (Context.getLangOpts().MSVCCompat) {
|
|
|
|
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)
|
|
|
|
return llvm::Function::AvailableExternallyLinkage;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (Linkage == GVA_StrongODR)
|
|
|
|
return !Context.getLangOpts().AppleKext ? llvm::Function::WeakODRLinkage
|
|
|
|
: llvm::Function::ExternalLinkage;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
llvm::CallSite newCall;
|
|
|
|
if (callSite.isCall()) {
|
|
|
|
newCall = llvm::CallInst::Create(newFn, newArgs, "",
|
|
|
|
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(),
|
|
|
|
newArgs, "",
|
|
|
|
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());
|
|
|
|
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) {
|
|
|
|
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,
|
|
|
|
/*IsForDefinition=*/true));
|
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) {
|
|
|
|
Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
|
|
|
|
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-04-30 05:22:47 +08:00
|
|
|
cast<llvm::PointerType>(Aliasee->getType()),
|
2014-06-03 10:42:01 +08:00
|
|
|
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) {
|
|
|
|
Diags.Report(AA->getLocation(), diag::err_cyclic_alias);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
SmallVector<UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
|
2012-09-06 23:59:27 +08:00
|
|
|
const UTF8 *FromPtr = (const UTF8 *)String.data();
|
2009-07-24 06:52:48 +08:00
|
|
|
UTF16 *ToPtr = &ToBuf[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-08 03:57:04 +08:00
|
|
|
(void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes,
|
|
|
|
&ToPtr, ToPtr + NumBytes,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-06 06:47:07 +08:00
|
|
|
static llvm::StringMapEntry<llvm::GlobalVariable *> &
|
|
|
|
GetConstantStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
|
|
|
|
const StringLiteral *Literal, unsigned &StringLength) {
|
2012-03-30 06:12:09 +08:00
|
|
|
StringRef String = Literal->getString();
|
|
|
|
StringLength = String.size();
|
2014-11-19 11:06:06 +08:00
|
|
|
return *Map.insert(std::make_pair(String, nullptr)).first;
|
2011-05-14 02:13:10 +08:00
|
|
|
}
|
|
|
|
|
2009-07-24 06:52:48 +08:00
|
|
|
llvm::Constant *
|
|
|
|
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)
|
2009-07-24 06:52:48 +08:00
|
|
|
return C;
|
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 };
|
2013-04-16 23:25:39 +08:00
|
|
|
llvm::Value *V;
|
|
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *GV = CreateRuntimeVariable(Ty,
|
2009-07-17 00:48:25 +08:00
|
|
|
"__CFConstantStringClassReference");
|
2008-08-24 02:37:06 +08:00
|
|
|
// Decay array -> ptr
|
2015-04-03 02:55:21 +08:00
|
|
|
V = llvm::ConstantExpr::getGetElementPtr(Ty, GV, Zeros);
|
2013-04-16 23:25:39 +08:00
|
|
|
CFConstantStringClassRef = V;
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
2013-04-16 23:25:39 +08:00
|
|
|
else
|
|
|
|
V = CFConstantStringClassRef;
|
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
|
|
|
|
2011-10-15 20:20:02 +08:00
|
|
|
llvm::Constant *Fields[4];
|
2008-12-12 00:49:14 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Class pointer.
|
2013-04-16 23:25:39 +08:00
|
|
|
Fields[0] = cast<llvm::ConstantExpr>(V);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// Flags.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) :
|
2009-08-16 13:55:31 +08:00
|
|
|
llvm::ConstantInt::get(Ty, 0x07C8);
|
|
|
|
|
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) {
|
2014-11-19 11:06:06 +08:00
|
|
|
ArrayRef<uint16_t> Arr = llvm::makeArrayRef<uint16_t>(
|
|
|
|
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");
|
2011-01-18 00:31:00 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
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.
|
2014-01-21 04:33:18 +08:00
|
|
|
// 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.
|
2009-04-03 08:57:44 +08:00
|
|
|
if (isUTF16) {
|
2010-01-27 02:46:23 +08:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2014-01-21 04:33:18 +08:00
|
|
|
GV->setSection("__TEXT,__ustring");
|
2011-04-13 07:30:52 +08:00
|
|
|
} else {
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2014-01-21 04:33:18 +08:00
|
|
|
GV->setSection("__TEXT,__cstring,cstring_literals");
|
2009-04-03 08:57:44 +08:00
|
|
|
}
|
2012-03-30 08:26:17 +08:00
|
|
|
|
|
|
|
// String.
|
2015-05-08 01:27:56 +08:00
|
|
|
Fields[2] =
|
|
|
|
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.
|
|
|
|
Fields[2] = llvm::ConstantExpr::getBitCast(Fields[2], Int8PtrTy);
|
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// String length.
|
|
|
|
Ty = getTypes().ConvertType(getContext().LongTy);
|
2009-08-16 13:55:31 +08:00
|
|
|
Fields[3] = llvm::ConstantInt::get(Ty, StringLength);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
// The struct.
|
2009-07-28 06:29:56 +08:00
|
|
|
C = llvm::ConstantStruct::get(STy, Fields);
|
2009-09-09 23:08:12 +08:00
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
2009-07-16 13:03:48 +08:00
|
|
|
"_unnamed_cfstring_");
|
2014-01-21 04:13:11 +08:00
|
|
|
GV->setSection("__DATA,__cfstring");
|
2014-11-19 11:06:06 +08:00
|
|
|
Entry.second = GV;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-01 08:41:52 +08:00
|
|
|
return GV;
|
2007-08-21 08:21:21 +08:00
|
|
|
}
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2015-04-06 06:45:47 +08:00
|
|
|
llvm::GlobalVariable *
|
2010-10-20 01:19:29 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) {
|
2010-04-24 01:41:07 +08:00
|
|
|
unsigned StringLength = 0;
|
2015-04-06 06:47:07 +08:00
|
|
|
llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
|
|
|
|
GetConstantStringEntry(CFConstantStringMap, Literal, StringLength);
|
2014-11-19 13:48:40 +08:00
|
|
|
|
2014-11-19 11:06:06 +08:00
|
|
|
if (auto *C = Entry.second)
|
2010-04-24 01:41:07 +08:00
|
|
|
return C;
|
|
|
|
|
2012-02-07 06:47:00 +08:00
|
|
|
llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
|
2010-04-24 01:41:07 +08:00
|
|
|
llvm::Constant *Zeros[] = { Zero, Zero };
|
2013-04-16 23:25:39 +08:00
|
|
|
llvm::Value *V;
|
2010-04-24 06:33:39 +08:00
|
|
|
// If we don't already have it, get _NSConstantStringClassReference.
|
2010-10-20 01:19:29 +08:00
|
|
|
if (!ConstantStringClassRef) {
|
2012-03-11 15:00:24 +08:00
|
|
|
std::string StringClass(getLangOpts().ObjCConstantStringClass);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
|
2010-10-20 01:19:29 +08:00
|
|
|
llvm::Constant *GV;
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2011-05-18 06:46:11 +08:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "OBJC_CLASS_$_NSConstantString"
|
|
|
|
: "OBJC_CLASS_$_" + StringClass;
|
2011-05-18 06:21:16 +08:00
|
|
|
GV = getObjCRuntime().GetClassGlobal(str);
|
|
|
|
// Make sure the result is of the correct type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
|
2013-04-16 23:25:39 +08:00
|
|
|
V = llvm::ConstantExpr::getBitCast(GV, PTy);
|
|
|
|
ConstantStringClassRef = V;
|
2011-05-18 06:21:16 +08:00
|
|
|
} else {
|
2011-05-18 06:46:11 +08:00
|
|
|
std::string str =
|
|
|
|
StringClass.empty() ? "_NSConstantStringClassReference"
|
|
|
|
: "_" + StringClass + "ClassReference";
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PTy = llvm::ArrayType::get(Ty, 0);
|
2011-05-18 06:46:11 +08:00
|
|
|
GV = CreateRuntimeVariable(PTy, str);
|
2011-05-18 06:21:16 +08:00
|
|
|
// Decay array -> ptr
|
2015-04-03 02:55:21 +08:00
|
|
|
V = llvm::ConstantExpr::getGetElementPtr(PTy, GV, Zeros);
|
2013-04-16 23:25:39 +08:00
|
|
|
ConstantStringClassRef = V;
|
2010-10-20 01:19:29 +08:00
|
|
|
}
|
2015-04-06 06:45:47 +08:00
|
|
|
} else
|
2013-04-16 23:25:39 +08:00
|
|
|
V = ConstantStringClassRef;
|
2011-08-09 23:54:21 +08:00
|
|
|
|
|
|
|
if (!NSConstantStringType) {
|
|
|
|
// Construct the type for a constant NSString.
|
2013-12-17 09:22:38 +08:00
|
|
|
RecordDecl *D = Context.buildImplicitRecord("__builtin_NSString");
|
2011-08-09 23:54:21 +08:00
|
|
|
D->startDefinition();
|
|
|
|
|
|
|
|
QualType FieldTypes[3];
|
|
|
|
|
|
|
|
// const int *isa;
|
|
|
|
FieldTypes[0] = Context.getPointerType(Context.IntTy.withConst());
|
|
|
|
// const char *str;
|
|
|
|
FieldTypes[1] = Context.getPointerType(Context.CharTy.withConst());
|
|
|
|
// unsigned int length;
|
|
|
|
FieldTypes[2] = Context.UnsignedIntTy;
|
|
|
|
|
|
|
|
// Create fields
|
|
|
|
for (unsigned i = 0; i < 3; ++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-09 23:54:21 +08:00
|
|
|
/*Mutable=*/false,
|
2012-06-10 11:12:00 +08:00
|
|
|
ICIS_NoInit);
|
2011-08-09 23:54:21 +08:00
|
|
|
Field->setAccess(AS_public);
|
|
|
|
D->addDecl(Field);
|
|
|
|
}
|
|
|
|
|
|
|
|
D->completeDefinition();
|
|
|
|
QualType NSTy = Context.getTagDeclType(D);
|
|
|
|
NSConstantStringType = cast<llvm::StructType>(getTypes().ConvertType(NSTy));
|
|
|
|
}
|
2010-04-24 01:41:07 +08:00
|
|
|
|
2011-10-15 20:20:02 +08:00
|
|
|
llvm::Constant *Fields[3];
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
// Class pointer.
|
2013-04-16 23:25:39 +08:00
|
|
|
Fields[0] = cast<llvm::ConstantExpr>(V);
|
2010-04-24 01:41:07 +08:00
|
|
|
|
|
|
|
// String pointer.
|
2012-02-05 10:30:40 +08:00
|
|
|
llvm::Constant *C =
|
2014-11-19 13:48:40 +08:00
|
|
|
llvm::ConstantDataArray::getString(VMContext, Entry.first());
|
|
|
|
|
2010-04-24 01:41:07 +08:00
|
|
|
llvm::GlobalValue::LinkageTypes Linkage;
|
|
|
|
bool isConstant;
|
2011-05-14 02:13:10 +08:00
|
|
|
Linkage = llvm::GlobalValue::PrivateLinkage;
|
2012-03-11 15:00:24 +08:00
|
|
|
isConstant = !LangOpts.WritableStrings;
|
2014-05-09 08:08:36 +08:00
|
|
|
|
|
|
|
auto *GV = new llvm::GlobalVariable(getModule(), C->getType(), isConstant,
|
|
|
|
Linkage, C, ".str");
|
2011-01-18 06:11:21 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
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.
|
2011-05-14 02:13:10 +08:00
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy);
|
|
|
|
GV->setAlignment(Align.getQuantity());
|
2015-04-03 02:55:21 +08:00
|
|
|
Fields[1] =
|
|
|
|
llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
|
|
|
|
|
2010-04-24 01:41:07 +08:00
|
|
|
// String length.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy);
|
2010-04-24 01:41:07 +08:00
|
|
|
Fields[2] = llvm::ConstantInt::get(Ty, StringLength);
|
|
|
|
|
|
|
|
// The struct.
|
2011-08-09 23:54:21 +08:00
|
|
|
C = llvm::ConstantStruct::get(NSConstantStringType, Fields);
|
2010-04-24 01:41:07 +08:00
|
|
|
GV = new llvm::GlobalVariable(getModule(), C->getType(), true,
|
|
|
|
llvm::GlobalVariable::PrivateLinkage, C,
|
|
|
|
"_unnamed_nsstring_");
|
2014-01-20 23:19:43 +08:00
|
|
|
const char *NSStringSection = "__OBJC,__cstring_object,regular,no_dead_strip";
|
|
|
|
const char *NSStringNonFragileABISection =
|
2014-01-21 04:13:11 +08:00
|
|
|
"__DATA,__objc_stringobj,regular,no_dead_strip";
|
2010-04-24 01:41:07 +08:00
|
|
|
// FIXME. Fix section.
|
2014-01-21 04:13:11 +08:00
|
|
|
GV->setSection(LangOpts.ObjCRuntime.isNonFragile()
|
|
|
|
? NSStringNonFragileABISection
|
|
|
|
: NSStringSection);
|
2014-11-19 11:06:06 +08:00
|
|
|
Entry.second = GV;
|
2014-11-19 13:48:40 +08:00
|
|
|
|
2010-04-24 01:41:07 +08:00
|
|
|
return GV;
|
2010-04-23 04:26:39 +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,
|
|
|
|
unsigned Alignment) {
|
|
|
|
// 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);
|
2014-06-05 03:56:57 +08:00
|
|
|
GV->setAlignment(Alignment);
|
|
|
|
GV->setUnnamedAddr(true);
|
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.
|
2014-07-11 06:18:36 +08:00
|
|
|
llvm::GlobalVariable *
|
2014-10-09 16:45:04 +08:00
|
|
|
CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S,
|
|
|
|
StringRef Name) {
|
2014-06-05 03:56:57 +08:00
|
|
|
auto Alignment =
|
|
|
|
getContext().getAlignOfGlobalVarInChars(S->getType()).getQuantity();
|
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) {
|
2014-06-05 03:56:57 +08:00
|
|
|
if (Alignment > GV->getAlignment())
|
|
|
|
GV->setAlignment(Alignment);
|
|
|
|
return GV;
|
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());
|
2011-11-01 10:23:42 +08:00
|
|
|
return GV;
|
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.
|
2014-07-11 06:18:36 +08:00
|
|
|
llvm::GlobalVariable *
|
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.
|
2014-07-11 06:18:36 +08:00
|
|
|
llvm::GlobalVariable *CodeGenModule::GetAddrOfConstantCString(
|
|
|
|
const std::string &Str, const char *GlobalName, unsigned Alignment) {
|
2014-06-05 04:25:57 +08:00
|
|
|
StringRef StrWithNull(Str.c_str(), Str.size() + 1);
|
2014-06-05 03:56:57 +08:00
|
|
|
if (Alignment == 0) {
|
|
|
|
Alignment = getContext()
|
|
|
|
.getAlignOfGlobalVarInChars(getContext().CharTy)
|
|
|
|
.getQuantity();
|
|
|
|
}
|
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) {
|
2014-06-05 03:56:57 +08:00
|
|
|
if (Alignment > GV->getAlignment())
|
|
|
|
GV->setAlignment(Alignment);
|
|
|
|
return GV;
|
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;
|
2011-08-04 09:03:22 +08:00
|
|
|
return GV;
|
2007-11-28 13:34:05 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
|
2013-06-05 08:46:14 +08:00
|
|
|
llvm::Constant *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
|
|
|
|
2015-08-14 07:50:15 +08:00
|
|
|
if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E])
|
2013-06-05 08:46:14 +08:00
|
|
|
return Slot;
|
|
|
|
|
|
|
|
// 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);
|
2013-06-05 08:46:14 +08:00
|
|
|
GV->setAlignment(
|
2013-06-14 11:07:01 +08:00
|
|
|
getContext().getTypeAlignInChars(MaterializedType).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;
|
2013-06-05 08:46:14 +08:00
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/// EmitNamespace - Emit all declarations in a namespace.
|
2009-04-01 08:58:25 +08:00
|
|
|
void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) {
|
2014-03-08 03:56:05 +08:00
|
|
|
for (auto *I : ND->decls()) {
|
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(I))
|
2013-09-05 05:07:37 +08:00
|
|
|
if (VD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization &&
|
|
|
|
VD->getTemplateSpecializationKind() != TSK_Undeclared)
|
|
|
|
continue;
|
2014-03-08 03:56:05 +08:00
|
|
|
EmitTopLevelDecl(I);
|
2013-09-05 05:07:37 +08:00
|
|
|
}
|
2009-04-01 08:58:25 +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;
|
|
|
|
}
|
|
|
|
|
2014-03-08 03:56:05 +08:00
|
|
|
for (auto *I : LSD->decls()) {
|
2012-10-27 06:20:25 +08:00
|
|
|
// Meta-data for ObjC class includes references to implemented methods.
|
|
|
|
// Generate class's method definitions first.
|
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
|
|
|
}
|
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:
|
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));
|
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:
|
2009-04-01 08:58:25 +08:00
|
|
|
EmitNamespace(cast<NamespaceDecl>(D));
|
2008-08-16 07:26:23 +08:00
|
|
|
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())
|
2012-12-04 02:28:12 +08:00
|
|
|
if (getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
|
|
|
|
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;
|
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
// Ignore import declarations that come from imported modules.
|
2015-08-19 10:30:28 +08:00
|
|
|
if (Import->getImportedOwningModule())
|
|
|
|
break;
|
2015-07-01 01:39:51 +08:00
|
|
|
if (CGDebugInfo *DI = getModuleDebugInfo())
|
|
|
|
DI->EmitImportDecl(*Import);
|
2013-01-15 01:21:00 +08:00
|
|
|
|
2013-01-15 04:53:57 +08:00
|
|
|
ImportedModules.insert(Import->getImportedModule());
|
2013-01-15 01:21:00 +08:00
|
|
|
break;
|
2014-03-04 07:48:23 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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() {
|
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
|
|
|
|
|
|
|
// StaticLocalDeclMap
|
2014-07-03 17:30:29 +08:00
|
|
|
for (auto &I : MangledDeclNames) {
|
|
|
|
llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
|
|
|
|
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;
|
|
|
|
llvm::Value *Addr = I.second;
|
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() {
|
|
|
|
if (!getCodeGenOpts().CoverageFile.empty()) {
|
2011-05-05 04:46:58 +08:00
|
|
|
if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) {
|
|
|
|
llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
|
|
|
|
llvm::LLVMContext &Ctx = TheModule.getContext();
|
2011-05-05 08:08:20 +08:00
|
|
|
llvm::MDString *CoverageFile =
|
|
|
|
llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile);
|
2011-05-05 04:46:58 +08:00
|
|
|
for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
|
2014-11-12 05:31:03 +08:00
|
|
|
llvm::MDNode *CU = CUNode->getOperand(i);
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Metadata *Elts[] = {CoverageFile, 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
|
|
|
|
2015-03-18 04:35:05 +08:00
|
|
|
llvm::Constant *
|
2015-03-30 05:55:10 +08:00
|
|
|
CodeGenModule::getAddrOfCXXCatchHandlerType(QualType Ty,
|
|
|
|
QualType CatchHandlerType) {
|
|
|
|
return getCXXABI().getAddrOfCXXCatchHandlerType(Ty, CatchHandlerType);
|
2015-03-18 04:35:00 +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);
|
2015-02-25 16:32:46 +08:00
|
|
|
if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
|
|
|
|
VD, GetAddrOfGlobalVar(VD), RefExpr->getLocStart(), PerformInit))
|
2014-11-11 12:05:39 +08:00
|
|
|
CXXGlobalInits.push_back(InitFunction);
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 03:08:05 +08:00
|
|
|
|
|
|
|
llvm::MDTuple *CodeGenModule::CreateVTableBitSetEntry(
|
|
|
|
llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD) {
|
|
|
|
std::string OutName;
|
|
|
|
llvm::raw_string_ostream Out(OutName);
|
|
|
|
getCXXABI().getMangleContext().mangleCXXVTableBitSet(RD, Out);
|
|
|
|
|
|
|
|
llvm::Metadata *BitsetOps[] = {
|
|
|
|
llvm::MDString::get(getLLVMContext(), Out.str()),
|
|
|
|
llvm::ConstantAsMetadata::get(VTable),
|
|
|
|
llvm::ConstantAsMetadata::get(
|
|
|
|
llvm::ConstantInt::get(Int64Ty, Offset.getQuantity()))};
|
|
|
|
return llvm::MDTuple::get(getLLVMContext(), BitsetOps);
|
|
|
|
}
|