[llvm] 7d9d855 - [GlobalOpt] Add struct for parts during GlobalSRA (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 22 07:35:19 PST 2023


Author: Nikita Popov
Date: 2023-02-22T16:35:10+01:00
New Revision: 7d9d855a073a496540d4e40e67945b6f09d3aea7

URL: https://github.com/llvm/llvm-project/commit/7d9d855a073a496540d4e40e67945b6f09d3aea7
DIFF: https://github.com/llvm/llvm-project/commit/7d9d855a073a496540d4e40e67945b6f09d3aea7.diff

LOG: [GlobalOpt] Add struct for parts during GlobalSRA (NFC)

This is in preparation for https://reviews.llvm.org/D129857.

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/GlobalOpt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 11621d9b67a9d..435f38f55f7cc 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -335,10 +335,14 @@ static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
   return Changed;
 }
 
+struct GlobalPart {
+  Type *Ty;
+};
+
 /// Look at all uses of the global and determine which (offset, type) pairs it
 /// can be split into.
-static bool collectSRATypes(DenseMap<uint64_t, Type *> &Types, GlobalValue *GV,
-                            const DataLayout &DL) {
+static bool collectSRATypes(DenseMap<uint64_t, GlobalPart> &Parts,
+                            GlobalValue *GV, const DataLayout &DL) {
   SmallVector<Use *, 16> Worklist;
   SmallPtrSet<Use *, 16> Visited;
   auto AppendUses = [&](Value *V) {
@@ -373,8 +377,8 @@ static bool collectSRATypes(DenseMap<uint64_t, Type *> &Types, GlobalValue *GV,
       // TODO: We currently require that all accesses at a given offset must
       // use the same type. This could be relaxed.
       Type *Ty = getLoadStoreType(V);
-      auto It = Types.try_emplace(Offset.getZExtValue(), Ty).first;
-      if (Ty != It->second)
+      auto It = Parts.try_emplace(Offset.getZExtValue(), GlobalPart{Ty}).first;
+      if (Ty != It->second.Ty)
         return false;
 
       // Scalable types not currently supported.
@@ -459,21 +463,22 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
   assert(GV->hasLocalLinkage());
 
   // Collect types to split into.
-  DenseMap<uint64_t, Type *> Types;
-  if (!collectSRATypes(Types, GV, DL) || Types.empty())
+  DenseMap<uint64_t, GlobalPart> Parts;
+  if (!collectSRATypes(Parts, GV, DL) || Parts.empty())
     return nullptr;
 
   // Make sure we don't SRA back to the same type.
-  if (Types.size() == 1 && Types.begin()->second == GV->getValueType())
+  if (Parts.size() == 1 && Parts.begin()->second.Ty == GV->getValueType())
     return nullptr;
 
   // Don't perform SRA if we would have to split into many globals.
-  if (Types.size() > 16)
+  if (Parts.size() > 16)
     return nullptr;
 
   // Sort by offset.
   SmallVector<std::pair<uint64_t, Type *>, 16> TypesVector;
-  append_range(TypesVector, Types);
+  for (const auto &Pair : Parts)
+    TypesVector.push_back({Pair.first, Pair.second.Ty});
   sort(TypesVector, llvm::less_first());
 
   // Check that the types are non-overlapping.
@@ -493,7 +498,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
   // Collect initializers for new globals.
   Constant *OrigInit = GV->getInitializer();
   DenseMap<uint64_t, Constant *> Initializers;
-  for (const auto &Pair : Types) {
+  for (const auto &Pair : TypesVector) {
     Constant *NewInit = ConstantFoldLoadFromConst(OrigInit, Pair.second,
                                                   APInt(64, Pair.first), DL);
     if (!NewInit) {


        


More information about the llvm-commits mailing list