Lines Matching refs:Globals
1 //===- GlobalMerge.cpp - Internal globals merging -------------------------===//
10 // This pass merges globals with internal linkage into one. This way all the
11 // globals which were merged into a biggest one can be addressed using offsets
14 // when many globals are involved.
54 // However, merging globals can have tradeoffs:
121 cl::desc("Improve global merge pass to ignore globals only used alone"),
135 STATISTIC(NumMerged, "Number of globals merged");
149 /// Currently, this applies a dead simple heuristic: only consider globals
159 bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
162 /// Merge everything in \p Globals for which the corresponding bit
164 bool doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
205 StringRef getPassName() const override { return "Merge internal globals"; }
219 bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals,
223 std::stable_sort(Globals.begin(), Globals.end(),
229 // If we want to just blindly group all globals together, do so.
231 BitVector AllGlobals(Globals.size());
233 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
237 // discover all sets of globals used together, and how many times each of
242 // code (currently, a Function) to the set of globals seen so far that are
248 // combination of the previous N-1 globals.
254 // We keep track of the sets of globals used together "close enough".
256 BitVector Globals;
259 UsedGlobalSet(size_t Size) : Globals(Size) {}
267 UsedGlobalSets.emplace_back(Globals.size());
295 for (size_t GI = 0, GE = Globals.size(); GI != GE; ++GI) {
296 GlobalVariable *GV = Globals[GI];
345 CreateGlobalSet().Globals.set(GI);
355 if (UsedGlobalSets[UGSIdx].Globals.test(GI)) {
377 NewUGS.Globals.set(GI);
378 NewUGS.Globals |= UsedGlobalSets[UGSIdx].Globals;
383 // Now we found a bunch of sets of globals used together. We accumulated
385 // that use that exact set of globals).
391 return UGS1.Globals.count() * UGS1.UsageCount <
392 UGS2.Globals.count() * UGS2.UsageCount;
395 // We can choose to merge all globals together, but ignore globals never used
399 BitVector AllGlobals(Globals.size());
404 if (UGS.Globals.count() > 1)
405 AllGlobals |= UGS.Globals;
407 return doMerge(Globals, AllGlobals, M, isConst, AddrSpace);
416 BitVector PickedGlobals(Globals.size());
423 if (PickedGlobals.anyCommon(UGS.Globals))
425 PickedGlobals |= UGS.Globals;
429 if (UGS.Globals.count() < 2)
431 Changed |= doMerge(Globals, UGS.Globals, M, isConst, AddrSpace);
437 bool GlobalMerge::doMerge(const SmallVectorImpl<GlobalVariable *> &Globals,
440 assert(Globals.size() > 1);
463 Type *Ty = Globals[j]->getValueType();
464 unsigned Align = DL.getPreferredAlignment(Globals[j]);
477 Inits.push_back(Globals[j]->getInitializer());
482 if (Globals[j]->hasExternalLinkage() && !HasExternal) {
484 FirstExternalName = Globals[j]->getName();
522 GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
523 std::string Name = Globals[k]->getName();
525 Globals[k]->getDLLStorageClass();
529 MergedGV->copyMetadata(Globals[k],
538 Globals[k]->replaceAllUsesWith(GEP);
539 Globals[k]->eraseFromParent();
585 // Keep globals used by landingpads and catchpads.
602 DenseMap<unsigned, SmallVector<GlobalVariable *, 16>> Globals, ConstGlobals,
607 // Grab all non-const globals.
608 for (auto &GV : M.globals()) {
609 // Merge is safe for "normal" internal or external globals only
614 // It's not safe to merge globals that may be preempted
627 // Ignore all 'special' globals.
632 // Ignore all "required" globals:
644 Globals[AddressSpace].push_back(&GV);
648 for (auto &P : Globals)