Lines Matching full:globals
1 //===- GlobalMerge.cpp - Internal globals merging -------------------------===//
9 // This pass merges globals with internal linkage into one. This way all the
10 // globals which were merged into a biggest one can be addressed using offsets
13 // when many globals are involved.
53 // However, merging globals can have tradeoffs:
125 cl::desc("Merge all const globals without looking at uses"),
130 cl::desc("Improve global merge pass to ignore globals only used alone"),
150 STATISTIC(NumMerged, "Number of globals merged");
160 bool doMerge(SmallVectorImpl<GlobalVariable *> &Globals, Module &M,
163 /// Merge everything in \p Globals for which the corresponding bit
165 bool doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
235 StringRef getPassName() const override { return "Merge internal globals"; }
260 bool GlobalMergeImpl::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
266 Globals, [&DL](const GlobalVariable *GV1, const GlobalVariable *GV2) {
272 // If we want to just blindly group all globals together, do so.
274 BitVector AllGlobals(Globals.size(), true);
275 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
279 // discover all sets of globals used together, and how many times each of
284 // code (currently, a Function) to the set of globals seen so far that are
290 // combination of the previous N-1 globals.
296 // We keep track of the sets of globals used together "close enough".
298 BitVector Globals;
301 UsedGlobalSet(size_t Size) : Globals(Size) {}
309 UsedGlobalSets.emplace_back(Globals.size());
337 for (size_t GI = 0, GE = Globals.size(); GI != GE; ++GI) {
338 GlobalVariable *GV = Globals[GI];
386 CreateGlobalSet().Globals.set(GI);
397 if (UsedGlobalSets[UGSIdx].Globals.test(GI)) {
419 NewUGS.Globals.set(GI);
420 NewUGS.Globals |= UsedGlobalSets[UGSIdx].Globals;
425 // We can choose to merge all globals together, but ignore globals never used
429 BitVector AllGlobals(Globals.size());
433 if (UGS.Globals.count() > 1)
434 AllGlobals |= UGS.Globals;
436 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
439 // Now we found a bunch of sets of globals used together. We accumulated
441 // that use that exact set of globals).
447 return UGS1.Globals.count() * UGS1.UsageCount <
448 UGS2.Globals.count() * UGS2.UsageCount;
457 BitVector PickedGlobals(Globals.size());
463 if (PickedGlobals.anyCommon(UGS.Globals))
465 PickedGlobals |= UGS.Globals;
469 if (UGS.Globals.count() < 2)
471 Changed |= doMerge(Globals, UGS.Globals, M, isConst, AddrSpace);
477 bool GlobalMergeImpl::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
480 assert(Globals.size() > 1);
487 << GlobalSet.find_first() << ", total of " << Globals.size()
504 Type *Ty = Globals[j]->getValueType();
507 Align Alignment = DL.getPreferredAlign(Globals[j]);
520 Inits.push_back(Globals[j]->getInitializer());
525 if (Globals[j]->hasExternalLinkage() && !HasExternal) {
527 FirstExternalName = Globals[j]->getName();
562 MergedGV->setSection(Globals[i]->getSection());
568 GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
569 std::string Name(Globals[k]->getName());
570 GlobalValue::VisibilityTypes Visibility = Globals[k]->getVisibility();
572 Globals[k]->getDLLStorageClass();
576 MergedGV->copyMetadata(Globals[k],
585 Globals[k]->replaceAllUsesWith(GEP);
586 Globals[k]->eraseFromParent();
641 // Keep globals used by landingpads, catchpads,
679 Globals, ConstGlobals, BSSGlobals;
689 // Grab all non-const globals.
690 for (auto &GV : M.globals()) {
691 // Merge is safe for "normal" internal or external globals only
695 // It's not safe to merge globals that may be preempted
713 // Ignore all 'special' globals.
717 // Ignore all "required" globals:
721 // Don't merge tagged globals, as each global should have its own unique
722 // memory tag at runtime. TODO(hctim): This can be relaxed: constant globals
724 // the globals occupy the same number of tag granules (i.e. `size_a / 16 ==
739 Globals[{AddressSpace, Section}].push_back(&GV);
745 for (auto &P : Globals)