Skip to content

Commit 645f0e6

Browse files
authored
IR: Make Module::getOrInsertGlobal() return a GlobalVariable.
After pointer element types were removed this function can only return a GlobalVariable, so reflect that in the type and comments and clean up callers. Reviewers: nikic Reviewed By: nikic Pull Request: #141323
1 parent ea88384 commit 645f0e6

File tree

13 files changed

+32
-49
lines changed

13 files changed

+32
-49
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,14 @@ class LLVM_ABI Module {
471471

472472
/// Look up the specified global in the module symbol table.
473473
/// If it does not exist, invoke a callback to create a declaration of the
474-
/// global and return it. The global is constantexpr casted to the expected
475-
/// type if necessary.
476-
Constant *
474+
/// global and return it.
475+
GlobalVariable *
477476
getOrInsertGlobal(StringRef Name, Type *Ty,
478477
function_ref<GlobalVariable *()> CreateGlobalCallback);
479478

480479
/// Look up the specified global in the module symbol table. If required, this
481480
/// overload constructs the global variable using its constructor's defaults.
482-
Constant *getOrInsertGlobal(StringRef Name, Type *Ty);
481+
GlobalVariable *getOrInsertGlobal(StringRef Name, Type *Ty);
483482

484483
/// @}
485484
/// @name Global Alias Accessors

llvm/lib/CodeGen/LowerEmuTLS.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
140140
PointerType *InitPtrType = PointerType::getUnqual(C);
141141
Type *ElementTypes[4] = {WordType, WordType, VoidPtrType, InitPtrType};
142142
StructType *EmuTlsVarType = StructType::create(ElementTypes);
143-
EmuTlsVar = cast<GlobalVariable>(
144-
M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType));
143+
EmuTlsVar = M.getOrInsertGlobal(EmuTlsVarName, EmuTlsVarType);
145144
copyLinkageVisibility(M, GV, EmuTlsVar);
146145

147146
// Define "__emutls_t.*" and "__emutls_v.*" only if GV is defined.
@@ -155,8 +154,7 @@ bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
155154
GlobalVariable *EmuTlsTmplVar = nullptr;
156155
if (InitValue) {
157156
std::string EmuTlsTmplName = ("__emutls_t." + GV->getName()).str();
158-
EmuTlsTmplVar = dyn_cast_or_null<GlobalVariable>(
159-
M.getOrInsertGlobal(EmuTlsTmplName, GVType));
157+
EmuTlsTmplVar = M.getOrInsertGlobal(EmuTlsTmplName, GVType);
160158
assert(EmuTlsTmplVar && "Failed to create emualted TLS initializer");
161159
EmuTlsTmplVar->setConstant(true);
162160
EmuTlsTmplVar->setInitializer(const_cast<Constant*>(InitValue));

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,9 @@ Value *TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB) const {
19751975
if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
19761976
Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
19771977
PointerType *PtrTy = PointerType::getUnqual(M.getContext());
1978-
Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
1979-
if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
1980-
G->setVisibility(GlobalValue::HiddenVisibility);
1981-
return C;
1978+
GlobalVariable *G = M.getOrInsertGlobal("__guard_local", PtrTy);
1979+
G->setVisibility(GlobalValue::HiddenVisibility);
1980+
return G;
19821981
}
19831982
return nullptr;
19841983
}

llvm/lib/CodeGen/WasmEHPrepare.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ bool WasmEHPrepareImpl::prepareEHPads(Function &F) {
247247
// we depend on CoalesceFeaturesAndStripAtomics to downgrade it to
248248
// non-thread-local ones, in which case we don't allow this object to be
249249
// linked with other objects using shared memory.
250-
LPadContextGV = cast<GlobalVariable>(
251-
M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy));
250+
LPadContextGV = M.getOrInsertGlobal("__wasm_lpad_context", LPadContextTy);
252251
LPadContextGV->setThreadLocalMode(GlobalValue::GeneralDynamicTLSModel);
253252

254253
LPadIndexField = LPadContextGV;

llvm/lib/IR/Module.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,9 @@ GlobalVariable *Module::getGlobalVariable(StringRef Name,
250250
}
251251

252252
/// getOrInsertGlobal - Look up the specified global in the module symbol table.
253-
/// 1. If it does not exist, add a declaration of the global and return it.
254-
/// 2. Else, the global exists but has the wrong type: return the function
255-
/// with a constantexpr cast to the right type.
256-
/// 3. Finally, if the existing global is the correct declaration, return the
257-
/// existing global.
258-
Constant *Module::getOrInsertGlobal(
253+
/// If it does not exist, add a declaration of the global and return it.
254+
/// Otherwise, return the existing global.
255+
GlobalVariable *Module::getOrInsertGlobal(
259256
StringRef Name, Type *Ty,
260257
function_ref<GlobalVariable *()> CreateGlobalCallback) {
261258
// See if we have a definition for the specified global already.
@@ -269,7 +266,7 @@ Constant *Module::getOrInsertGlobal(
269266
}
270267

271268
// Overload to construct a global variable using its constructor's defaults.
272-
Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
269+
GlobalVariable *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
273270
return getOrInsertGlobal(Name, Ty, [&] {
274271
return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
275272
nullptr, Name);

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -983,11 +983,10 @@ LowerTypeTestsModule::importTypeId(StringRef TypeId) {
983983
auto ImportGlobal = [&](StringRef Name) {
984984
// Give the global a type of length 0 so that it is not assumed not to alias
985985
// with any other global.
986-
Constant *C = M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(),
987-
Int8Arr0Ty);
988-
if (auto *GV = dyn_cast<GlobalVariable>(C))
989-
GV->setVisibility(GlobalValue::HiddenVisibility);
990-
return C;
986+
GlobalVariable *GV = M.getOrInsertGlobal(
987+
("__typeid_" + TypeId + "_" + Name).str(), Int8Arr0Ty);
988+
GV->setVisibility(GlobalValue::HiddenVisibility);
989+
return GV;
991990
};
992991

993992
auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,12 +1689,10 @@ void DevirtModule::exportConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,
16891689

16901690
Constant *DevirtModule::importGlobal(VTableSlot Slot, ArrayRef<uint64_t> Args,
16911691
StringRef Name) {
1692-
Constant *C =
1692+
GlobalVariable *GV =
16931693
M.getOrInsertGlobal(getGlobalName(Slot, Args, Name), Int8Arr0Ty);
1694-
auto *GV = dyn_cast<GlobalVariable>(C);
1695-
if (GV)
1696-
GV->setVisibility(GlobalValue::HiddenVisibility);
1697-
return C;
1694+
GV->setVisibility(GlobalValue::HiddenVisibility);
1695+
return GV;
16981696
}
16991697

17001698
Constant *DevirtModule::importConstant(VTableSlot Slot, ArrayRef<uint64_t> Args,

llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,12 +1506,10 @@ bool DataFlowSanitizer::runImpl(
15061506

15071507
auto GetOrInsertGlobal = [this, &Changed](StringRef Name,
15081508
Type *Ty) -> Constant * {
1509-
Constant *C = Mod->getOrInsertGlobal(Name, Ty);
1510-
if (GlobalVariable *G = dyn_cast<GlobalVariable>(C)) {
1511-
Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel;
1512-
G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1513-
}
1514-
return C;
1509+
GlobalVariable *G = Mod->getOrInsertGlobal(Name, Ty);
1510+
Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel;
1511+
G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1512+
return G;
15151513
};
15161514

15171515
// These globals must be kept in sync with the ones in dfsan.cpp.

llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,14 @@ void HWAddressSanitizer::initializeModule() {
692692
}
693693

694694
if (!TargetTriple.isAndroid()) {
695-
Constant *C = M.getOrInsertGlobal("__hwasan_tls", IntptrTy, [&] {
695+
ThreadPtrGlobal = M.getOrInsertGlobal("__hwasan_tls", IntptrTy, [&] {
696696
auto *GV = new GlobalVariable(M, IntptrTy, /*isConstant=*/false,
697697
GlobalValue::ExternalLinkage, nullptr,
698698
"__hwasan_tls", nullptr,
699699
GlobalVariable::InitialExecTLSModel);
700700
appendToCompilerUsed(M, GV);
701701
return GV;
702702
});
703-
ThreadPtrGlobal = cast<GlobalVariable>(C);
704703
}
705704
}
706705

llvm/lib/Transforms/Instrumentation/NumericalStabilitySanitizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,11 @@ NumericalStabilitySanitizerPass::run(Module &M, ModuleAnalysisManager &MAM) {
642642
}
643643

644644
static GlobalValue *createThreadLocalGV(const char *Name, Module &M, Type *Ty) {
645-
return dyn_cast<GlobalValue>(M.getOrInsertGlobal(Name, Ty, [&M, Ty, Name] {
645+
return M.getOrInsertGlobal(Name, Ty, [&M, Ty, Name] {
646646
return new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
647647
nullptr, Name, nullptr,
648648
GlobalVariable::InitialExecTLSModel);
649-
}));
649+
});
650650
}
651651

652652
NumericalStabilitySanitizer::NumericalStabilitySanitizer(Module &M)

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,8 @@ bool ModuleSanitizerCoverage::instrumentModule() {
486486
SanCovTraceSwitchFunction =
487487
M.getOrInsertFunction(SanCovTraceSwitchName, VoidTy, Int64Ty, PtrTy);
488488

489-
Constant *SanCovLowestStackConstant =
490-
M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
491-
SanCovLowestStack = dyn_cast<GlobalVariable>(SanCovLowestStackConstant);
492-
if (!SanCovLowestStack || SanCovLowestStack->getValueType() != IntptrTy) {
489+
SanCovLowestStack = M.getOrInsertGlobal(SanCovLowestStackName, IntptrTy);
490+
if (SanCovLowestStack->getValueType() != IntptrTy) {
493491
C->emitError(StringRef("'") + SanCovLowestStackName +
494492
"' should not be declared by the user");
495493
return true;

llvm/unittests/Analysis/BasicAliasAnalysisTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ TEST_F(BasicAATest, AliasInstWithObjectOfImpreciseSize) {
8181

8282
Value *IncomingI32Ptr = F->arg_begin();
8383

84-
auto *GlobalPtr =
85-
cast<GlobalVariable>(M.getOrInsertGlobal("some_global", B.getInt8Ty()));
84+
auto *GlobalPtr = M.getOrInsertGlobal("some_global", B.getInt8Ty());
8685

8786
// Without sufficiently restricted linkage/an init, some of the object size
8887
// checking bits get more conservative.

llvm/unittests/IR/ConstantsTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,12 +774,12 @@ TEST(ConstantsTest, ComdatUserTracking) {
774774
EXPECT_TRUE(Users.size() == 0);
775775

776776
Type *Ty = Type::getInt8Ty(Context);
777-
GlobalVariable *GV1 = cast<GlobalVariable>(M.getOrInsertGlobal("gv1", Ty));
777+
GlobalVariable *GV1 = M.getOrInsertGlobal("gv1", Ty);
778778
GV1->setComdat(C);
779779
EXPECT_TRUE(Users.size() == 1);
780780
EXPECT_TRUE(Users.contains(GV1));
781781

782-
GlobalVariable *GV2 = cast<GlobalVariable>(M.getOrInsertGlobal("gv2", Ty));
782+
GlobalVariable *GV2 = M.getOrInsertGlobal("gv2", Ty);
783783
GV2->setComdat(C);
784784
EXPECT_TRUE(Users.size() == 2);
785785
EXPECT_TRUE(Users.contains(GV2));

0 commit comments

Comments
 (0)