From 7d9d855a073a496540d4e40e67945b6f09d3aea7 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 15 Jul 2022 16:36:46 +0200 Subject: [PATCH] [GlobalOpt] Add struct for parts during GlobalSRA (NFC) This is in preparation for https://reviews.llvm.org/D129857. --- llvm/lib/Transforms/IPO/GlobalOpt.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp index 11621d9..435f38f 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 &Types, GlobalValue *GV, - const DataLayout &DL) { +static bool collectSRATypes(DenseMap &Parts, + GlobalValue *GV, const DataLayout &DL) { SmallVector Worklist; SmallPtrSet Visited; auto AppendUses = [&](Value *V) { @@ -373,8 +377,8 @@ static bool collectSRATypes(DenseMap &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 Types; - if (!collectSRATypes(Types, GV, DL) || Types.empty()) + DenseMap 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, 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 Initializers; - for (const auto &Pair : Types) { + for (const auto &Pair : TypesVector) { Constant *NewInit = ConstantFoldLoadFromConst(OrigInit, Pair.second, APInt(64, Pair.first), DL); if (!NewInit) { -- 2.7.4