summaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorArthur Eubanks <aeubanks@google.com>2021-05-17 10:07:28 -0700
committerArthur Eubanks <aeubanks@google.com>2021-05-17 18:41:17 -0700
commit9f7d552cff8d62250461ef396d9d4ffab4ecd378 (patch)
tree5489f251d74eba226bef85d6651cb7bd773c3772 /clang/lib/CodeGen/CodeGenModule.cpp
parentcc64ece77dddfa7aa590b6d69fe026fcd64d00cf (diff)
[NFC] Pass GV value type instead of pointer type to GetOrCreateLLVMGlobal
For opaque pointers, to avoid PointerType::getElementType(). Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D102638
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenModule.cpp69
1 files changed, 32 insertions, 37 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index b408ea30e423..8ce33c3124ca 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2808,9 +2808,7 @@ ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) {
GlobalDecl(cast<FunctionDecl>(VD)),
/*ForVTable=*/false);
else
- Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
- llvm::PointerType::getUnqual(DeclTy),
- nullptr);
+ Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0, nullptr);
auto *F = cast<llvm::GlobalValue>(Aliasee);
F->setLinkage(llvm::Function::ExternalWeakLinkage);
@@ -3772,9 +3770,9 @@ bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
}
/// 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.
+/// create and return an llvm GlobalVariable with the specified type and address
+/// space. 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.
@@ -3783,9 +3781,8 @@ bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
/// type Ty will be returned, not conversion of a variable with the same
/// mangled name but some other type.
llvm::Constant *
-CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
- llvm::PointerType *Ty,
- const VarDecl *D,
+CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
+ unsigned AddrSpace, const VarDecl *D,
ForDefinition_t IsForDefinition) {
// Lookup the entry, lazily creating it if necessary.
llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
@@ -3802,7 +3799,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
- if (Entry->getType() == Ty)
+ if (Entry->getValueType() == Ty && Entry->getAddressSpace() == AddrSpace)
return Entry;
// If there are two attempts to define the same mangled name, issue an
@@ -3826,22 +3823,24 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
}
// Make sure the result is of the correct type.
- if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
- return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
+ if (Entry->getType()->getAddressSpace() != AddrSpace) {
+ return llvm::ConstantExpr::getAddrSpaceCast(Entry,
+ Ty->getPointerTo(AddrSpace));
+ }
// (If global is requested for a definition, we always need to create a new
// global, not just return a bitcast.)
if (!IsForDefinition)
- return llvm::ConstantExpr::getBitCast(Entry, Ty);
+ return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(AddrSpace));
}
- auto AddrSpace = GetGlobalVarAddressSpace(D);
- auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace);
+ auto DAddrSpace = GetGlobalVarAddressSpace(D);
+ auto TargetAddrSpace = getContext().getTargetAddressSpace(DAddrSpace);
auto *GV = new llvm::GlobalVariable(
- getModule(), Ty->getElementType(), false,
- llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
- llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace);
+ getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
+ MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
+ TargetAddrSpace);
// If we already created a global with the same mangled name (but different
// type) before, take its name and remove it from its parent.
@@ -3964,11 +3963,11 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
LangAS ExpectedAS =
D ? D->getType().getAddressSpace()
: (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
- assert(getContext().getTargetAddressSpace(ExpectedAS) ==
- Ty->getPointerAddressSpace());
- if (AddrSpace != ExpectedAS)
- return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace,
- ExpectedAS, Ty);
+ assert(getContext().getTargetAddressSpace(ExpectedAS) == AddrSpace);
+ if (DAddrSpace != ExpectedAS) {
+ return getTargetCodeGenInfo().performAddrSpaceCast(
+ *this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(AddrSpace));
+ }
return GV;
}
@@ -4056,11 +4055,10 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
if (!Ty)
Ty = getTypes().ConvertTypeForMem(ASTTy);
- llvm::PointerType *PTy =
- llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
-
StringRef MangledName = getMangledName(D);
- return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition);
+ return GetOrCreateLLVMGlobal(MangledName, Ty,
+ getContext().getTargetAddressSpace(ASTTy), D,
+ IsForDefinition);
}
/// CreateRuntimeVariable - Create a new runtime global variable with the
@@ -4068,12 +4066,11 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D,
llvm::Constant *
CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty,
StringRef Name) {
- auto PtrTy =
+ auto AddrSpace =
getContext().getLangOpts().OpenCL
- ? llvm::PointerType::get(
- Ty, getContext().getTargetAddressSpace(LangAS::opencl_global))
- : llvm::PointerType::getUnqual(Ty);
- auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr);
+ ? getContext().getTargetAddressSpace(LangAS::opencl_global)
+ : 0;
+ auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
return Ret;
}
@@ -4487,9 +4484,8 @@ void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
if (getCodeGenOpts().hasReducedDebugInfo()) {
QualType ASTTy = D->getType();
llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
- llvm::PointerType *PTy =
- llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
- llvm::Constant *GV = GetOrCreateLLVMGlobal(D->getName(), PTy, D);
+ llvm::Constant *GV = GetOrCreateLLVMGlobal(
+ D->getName(), Ty, getContext().getTargetAddressSpace(ASTTy), D);
DI->EmitExternalVariable(
cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
}
@@ -4861,8 +4857,7 @@ void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
/*ForVTable=*/false);
LT = getFunctionLinkage(GD);
} else {
- Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
- llvm::PointerType::getUnqual(DeclTy),
+ Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, 0,
/*D=*/nullptr);
if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl()))
LT = getLLVMLinkageVarDefinition(VD, D->getType().isConstQualified());