aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGDecl.cpp
diff options
context:
space:
mode:
authorEli Friedman <efriedma@quicinc.com>2024-08-09 09:22:40 -0700
committerGitHub <noreply@github.com>2024-08-09 09:22:40 -0700
commit2f8f58dd17a11934e8c8ec212b6474f76fb18e61 (patch)
tree9ec3e09723b14832c2c97de2d89430dd3c6d0726 /clang/lib/CodeGen/CGDecl.cpp
parent5c016bf40ea38bc0198b53b1eff743d305e16707 (diff)
downloadllvm-2f8f58dd17a11934e8c8ec212b6474f76fb18e61.zip
llvm-2f8f58dd17a11934e8c8ec212b6474f76fb18e61.tar.gz
llvm-2f8f58dd17a11934e8c8ec212b6474f76fb18e61.tar.bz2
[IR] Add method to GlobalVariable to change type of initializer. (#102553)
With opaque pointers, nothing directly uses the value type, so we can mutate it if we want. This avoid doing a complicated RAUW dance.
Diffstat (limited to 'clang/lib/CodeGen/CGDecl.cpp')
-rw-r--r--clang/lib/CodeGen/CGDecl.cpp28
1 files changed, 1 insertions, 27 deletions
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 882dbad..563f728 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -370,38 +370,12 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
assert(VarSize == CstSize && "Emitted constant has unexpected size");
#endif
- // The initializer may differ in type from the global. Rewrite
- // the global to match the initializer. (We have to do this
- // because some types, like unions, can't be completely represented
- // in the LLVM type system.)
- if (GV->getValueType() != Init->getType()) {
- llvm::GlobalVariable *OldGV = GV;
-
- GV = new llvm::GlobalVariable(
- CGM.getModule(), Init->getType(), OldGV->isConstant(),
- OldGV->getLinkage(), Init, "",
- /*InsertBefore*/ OldGV, OldGV->getThreadLocalMode(),
- OldGV->getType()->getPointerAddressSpace());
- GV->setVisibility(OldGV->getVisibility());
- GV->setDSOLocal(OldGV->isDSOLocal());
- GV->setComdat(OldGV->getComdat());
-
- // Steal the name of the old global
- GV->takeName(OldGV);
-
- // Replace all uses of the old global with the new global
- OldGV->replaceAllUsesWith(GV);
-
- // Erase the old global, since it is no longer used.
- OldGV->eraseFromParent();
- }
-
bool NeedsDtor =
D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
GV->setConstant(
D.getType().isConstantStorage(getContext(), true, !NeedsDtor));
- GV->setInitializer(Init);
+ GV->replaceInitializer(Init);
emitter.finalize(GV);