2003-04-18 12:34:29 +08:00
|
|
|
//===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-19 04:05:37 +08:00
|
|
|
//
|
|
|
|
// This file defines the interface to a pass that merges duplicate global
|
|
|
|
// constants together into a single constant that is shared. This is useful
|
|
|
|
// because some passes (ie TraceValues) insert a lot of string constants into
|
2002-09-24 07:00:46 +08:00
|
|
|
// the program, regardless of whether or not an existing string is available.
|
2001-10-19 04:05:37 +08:00
|
|
|
//
|
|
|
|
// Algorithm: ConstantMerge is designed to build up a map of available constants
|
2002-09-24 07:00:46 +08:00
|
|
|
// and eliminate duplicates when it is initialized.
|
2001-10-19 04:05:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-05-05 08:51:09 +08:00
|
|
|
#include "llvm/Transforms/IPO/ConstantMerge.h"
|
2010-02-13 02:17:23 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2010-09-15 08:30:11 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-05-05 08:51:09 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2017-10-11 06:49:55 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <utility>
|
|
|
|
|
2003-11-22 05:54:22 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "constmerge"
|
|
|
|
|
2006-12-20 06:09:18 +08:00
|
|
|
STATISTIC(NumMerged, "Number of global constants merged");
|
2002-10-10 07:16:04 +08:00
|
|
|
|
2010-09-15 08:30:11 +08:00
|
|
|
/// Find values that are marked as llvm.used.
|
|
|
|
static void FindUsedValues(GlobalVariable *LLVMUsed,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!LLVMUsed) return;
|
2013-04-22 22:58:02 +08:00
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
|
2013-05-06 09:48:55 +08:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
|
|
|
|
Value *Operand = Inits->getOperand(i)->stripPointerCastsNoFollowAliases();
|
|
|
|
GlobalValue *GV = cast<GlobalValue>(Operand);
|
|
|
|
UsedValues.insert(GV);
|
|
|
|
}
|
2010-09-15 08:30:11 +08:00
|
|
|
}
|
|
|
|
|
2011-01-17 01:05:09 +08:00
|
|
|
// True if A is better than B.
|
2014-01-25 01:20:08 +08:00
|
|
|
static bool IsBetterCanonical(const GlobalVariable &A,
|
|
|
|
const GlobalVariable &B) {
|
2011-01-17 01:05:09 +08:00
|
|
|
if (!A.hasLocalLinkage() && B.hasLocalLinkage())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (A.hasLocalLinkage() && !B.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
2016-06-15 05:01:22 +08:00
|
|
|
return A.hasGlobalUnnamedAddr();
|
2011-01-17 01:05:09 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:03:37 +08:00
|
|
|
static bool hasMetadataOtherThanDebugLoc(const GlobalVariable *GV) {
|
|
|
|
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
|
|
|
|
GV->getAllMetadata(MDs);
|
|
|
|
for (const auto &V : MDs)
|
|
|
|
if (V.first != LLVMContext::MD_dbg)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copyDebugLocMetadata(const GlobalVariable *From,
|
|
|
|
GlobalVariable *To) {
|
|
|
|
SmallVector<DIGlobalVariableExpression *, 1> MDs;
|
|
|
|
From->getDebugInfo(MDs);
|
|
|
|
for (auto MD : MDs)
|
|
|
|
To->addDebugInfo(MD);
|
|
|
|
}
|
|
|
|
|
2016-05-04 11:21:20 +08:00
|
|
|
static unsigned getAlignment(GlobalVariable *GV) {
|
2013-11-13 04:21:43 +08:00
|
|
|
unsigned Align = GV->getAlignment();
|
|
|
|
if (Align)
|
|
|
|
return Align;
|
2015-03-10 10:37:25 +08:00
|
|
|
return GV->getParent()->getDataLayout().getPreferredAlignment(GV);
|
2011-07-28 03:47:34 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 08:51:09 +08:00
|
|
|
static bool mergeConstants(Module &M) {
|
2010-09-15 08:30:11 +08:00
|
|
|
// Find all the globals that are marked "used". These cannot be merged.
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> UsedGlobals;
|
|
|
|
FindUsedValues(M.getGlobalVariable("llvm.used"), UsedGlobals);
|
|
|
|
FindUsedValues(M.getGlobalVariable("llvm.compiler.used"), UsedGlobals);
|
2015-03-10 10:37:25 +08:00
|
|
|
|
|
|
|
// Map unique constants to globals.
|
|
|
|
DenseMap<Constant *, GlobalVariable *> CMap;
|
2003-12-23 07:49:36 +08:00
|
|
|
|
|
|
|
// Replacements - This vector contains a list of replacements to perform.
|
2010-02-13 02:17:23 +08:00
|
|
|
SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
|
2003-12-23 07:49:36 +08:00
|
|
|
|
2003-12-28 15:19:08 +08:00
|
|
|
bool MadeChange = false;
|
2001-10-19 04:05:37 +08:00
|
|
|
|
2003-12-28 15:19:08 +08:00
|
|
|
// Iterate constant merging while we are still making progress. Merging two
|
|
|
|
// constants together may allow us to merge other constants together if the
|
|
|
|
// second level constants have initializers which point to the globals that
|
|
|
|
// were just merged.
|
2017-10-11 06:49:55 +08:00
|
|
|
while (true) {
|
2011-01-17 01:05:09 +08:00
|
|
|
// First: Find the canonical constants others will be merged with.
|
2007-04-15 02:06:52 +08:00
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
2015-10-14 01:51:03 +08:00
|
|
|
GlobalVariable *GV = &*GVI++;
|
2011-01-17 01:05:09 +08:00
|
|
|
|
2007-04-14 09:11:54 +08:00
|
|
|
// If this GV is dead, remove it.
|
|
|
|
GV->removeDeadConstantUsers();
|
2009-01-16 04:18:42 +08:00
|
|
|
if (GV->use_empty() && GV->hasLocalLinkage()) {
|
2007-04-15 01:18:29 +08:00
|
|
|
GV->eraseFromParent();
|
|
|
|
continue;
|
2007-04-14 09:11:54 +08:00
|
|
|
}
|
2011-01-17 01:05:09 +08:00
|
|
|
|
2011-01-16 02:14:21 +08:00
|
|
|
// Only process constants with initializers in the default address space.
|
2011-01-16 02:42:52 +08:00
|
|
|
if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
|
2011-01-16 02:14:21 +08:00
|
|
|
GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
|
2010-09-15 08:30:11 +08:00
|
|
|
// Don't touch values marked with attribute(used).
|
|
|
|
UsedGlobals.count(GV))
|
2010-02-13 02:17:23 +08:00
|
|
|
continue;
|
2011-01-17 01:05:09 +08:00
|
|
|
|
2012-01-12 06:06:46 +08:00
|
|
|
// This transformation is legal for weak ODR globals in the sense it
|
|
|
|
// doesn't change semantics, but we really don't want to perform it
|
|
|
|
// anyway; it's likely to pessimize code generation, and some tools
|
|
|
|
// (like the Darwin linker in cases involving CFString) don't expect it.
|
|
|
|
if (GV->isWeakForLinker())
|
2012-01-11 08:13:08 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-09 08:03:37 +08:00
|
|
|
// Don't touch globals with metadata other then !dbg.
|
|
|
|
if (hasMetadataOtherThanDebugLoc(GV))
|
|
|
|
continue;
|
|
|
|
|
2010-02-13 02:17:23 +08:00
|
|
|
Constant *Init = GV->getInitializer();
|
|
|
|
|
|
|
|
// Check to see if the initializer is already known.
|
2015-03-10 10:37:25 +08:00
|
|
|
GlobalVariable *&Slot = CMap[Init];
|
2010-02-13 02:17:23 +08:00
|
|
|
|
2012-01-11 08:13:08 +08:00
|
|
|
// If this is the first constant we find or if the old one is local,
|
|
|
|
// replace with the current one. If the current is externally visible
|
2011-01-17 01:05:09 +08:00
|
|
|
// it cannot be replace, but can be the canonical constant we merge with.
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!Slot || IsBetterCanonical(*GV, *Slot))
|
2010-02-13 02:17:23 +08:00
|
|
|
Slot = GV;
|
2011-01-16 02:14:21 +08:00
|
|
|
}
|
|
|
|
|
2011-01-17 01:05:09 +08:00
|
|
|
// Second: identify all globals that can be merged together, filling in
|
|
|
|
// the Replacements vector. We cannot do the replacement in this pass
|
|
|
|
// because doing so may cause initializers of other globals to be rewritten,
|
|
|
|
// invalidating the Constant* pointers in CMap.
|
2011-01-16 02:14:21 +08:00
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
2015-10-14 01:51:03 +08:00
|
|
|
GlobalVariable *GV = &*GVI++;
|
2011-01-16 02:14:21 +08:00
|
|
|
|
|
|
|
// Only process constants with initializers in the default address space.
|
2011-01-16 02:42:52 +08:00
|
|
|
if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
|
2011-01-16 02:14:21 +08:00
|
|
|
GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
|
|
|
|
// Don't touch values marked with attribute(used).
|
|
|
|
UsedGlobals.count(GV))
|
|
|
|
continue;
|
|
|
|
|
2012-01-12 06:06:46 +08:00
|
|
|
// We can only replace constant with local linkage.
|
|
|
|
if (!GV->hasLocalLinkage())
|
2011-01-16 02:14:21 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Constant *Init = GV->getInitializer();
|
|
|
|
|
|
|
|
// Check to see if the initializer is already known.
|
2018-08-09 12:17:48 +08:00
|
|
|
auto Found = CMap.find(Init);
|
|
|
|
if (Found == CMap.end())
|
|
|
|
continue;
|
2011-01-16 02:14:21 +08:00
|
|
|
|
2018-08-09 12:17:48 +08:00
|
|
|
GlobalVariable *Slot = Found->second;
|
|
|
|
if (Slot == GV)
|
2011-01-17 01:05:09 +08:00
|
|
|
continue;
|
|
|
|
|
2016-06-15 05:01:22 +08:00
|
|
|
if (!Slot->hasGlobalUnnamedAddr() && !GV->hasGlobalUnnamedAddr())
|
2011-01-17 01:05:09 +08:00
|
|
|
continue;
|
|
|
|
|
2017-03-09 08:03:37 +08:00
|
|
|
if (hasMetadataOtherThanDebugLoc(GV))
|
|
|
|
continue;
|
|
|
|
|
2016-06-15 05:01:22 +08:00
|
|
|
if (!GV->hasGlobalUnnamedAddr())
|
|
|
|
Slot->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
|
2011-01-17 01:05:09 +08:00
|
|
|
|
|
|
|
// Make all uses of the duplicate constant use the canonical version.
|
|
|
|
Replacements.push_back(std::make_pair(GV, Slot));
|
2007-04-14 09:11:54 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2003-12-28 15:19:08 +08:00
|
|
|
if (Replacements.empty())
|
|
|
|
return MadeChange;
|
|
|
|
CMap.clear();
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2003-12-28 15:19:08 +08:00
|
|
|
// Now that we have figured out which replacements must be made, do them all
|
|
|
|
// now. This avoid invalidating the pointers in CMap, which are unneeded
|
|
|
|
// now.
|
|
|
|
for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
|
2011-07-28 03:47:34 +08:00
|
|
|
// Bump the alignment if necessary.
|
|
|
|
if (Replacements[i].first->getAlignment() ||
|
|
|
|
Replacements[i].second->getAlignment()) {
|
2013-11-13 04:21:43 +08:00
|
|
|
Replacements[i].second->setAlignment(
|
|
|
|
std::max(getAlignment(Replacements[i].first),
|
|
|
|
getAlignment(Replacements[i].second)));
|
2011-07-28 03:47:34 +08:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:03:37 +08:00
|
|
|
copyDebugLocMetadata(Replacements[i].first, Replacements[i].second);
|
|
|
|
|
2010-02-13 02:17:23 +08:00
|
|
|
// Eliminate any uses of the dead global.
|
2003-12-28 15:19:08 +08:00
|
|
|
Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2010-02-13 02:17:23 +08:00
|
|
|
// Delete the global value from the module.
|
2011-01-16 02:14:21 +08:00
|
|
|
assert(Replacements[i].first->hasLocalLinkage() &&
|
|
|
|
"Refusing to delete an externally visible global variable.");
|
2010-02-13 02:17:23 +08:00
|
|
|
Replacements[i].first->eraseFromParent();
|
2018-08-10 05:36:57 +08:00
|
|
|
MadeChange = true;
|
2001-10-19 04:05:37 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2003-12-28 15:19:08 +08:00
|
|
|
NumMerged += Replacements.size();
|
|
|
|
Replacements.clear();
|
2003-12-23 07:49:36 +08:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 08:51:09 +08:00
|
|
|
|
2016-06-17 08:11:01 +08:00
|
|
|
PreservedAnalyses ConstantMergePass::run(Module &M, ModuleAnalysisManager &) {
|
2016-05-05 08:51:09 +08:00
|
|
|
if (!mergeConstants(M))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2017-10-11 06:49:55 +08:00
|
|
|
|
2016-05-05 08:51:09 +08:00
|
|
|
struct ConstantMergeLegacyPass : public ModulePass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2017-10-11 06:49:55 +08:00
|
|
|
|
2016-05-05 08:51:09 +08:00
|
|
|
ConstantMergeLegacyPass() : ModulePass(ID) {
|
|
|
|
initializeConstantMergeLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
// For this pass, process all of the globals in the module, eliminating
|
|
|
|
// duplicate constants.
|
2017-10-11 06:49:55 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
2016-05-05 08:51:09 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
return mergeConstants(M);
|
|
|
|
}
|
|
|
|
};
|
2017-10-11 06:49:55 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2016-05-05 08:51:09 +08:00
|
|
|
|
|
|
|
char ConstantMergeLegacyPass::ID = 0;
|
2017-10-11 06:49:55 +08:00
|
|
|
|
2016-05-05 08:51:09 +08:00
|
|
|
INITIALIZE_PASS(ConstantMergeLegacyPass, "constmerge",
|
|
|
|
"Merge Duplicate Global Constants", false, false)
|
|
|
|
|
|
|
|
ModulePass *llvm::createConstantMergePass() {
|
|
|
|
return new ConstantMergeLegacyPass();
|
|
|
|
}
|