[clang] [clang codegen][NFC] Delete dead code in constant emission. (PR #90106)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 12:17:19 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Eli Friedman (efriedma-quic)

<details>
<summary>Changes</summary>

This code was apparently added in de0fe07eef, but never used.

I think more of the related code might actually dead, but I haven't tried to dig more deeply.

---
Full diff: https://github.com/llvm/llvm-project/pull/90106.diff


4 Files Affected:

- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+1-1) 
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+4-34) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+4-6) 
- (modified) clang/lib/CodeGen/ConstantEmitter.h (+3-27) 


``````````diff
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 355fec42be4489..318d0e031095ea 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -537,7 +537,7 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
     ConstantEmitter Emitter(CGF);
     LangAS AS = ArrayQTy.getAddressSpace();
     if (llvm::Constant *C =
-            Emitter.tryEmitForInitializer(ExprToVisit, AS, ArrayQTy)) {
+            Emitter.tryEmitForInitializer(ExprToVisit, ArrayQTy)) {
       auto GV = new llvm::GlobalVariable(
           CGM.getModule(), C->getType(),
           /* isConstant= */ true, llvm::GlobalValue::PrivateLinkage, C,
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index c924660c5a91c8..91582c3257934c 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -926,7 +926,7 @@ tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
 
   LangAS addressSpace = E->getType().getAddressSpace();
   llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
-                                                    addressSpace, E->getType());
+                                                    E->getType());
   if (!C) {
     assert(!E->isFileScope() &&
            "file-scope compound literal did not have constant initializer!");
@@ -1474,54 +1474,24 @@ ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
 }
 
 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
-  initializeNonAbstract(D.getType().getAddressSpace());
+  initializeNonAbstract();
   return markIfFailed(tryEmitPrivateForVarInit(D));
 }
 
 llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
-                                                       LangAS destAddrSpace,
                                                        QualType destType) {
-  initializeNonAbstract(destAddrSpace);
+  initializeNonAbstract();
   return markIfFailed(tryEmitPrivateForMemory(E, destType));
 }
 
 llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
-                                                    LangAS destAddrSpace,
                                                     QualType destType) {
-  initializeNonAbstract(destAddrSpace);
+  initializeNonAbstract();
   auto C = tryEmitPrivateForMemory(value, destType);
   assert(C && "couldn't emit constant value non-abstractly?");
   return C;
 }
 
-llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
-  assert(!Abstract && "cannot get current address for abstract constant");
-
-
-
-  // Make an obviously ill-formed global that should blow up compilation
-  // if it survives.
-  auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
-                                         llvm::GlobalValue::PrivateLinkage,
-                                         /*init*/ nullptr,
-                                         /*name*/ "",
-                                         /*before*/ nullptr,
-                                         llvm::GlobalVariable::NotThreadLocal,
-                                         CGM.getContext().getTargetAddressSpace(DestAddressSpace));
-
-  PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
-
-  return global;
-}
-
-void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
-                                           llvm::GlobalValue *placeholder) {
-  assert(!PlaceholderAddresses.empty());
-  assert(PlaceholderAddresses.back().first == nullptr);
-  assert(PlaceholderAddresses.back().second == placeholder);
-  PlaceholderAddresses.back().first = signal;
-}
-
 namespace {
   struct ReplacePlaceholders {
     CodeGenModule &CGM;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 0c447b20cef40d..a6592311c7a01f 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3503,8 +3503,7 @@ ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) {
   if (!V.isAbsent()) {
     // If possible, emit the APValue version of the initializer. In particular,
     // this gets the type of the constant right.
-    Init = Emitter.emitForInitializer(
-        GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
+    Init = Emitter.emitForInitializer(GD->getAsAPValue(), GD->getType());
   } else {
     // As a fallback, directly construct the constant.
     // FIXME: This may get padding wrong under esoteric struct layout rules.
@@ -3552,8 +3551,7 @@ ConstantAddress CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl(
   const APValue &V = GCD->getValue();
 
   assert(!V.isAbsent());
-  Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
-                                    GCD->getType());
+  Init = Emitter.emitForInitializer(V, GCD->getType());
 
   auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
                                       /*isConstant=*/true,
@@ -3578,7 +3576,7 @@ ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
 
   ConstantEmitter Emitter(*this);
   llvm::Constant *Init = Emitter.emitForInitializer(
-        TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
+        TPO->getValue(), TPO->getType());
 
   if (!Init) {
     ErrorUnsupported(TPO, "template parameter object");
@@ -6520,7 +6518,7 @@ ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
   if (Value) {
     // The temporary has a constant initializer, use it.
     emitter.emplace(*this);
-    InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
+    InitialValue = emitter->emitForInitializer(*Value,
                                                MaterializedType);
     Constant =
         MaterializedType.isConstantStorage(getContext(), /*ExcludeCtor*/ Value,
diff --git a/clang/lib/CodeGen/ConstantEmitter.h b/clang/lib/CodeGen/ConstantEmitter.h
index a55da0dcad7921..52009adbf0ebe4 100644
--- a/clang/lib/CodeGen/ConstantEmitter.h
+++ b/clang/lib/CodeGen/ConstantEmitter.h
@@ -40,10 +40,6 @@ class ConstantEmitter {
   /// Whether we're in a constant context.
   bool InConstantContext = false;
 
-  /// The AST address space where this (non-abstract) initializer is going.
-  /// Used for generating appropriate placeholders.
-  LangAS DestAddressSpace = LangAS::Default;
-
   llvm::SmallVector<std::pair<llvm::Constant *, llvm::GlobalVariable*>, 4>
     PlaceholderAddresses;
 
@@ -73,10 +69,8 @@ class ConstantEmitter {
   /// Try to emit the initiaizer of the given declaration as an abstract
   /// constant.  If this succeeds, the emission must be finalized.
   llvm::Constant *tryEmitForInitializer(const VarDecl &D);
-  llvm::Constant *tryEmitForInitializer(const Expr *E, LangAS destAddrSpace,
-                                        QualType destType);
-  llvm::Constant *emitForInitializer(const APValue &value, LangAS destAddrSpace,
-                                     QualType destType);
+  llvm::Constant *tryEmitForInitializer(const Expr *E, QualType destType);
+  llvm::Constant *emitForInitializer(const APValue &value, QualType destType);
 
   void finalize(llvm::GlobalVariable *global);
 
@@ -138,28 +132,10 @@ class ConstantEmitter {
   llvm::Constant *tryEmitPrivate(const APValue &value, QualType T);
   llvm::Constant *tryEmitPrivateForMemory(const APValue &value, QualType T);
 
-  /// Get the address of the current location.  This is a constant
-  /// that will resolve, after finalization, to the address of the
-  /// 'signal' value that is registered with the emitter later.
-  llvm::GlobalValue *getCurrentAddrPrivate();
-
-  /// Register a 'signal' value with the emitter to inform it where to
-  /// resolve a placeholder.  The signal value must be unique in the
-  /// initializer; it might, for example, be the address of a global that
-  /// refers to the current-address value in its own initializer.
-  ///
-  /// Uses of the placeholder must be properly anchored before finalizing
-  /// the emitter, e.g. by being installed as the initializer of a global
-  /// variable.  That is, it must be possible to replaceAllUsesWith
-  /// the placeholder with the proper address of the signal.
-  void registerCurrentAddrPrivate(llvm::Constant *signal,
-                                  llvm::GlobalValue *placeholder);
-
 private:
-  void initializeNonAbstract(LangAS destAS) {
+  void initializeNonAbstract() {
     assert(!InitializedNonAbstract);
     InitializedNonAbstract = true;
-    DestAddressSpace = destAS;
   }
   llvm::Constant *markIfFailed(llvm::Constant *init) {
     if (!init)

``````````

</details>


https://github.com/llvm/llvm-project/pull/90106


More information about the cfe-commits mailing list