ConstantMerge.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the interface to a pass that merges duplicate global
  10. // constants together into a single constant that is shared. This is useful
  11. // because some passes (ie TraceValues) insert a lot of string constants into
  12. // the program, regardless of whether or not an existing string is available.
  13. //
  14. // Algorithm: ConstantMerge is designed to build up a map of available constants
  15. // and eliminate duplicates when it is initialized.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Transforms/IPO/ConstantMerge.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/IR/Constants.h"
  24. #include "llvm/IR/DataLayout.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/GlobalValue.h"
  27. #include "llvm/IR/GlobalVariable.h"
  28. #include "llvm/IR/LLVMContext.h"
  29. #include "llvm/IR/Module.h"
  30. #include "llvm/InitializePasses.h"
  31. #include "llvm/Pass.h"
  32. #include "llvm/Support/Casting.h"
  33. #include "llvm/Transforms/IPO.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <utility>
  37. using namespace llvm;
  38. #define DEBUG_TYPE "constmerge"
  39. STATISTIC(NumIdenticalMerged, "Number of identical global constants merged");
  40. /// Find values that are marked as llvm.used.
  41. static void FindUsedValues(GlobalVariable *LLVMUsed,
  42. SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
  43. if (!LLVMUsed) return;
  44. ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
  45. for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
  46. Value *Operand = Inits->getOperand(i)->stripPointerCasts();
  47. GlobalValue *GV = cast<GlobalValue>(Operand);
  48. UsedValues.insert(GV);
  49. }
  50. }
  51. // True if A is better than B.
  52. static bool IsBetterCanonical(const GlobalVariable &A,
  53. const GlobalVariable &B) {
  54. if (!A.hasLocalLinkage() && B.hasLocalLinkage())
  55. return true;
  56. if (A.hasLocalLinkage() && !B.hasLocalLinkage())
  57. return false;
  58. return A.hasGlobalUnnamedAddr();
  59. }
  60. static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV) {
  61. SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  62. GV->getAllMetadata(MDs);
  63. for (const auto &V : MDs)
  64. if (V.first != LLVMContext::MD_dbg)
  65. return true;
  66. return false;
  67. }
  68. static void copyDebugLocMetadata(const GlobalVariable *From,
  69. GlobalVariable *To) {
  70. SmallVector<DIGlobalVariableExpression *, 1> MDs;
  71. From->getDebugInfo(MDs);
  72. for (auto *MD : MDs)
  73. To->addDebugInfo(MD);
  74. }
  75. static Align getAlign(GlobalVariable *GV) {
  76. return GV->getAlign().value_or(
  77. GV->getParent()->getDataLayout().getPreferredAlign(GV));
  78. }
  79. static bool
  80. isUnmergeableGlobal(GlobalVariable *GV,
  81. const SmallPtrSetImpl<const GlobalValue *> &UsedGlobals) {
  82. // Only process constants with initializers in the default address space.
  83. return !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
  84. GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
  85. // Don't touch thread-local variables.
  86. GV->isThreadLocal() ||
  87. // Don't touch values marked with attribute(used).
  88. UsedGlobals.count(GV);
  89. }
  90. enum class CanMerge { No, Yes };
  91. static CanMerge makeMergeable(GlobalVariable *Old, GlobalVariable *New) {
  92. if (!Old->hasGlobalUnnamedAddr() && !New->hasGlobalUnnamedAddr())
  93. return CanMerge::No;
  94. if (hasMetadataOtherThanDebugLoc(Old))
  95. return CanMerge::No;
  96. assert(!hasMetadataOtherThanDebugLoc(New));
  97. if (!Old->hasGlobalUnnamedAddr())
  98. New->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
  99. return CanMerge::Yes;
  100. }
  101. static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New) {
  102. Constant *NewConstant = New;
  103. LLVM_DEBUG(dbgs() << "Replacing global: @" << Old->getName() << " -> @"
  104. << New->getName() << "\n");
  105. // Bump the alignment if necessary.
  106. if (Old->getAlign() || New->getAlign())
  107. New->setAlignment(std::max(getAlign(Old), getAlign(New)));
  108. copyDebugLocMetadata(Old, New);
  109. Old->replaceAllUsesWith(NewConstant);
  110. // Delete the global value from the module.
  111. assert(Old->hasLocalLinkage() &&
  112. "Refusing to delete an externally visible global variable.");
  113. Old->eraseFromParent();
  114. }
  115. static bool mergeConstants(Module &M) {
  116. // Find all the globals that are marked "used". These cannot be merged.
  117. SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
  118. FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
  119. FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
  120. // Map unique constants to globals.
  121. DenseMap<Constant *, GlobalVariable *> CMap;
  122. SmallVector<std::pair<GlobalVariable *, GlobalVariable *>, 32>
  123. SameContentReplacements;
  124. size_t ChangesMade = 0;
  125. size_t OldChangesMade = 0;
  126. // Iterate constant merging while we are still making progress. Merging two
  127. // constants together may allow us to merge other constants together if the
  128. // second level constants have initializers which point to the globals that
  129. // were just merged.
  130. while (true) {
  131. // Find the canonical constants others will be merged with.
  132. for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
  133. // If this GV is dead, remove it.
  134. GV.removeDeadConstantUsers();
  135. if (GV.use_empty() && GV.hasLocalLinkage()) {
  136. GV.eraseFromParent();
  137. ++ChangesMade;
  138. continue;
  139. }
  140. if (isUnmergeableGlobal(&GV, UsedGlobals))
  141. continue;
  142. // This transformation is legal for weak ODR globals in the sense it
  143. // doesn't change semantics, but we really don't want to perform it
  144. // anyway; it's likely to pessimize code generation, and some tools
  145. // (like the Darwin linker in cases involving CFString) don't expect it.
  146. if (GV.isWeakForLinker())
  147. continue;
  148. // Don't touch globals with metadata other then !dbg.
  149. if (hasMetadataOtherThanDebugLoc(&GV))
  150. continue;
  151. Constant *Init = GV.getInitializer();
  152. // Check to see if the initializer is already known.
  153. GlobalVariable *&Slot = CMap[Init];
  154. // If this is the first constant we find or if the old one is local,
  155. // replace with the current one. If the current is externally visible
  156. // it cannot be replace, but can be the canonical constant we merge with.
  157. bool FirstConstantFound = !Slot;
  158. if (FirstConstantFound || IsBetterCanonical(GV, *Slot)) {
  159. Slot = &GV;
  160. LLVM_DEBUG(dbgs() << "Cmap[" << *Init << "] = " << GV.getName()
  161. << (FirstConstantFound ? "\n" : " (updated)\n"));
  162. }
  163. }
  164. // Identify all globals that can be merged together, filling in the
  165. // SameContentReplacements vector. We cannot do the replacement in this pass
  166. // because doing so may cause initializers of other globals to be rewritten,
  167. // invalidating the Constant* pointers in CMap.
  168. for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
  169. if (isUnmergeableGlobal(&GV, UsedGlobals))
  170. continue;
  171. // We can only replace constant with local linkage.
  172. if (!GV.hasLocalLinkage())
  173. continue;
  174. Constant *Init = GV.getInitializer();
  175. // Check to see if the initializer is already known.
  176. auto Found = CMap.find(Init);
  177. if (Found == CMap.end())
  178. continue;
  179. GlobalVariable *Slot = Found->second;
  180. if (Slot == &GV)
  181. continue;
  182. if (makeMergeable(&GV, Slot) == CanMerge::No)
  183. continue;
  184. // Make all uses of the duplicate constant use the canonical version.
  185. LLVM_DEBUG(dbgs() << "Will replace: @" << GV.getName() << " -> @"
  186. << Slot->getName() << "\n");
  187. SameContentReplacements.push_back(std::make_pair(&GV, Slot));
  188. }
  189. // Now that we have figured out which replacements must be made, do them all
  190. // now. This avoid invalidating the pointers in CMap, which are unneeded
  191. // now.
  192. for (unsigned i = 0, e = SameContentReplacements.size(); i != e; ++i) {
  193. GlobalVariable *Old = SameContentReplacements[i].first;
  194. GlobalVariable *New = SameContentReplacements[i].second;
  195. replace(M, Old, New);
  196. ++ChangesMade;
  197. ++NumIdenticalMerged;
  198. }
  199. if (ChangesMade == OldChangesMade)
  200. break;
  201. OldChangesMade = ChangesMade;
  202. SameContentReplacements.clear();
  203. CMap.clear();
  204. }
  205. return ChangesMade;
  206. }
  207. PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
  208. if (!mergeConstants(M))
  209. return PreservedAnalyses::all();
  210. return PreservedAnalyses::none();
  211. }
  212. namespace {
  213. struct ConstantMergeLegacyPass : public ModulePass {
  214. static char ID; // Pass identification, replacement for typeid
  215. ConstantMergeLegacyPass() : ModulePass(ID) {
  216. initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
  217. }
  218. // For this pass, process all of the globals in the module, eliminating
  219. // duplicate constants.
  220. bool runOnModule(Module &M) override {
  221. if (skipModule(M))
  222. return false;
  223. return mergeConstants(M);
  224. }
  225. };
  226. } // end anonymous namespace
  227. char ConstantMergeLegacyPass::ID = 0;
  228. INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
  229. "Merge Duplicate Global Constants", false, false)
  230. ModulePass *llvm::createConstantMergePass() {
  231. return new ConstantMergeLegacyPass();
  232. }