X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FGlobalMerge.cpp;h=8d31c1cfd617a033fefb13694858943e2546fa93;hb=f60104e676a0a9069e7606c59073932bf383d38a;hp=2157bcbe7d4a4b7fc7fbe5c05a98069c0c2834aa;hpb=d2069333eea1439a607b9ec8e0c305b1d0b78e99;p=oota-llvm.git diff --git a/lib/CodeGen/GlobalMerge.cpp b/lib/CodeGen/GlobalMerge.cpp index 2157bcbe7d4..8d31c1cfd61 100644 --- a/lib/CodeGen/GlobalMerge.cpp +++ b/lib/CodeGen/GlobalMerge.cpp @@ -49,9 +49,19 @@ // str r0, [r5], #4 // // note that we saved 2 registers here almostly "for free". +// +// However, merging globals can have tradeoffs: +// - it confuses debuggers, tools, and users +// - it makes linker optimizations less useful (order files, LOHs, ...) +// - it forces usage of indexed addressing (which isn't necessarily "free") +// - it can increase register pressure when the uses are disparate enough. +// +// We use heuristics to discover the best global grouping we can (cf cl::opts). // ===---------------------------------------------------------------------===// #include "llvm/Transforms/Scalar.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallBitVector.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/CodeGen/Passes.h" @@ -66,9 +76,12 @@ #include "llvm/IR/Module.h" #include "llvm/Pass.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetLowering.h" #include "llvm/Target/TargetLoweringObjectFile.h" #include "llvm/Target/TargetSubtargetInfo.h" +#include using namespace llvm; #define DEBUG_TYPE "global-merge" @@ -79,6 +92,15 @@ EnableGlobalMerge("enable-global-merge", cl::Hidden, cl::desc("Enable the global merge pass"), cl::init(true)); +static cl::opt GlobalMergeGroupByUse( + "global-merge-group-by-use", cl::Hidden, + cl::desc("Improve global merge pass to look at uses"), cl::init(true)); + +static cl::opt GlobalMergeIgnoreSingleUse( + "global-merge-ignore-single-use", cl::Hidden, + cl::desc("Improve global merge pass to ignore globals only used alone"), + cl::init(true)); + static cl::opt EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, cl::desc("Enable global merge pass on constants"), @@ -86,24 +108,36 @@ EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, // FIXME: this could be a transitional option, and we probably need to remove // it if only we are sure this optimization could always benefit all targets. -static cl::opt +static cl::opt EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden, - cl::desc("Enable global merge pass on external linkage"), - cl::init(false)); + cl::desc("Enable global merge pass on external linkage")); STATISTIC(NumMerged, "Number of globals merged"); namespace { class GlobalMerge : public FunctionPass { const TargetMachine *TM; - const DataLayout *DL; // FIXME: Infer the maximum possible offset depending on the actual users // (these max offsets are different for the users inside Thumb or ARM // functions), see the code that passes in the offset in the ARM backend // for more information. unsigned MaxOffset; + /// Whether we should try to optimize for size only. + /// Currently, this applies a dead simple heuristic: only consider globals + /// used in minsize functions for merging. + /// FIXME: This could learn about optsize, and be used in the cost model. + bool OnlyOptimizeForSize; + + /// Whether we should merge global variables that have external linkage. + bool MergeExternalGlobals; + bool doMerge(SmallVectorImpl &Globals, Module &M, bool isConst, unsigned AddrSpace) const; + /// \brief Merge everything in \p Globals for which the corresponding bit + /// in \p GlobalSet is set. + bool doMerge(SmallVectorImpl &Globals, + const BitVector &GlobalSet, Module &M, bool isConst, + unsigned AddrSpace) const; /// \brief Check if the given variable has been identified as must keep /// \pre setMustKeepGlobalVariables must have been called on the Module that @@ -125,9 +159,12 @@ namespace { public: static char ID; // Pass identification, replacement for typeid. explicit GlobalMerge(const TargetMachine *TM = nullptr, - unsigned MaximalOffset = 0) - : FunctionPass(ID), TM(TM), DL(TM->getDataLayout()), - MaxOffset(MaximalOffset) { + unsigned MaximalOffset = 0, + bool OnlyOptimizeForSize = false, + bool MergeExternalGlobals = false) + : FunctionPass(ID), TM(TM), MaxOffset(MaximalOffset), + OnlyOptimizeForSize(OnlyOptimizeForSize), + MergeExternalGlobals(MergeExternalGlobals) { initializeGlobalMergePass(*PassRegistry::getPassRegistry()); } @@ -154,85 +191,279 @@ INITIALIZE_PASS_END(GlobalMerge, "global-merge", "Merge global variables", bool GlobalMerge::doMerge(SmallVectorImpl &Globals, Module &M, bool isConst, unsigned AddrSpace) const { + auto &DL = M.getDataLayout(); // FIXME: Find better heuristics std::stable_sort(Globals.begin(), Globals.end(), - [this](const GlobalVariable *GV1, const GlobalVariable *GV2) { - Type *Ty1 = cast(GV1->getType())->getElementType(); - Type *Ty2 = cast(GV2->getType())->getElementType(); + [&DL](const GlobalVariable *GV1, const GlobalVariable *GV2) { + return DL.getTypeAllocSize(GV1->getValueType()) < + DL.getTypeAllocSize(GV2->getValueType()); + }); + + // If we want to just blindly group all globals together, do so. + if (!GlobalMergeGroupByUse) { + BitVector AllGlobals(Globals.size()); + AllGlobals.set(); + return doMerge(Globals, AllGlobals, M, isConst, AddrSpace); + } + + // If we want to be smarter, look at all uses of each global, to try to + // discover all sets of globals used together, and how many times each of + // these sets occurred. + // + // Keep this reasonably efficient, by having an append-only list of all sets + // discovered so far (UsedGlobalSet), and mapping each "together-ness" unit of + // code (currently, a Function) to the set of globals seen so far that are + // used together in that unit (GlobalUsesByFunction). + // + // When we look at the Nth global, we now that any new set is either: + // - the singleton set {N}, containing this global only, or + // - the union of {N} and a previously-discovered set, containing some + // combination of the previous N-1 globals. + // Using that knowledge, when looking at the Nth global, we can keep: + // - a reference to the singleton set {N} (CurGVOnlySetIdx) + // - a list mapping each previous set to its union with {N} (EncounteredUGS), + // if it actually occurs. + + // We keep track of the sets of globals used together "close enough". + struct UsedGlobalSet { + UsedGlobalSet(size_t Size) : Globals(Size), UsageCount(1) {} + BitVector Globals; + unsigned UsageCount; + }; - return (DL->getTypeAllocSize(Ty1) < DL->getTypeAllocSize(Ty2)); - }); + // Each set is unique in UsedGlobalSets. + std::vector UsedGlobalSets; + + // Avoid repeating the create-global-set pattern. + auto CreateGlobalSet = [&]() -> UsedGlobalSet & { + UsedGlobalSets.emplace_back(Globals.size()); + return UsedGlobalSets.back(); + }; + + // The first set is the empty set. + CreateGlobalSet().UsageCount = 0; + + // We define "close enough" to be "in the same function". + // FIXME: Grouping uses by function is way too aggressive, so we should have + // a better metric for distance between uses. + // The obvious alternative would be to group by BasicBlock, but that's in + // turn too conservative.. + // Anything in between wouldn't be trivial to compute, so just stick with + // per-function grouping. + + // The value type is an index into UsedGlobalSets. + // The default (0) conveniently points to the empty set. + DenseMap GlobalUsesByFunction; + + // Now, look at each merge-eligible global in turn. + + // Keep track of the sets we already encountered to which we added the + // current global. + // Each element matches the same-index element in UsedGlobalSets. + // This lets us efficiently tell whether a set has already been expanded to + // include the current global. + std::vector EncounteredUGS; + + for (size_t GI = 0, GE = Globals.size(); GI != GE; ++GI) { + GlobalVariable *GV = Globals[GI]; + + // Reset the encountered sets for this global... + std::fill(EncounteredUGS.begin(), EncounteredUGS.end(), 0); + // ...and grow it in case we created new sets for the previous global. + EncounteredUGS.resize(UsedGlobalSets.size()); + + // We might need to create a set that only consists of the current global. + // Keep track of its index into UsedGlobalSets. + size_t CurGVOnlySetIdx = 0; + + // For each global, look at all its Uses. + for (auto &U : GV->uses()) { + // This Use might be a ConstantExpr. We're interested in Instruction + // users, so look through ConstantExpr... + Use *UI, *UE; + if (ConstantExpr *CE = dyn_cast(U.getUser())) { + if (CE->use_empty()) + continue; + UI = &*CE->use_begin(); + UE = nullptr; + } else if (isa(U.getUser())) { + UI = &U; + UE = UI->getNext(); + } else { + continue; + } + + // ...to iterate on all the instruction users of the global. + // Note that we iterate on Uses and not on Users to be able to getNext(). + for (; UI != UE; UI = UI->getNext()) { + Instruction *I = dyn_cast(UI->getUser()); + if (!I) + continue; + + Function *ParentFn = I->getParent()->getParent(); + + // If we're only optimizing for size, ignore non-minsize functions. + if (OnlyOptimizeForSize && !ParentFn->optForMinSize()) + continue; + + size_t UGSIdx = GlobalUsesByFunction[ParentFn]; + + // If this is the first global the basic block uses, map it to the set + // consisting of this global only. + if (!UGSIdx) { + // If that set doesn't exist yet, create it. + if (!CurGVOnlySetIdx) { + CurGVOnlySetIdx = UsedGlobalSets.size(); + CreateGlobalSet().Globals.set(GI); + } else { + ++UsedGlobalSets[CurGVOnlySetIdx].UsageCount; + } + + GlobalUsesByFunction[ParentFn] = CurGVOnlySetIdx; + continue; + } + + // If we already encountered this BB, just increment the counter. + if (UsedGlobalSets[UGSIdx].Globals.test(GI)) { + ++UsedGlobalSets[UGSIdx].UsageCount; + continue; + } + + // If not, the previous set wasn't actually used in this function. + --UsedGlobalSets[UGSIdx].UsageCount; + + // If we already expanded the previous set to include this global, just + // reuse that expanded set. + if (size_t ExpandedIdx = EncounteredUGS[UGSIdx]) { + ++UsedGlobalSets[ExpandedIdx].UsageCount; + GlobalUsesByFunction[ParentFn] = ExpandedIdx; + continue; + } + + // If not, create a new set consisting of the union of the previous set + // and this global. Mark it as encountered, so we can reuse it later. + GlobalUsesByFunction[ParentFn] = EncounteredUGS[UGSIdx] = + UsedGlobalSets.size(); + + UsedGlobalSet &NewUGS = CreateGlobalSet(); + NewUGS.Globals.set(GI); + NewUGS.Globals |= UsedGlobalSets[UGSIdx].Globals; + } + } + } + + // Now we found a bunch of sets of globals used together. We accumulated + // the number of times we encountered the sets (i.e., the number of blocks + // that use that exact set of globals). + // + // Multiply that by the size of the set to give us a crude profitability + // metric. + std::sort(UsedGlobalSets.begin(), UsedGlobalSets.end(), + [](const UsedGlobalSet &UGS1, const UsedGlobalSet &UGS2) { + return UGS1.Globals.count() * UGS1.UsageCount < + UGS2.Globals.count() * UGS2.UsageCount; + }); + + // We can choose to merge all globals together, but ignore globals never used + // with another global. This catches the obviously non-profitable cases of + // having a single global, but is aggressive enough for any other case. + if (GlobalMergeIgnoreSingleUse) { + BitVector AllGlobals(Globals.size()); + for (size_t i = 0, e = UsedGlobalSets.size(); i != e; ++i) { + const UsedGlobalSet &UGS = UsedGlobalSets[e - i - 1]; + if (UGS.UsageCount == 0) + continue; + if (UGS.Globals.count() > 1) + AllGlobals |= UGS.Globals; + } + return doMerge(Globals, AllGlobals, M, isConst, AddrSpace); + } + + // Starting from the sets with the best (=biggest) profitability, find a + // good combination. + // The ideal (and expensive) solution can only be found by trying all + // combinations, looking for the one with the best profitability. + // Don't be smart about it, and just pick the first compatible combination, + // starting with the sets with the best profitability. + BitVector PickedGlobals(Globals.size()); + bool Changed = false; + + for (size_t i = 0, e = UsedGlobalSets.size(); i != e; ++i) { + const UsedGlobalSet &UGS = UsedGlobalSets[e - i - 1]; + if (UGS.UsageCount == 0) + continue; + if (PickedGlobals.anyCommon(UGS.Globals)) + continue; + PickedGlobals |= UGS.Globals; + // If the set only contains one global, there's no point in merging. + // Ignore the global for inclusion in other sets though, so keep it in + // PickedGlobals. + if (UGS.Globals.count() < 2) + continue; + Changed |= doMerge(Globals, UGS.Globals, M, isConst, AddrSpace); + } + + return Changed; +} + +bool GlobalMerge::doMerge(SmallVectorImpl &Globals, + const BitVector &GlobalSet, Module &M, bool isConst, + unsigned AddrSpace) const { Type *Int32Ty = Type::getInt32Ty(M.getContext()); + auto &DL = M.getDataLayout(); assert(Globals.size() > 1); - // FIXME: This simple solution merges globals all together as maximum as - // possible. However, with this solution it would be hard to remove dead - // global symbols at link-time. An alternative solution could be checking - // global symbols references function by function, and make the symbols - // being referred in the same function merged and we would probably need - // to introduce heuristic algorithm to solve the merge conflict from - // different functions. - for (size_t i = 0, e = Globals.size(); i != e; ) { - size_t j = 0; + DEBUG(dbgs() << " Trying to merge set, starts with #" + << GlobalSet.find_first() << "\n"); + + ssize_t i = GlobalSet.find_first(); + while (i != -1) { + ssize_t j = 0; uint64_t MergedSize = 0; std::vector Tys; std::vector Inits; - bool HasExternal = false; - GlobalVariable *TheFirstExternal = 0; - for (j = i; j != e; ++j) { - Type *Ty = Globals[j]->getType()->getElementType(); - MergedSize += DL->getTypeAllocSize(Ty); + for (j = i; j != -1; j = GlobalSet.find_next(j)) { + Type *Ty = Globals[j]->getValueType(); + MergedSize += DL.getTypeAllocSize(Ty); if (MergedSize > MaxOffset) { break; } Tys.push_back(Ty); Inits.push_back(Globals[j]->getInitializer()); - - if (Globals[j]->hasExternalLinkage() && !HasExternal) { - HasExternal = true; - TheFirstExternal = Globals[j]; - } } - // If merged variables doesn't have external linkage, we needn't to expose - // the symbol after merging. - GlobalValue::LinkageTypes Linkage = HasExternal - ? GlobalValue::ExternalLinkage - : GlobalValue::InternalLinkage; - StructType *MergedTy = StructType::get(M.getContext(), Tys); Constant *MergedInit = ConstantStruct::get(MergedTy, Inits); - // If merged variables have external linkage, we use symbol name of the - // first variable merged as the suffix of global symbol name. This would - // be able to avoid the link-time naming conflict for globalm symbols. GlobalVariable *MergedGV = new GlobalVariable( - M, MergedTy, isConst, Linkage, MergedInit, - HasExternal ? "_MergedGlobals_" + TheFirstExternal->getName() - : "_MergedGlobals", - nullptr, GlobalVariable::NotThreadLocal, AddrSpace); + M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit, + "_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace); - for (size_t k = i; k < j; ++k) { + for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k)) { GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage(); std::string Name = Globals[k]->getName(); Constant *Idx[2] = { ConstantInt::get(Int32Ty, 0), - ConstantInt::get(Int32Ty, k-i) + ConstantInt::get(Int32Ty, idx++) }; Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedTy, MergedGV, Idx); Globals[k]->replaceAllUsesWith(GEP); Globals[k]->eraseFromParent(); - if (Linkage != GlobalValue::InternalLinkage) { - // Generate a new alias... + // When the linkage is not internal we must emit an alias for the original + // variable name as it may be accessed from another object. On non-Mach-O + // we can also emit an alias for internal linkage as it's safe to do so. + // It's not safe on Mach-O as the alias (and thus the portion of the + // MergedGlobals variable) may be dead stripped at link time. + if (Linkage != GlobalValue::InternalLinkage || + !TM->getTargetTriple().isOSBinFormatMachO()) { auto *PTy = cast(GEP->getType()); - GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), - Linkage, Name, GEP, &M); + GlobalAlias::create(PTy, Linkage, Name, GEP, &M); } NumMerged++; @@ -284,6 +515,7 @@ bool GlobalMerge::doInitialization(Module &M) { if (!EnableGlobalMerge) return false; + auto &DL = M.getDataLayout(); DenseMap > Globals, ConstGlobals, BSSGlobals; bool Changed = false; @@ -296,7 +528,7 @@ bool GlobalMerge::doInitialization(Module &M) { if (I->isDeclaration() || I->isThreadLocal() || I->hasSection()) continue; - if (!(EnableGlobalMergeOnExternal && I->hasExternalLinkage()) && + if (!(MergeExternalGlobals && I->hasExternalLinkage()) && !I->hasInternalLinkage()) continue; @@ -306,9 +538,9 @@ bool GlobalMerge::doInitialization(Module &M) { unsigned AddressSpace = PT->getAddressSpace(); // Ignore fancy-aligned globals for now. - unsigned Alignment = DL->getPreferredAlignment(I); - Type *Ty = I->getType()->getElementType(); - if (Alignment > DL->getABITypeAlignment(Ty)) + unsigned Alignment = DL.getPreferredAlignment(I); + Type *Ty = I->getValueType(); + if (Alignment > DL.getABITypeAlignment(Ty)) continue; // Ignore all 'special' globals. @@ -320,7 +552,7 @@ bool GlobalMerge::doInitialization(Module &M) { if (isMustKeepGlobalVariable(I)) continue; - if (DL->getTypeAllocSize(Ty) < MaxOffset) { + if (DL.getTypeAllocSize(Ty) < MaxOffset) { if (TargetLoweringObjectFile::getKindForGlobal(I, *TM).isBSSLocal()) BSSGlobals[AddressSpace].push_back(I); else if (I->isConstant()) @@ -358,6 +590,10 @@ bool GlobalMerge::doFinalization(Module &M) { return false; } -Pass *llvm::createGlobalMergePass(const TargetMachine *TM, unsigned Offset) { - return new GlobalMerge(TM, Offset); +Pass *llvm::createGlobalMergePass(const TargetMachine *TM, unsigned Offset, + bool OnlyOptimizeForSize, + bool MergeExternalByDefault) { + bool MergeExternal = (EnableGlobalMergeOnExternal == cl::BOU_UNSET) ? + MergeExternalByDefault : (EnableGlobalMergeOnExternal == cl::BOU_TRUE); + return new GlobalMerge(TM, Offset, OnlyOptimizeForSize, MergeExternal); }