2003-04-18 06:34:29 +02:00
|
|
|
//===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-18 22:05:37 +02: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 01:00:46 +02:00
|
|
|
// the program, regardless of whether or not an existing string is available.
|
2001-10-18 22:05:37 +02:00
|
|
|
//
|
|
|
|
// Algorithm: ConstantMerge is designed to build up a map of available constants
|
2002-09-24 01:00:46 +02:00
|
|
|
// and eliminate duplicates when it is initialized.
|
2001-10-18 22:05:37 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
#define DEBUG_TYPE "constmerge"
|
2002-07-23 21:57:40 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2010-09-15 02:30:11 +02:00
|
|
|
#include "llvm/Constants.h"
|
2010-02-12 19:17:23 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-01-31 01:45:11 +01:00
|
|
|
#include "llvm/Module.h"
|
2002-02-26 22:46:54 +01:00
|
|
|
#include "llvm/Pass.h"
|
2010-02-12 19:17:23 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2010-09-15 02:30:11 +02:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-21 22:54:22 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
STATISTIC(NumMerged, "Number of global constants merged");
|
2002-10-10 01:16:04 +02:00
|
|
|
|
2006-12-19 23:09:18 +01:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct ConstantMerge : public ModulePass {
|
2007-05-06 15:37:16 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 19:21:58 +02:00
|
|
|
ConstantMerge() : ModulePass(ID) {
|
|
|
|
initializeConstantMergePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2002-06-25 17:55:29 +02:00
|
|
|
// run - For this pass, process all of the globals in the module,
|
|
|
|
// eliminating duplicate constants.
|
|
|
|
//
|
2004-09-20 06:48:05 +02:00
|
|
|
bool runOnModule(Module &M);
|
2002-06-25 17:55:29 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
char ConstantMerge::ID = 0;
|
2010-07-22 00:09:45 +02:00
|
|
|
INITIALIZE_PASS(ConstantMerge, "constmerge",
|
2010-10-08 00:25:06 +02:00
|
|
|
"Merge Duplicate Global Constants", false, false)
|
2008-05-13 02:00:25 +02:00
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
|
2002-06-25 17:55:29 +02:00
|
|
|
|
2010-09-15 02:30:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
/// Find values that are marked as llvm.used.
|
|
|
|
static void FindUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
if (Inits == 0) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
|
|
|
if (GlobalValue *GV =
|
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
|
|
|
UsedValues.insert(GV);
|
|
|
|
}
|
|
|
|
|
2011-01-16 18:05:09 +01:00
|
|
|
// True if A is better than B.
|
|
|
|
static bool IsBetterCannonical(const GlobalVariable &A,
|
|
|
|
const GlobalVariable &B) {
|
|
|
|
if (!A.hasLocalLinkage() && B.hasLocalLinkage())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (A.hasLocalLinkage() && !B.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return A.hasUnnamedAddr();
|
|
|
|
}
|
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
bool ConstantMerge::runOnModule(Module &M) {
|
2010-09-15 02:30:11 +02: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);
|
|
|
|
|
2006-03-07 18:56:59 +01:00
|
|
|
// Map unique constant/section pairs to globals. We don't want to merge
|
|
|
|
// globals in different sections.
|
2010-02-12 19:17:23 +01:00
|
|
|
DenseMap<Constant*, GlobalVariable*> CMap;
|
2003-12-23 00:49:36 +01:00
|
|
|
|
|
|
|
// Replacements - This vector contains a list of replacements to perform.
|
2010-02-12 19:17:23 +01:00
|
|
|
SmallVector<std::pair<GlobalVariable*, GlobalVariable*>, 32> Replacements;
|
2003-12-23 00:49:36 +01:00
|
|
|
|
2003-12-28 08:19:08 +01:00
|
|
|
bool MadeChange = false;
|
2001-10-18 22:05:37 +02:00
|
|
|
|
2003-12-28 08:19:08 +01: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.
|
|
|
|
while (1) {
|
2011-01-16 18:05:09 +01:00
|
|
|
|
|
|
|
// First: Find the canonical constants others will be merged with.
|
2007-04-14 20:06:52 +02:00
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
|
|
|
GlobalVariable *GV = GVI++;
|
2011-01-16 18:05:09 +01:00
|
|
|
|
2007-04-14 03:11:54 +02:00
|
|
|
// If this GV is dead, remove it.
|
|
|
|
GV->removeDeadConstantUsers();
|
2009-01-15 21:18:42 +01:00
|
|
|
if (GV->use_empty() && GV->hasLocalLinkage()) {
|
2007-04-14 19:18:29 +02:00
|
|
|
GV->eraseFromParent();
|
|
|
|
continue;
|
2007-04-14 03:11:54 +02:00
|
|
|
}
|
2011-01-16 18:05:09 +01:00
|
|
|
|
2011-01-15 19:14:21 +01:00
|
|
|
// Only process constants with initializers in the default address space.
|
2011-01-15 19:42:52 +01:00
|
|
|
if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
|
2011-01-15 19:14:21 +01:00
|
|
|
GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
|
2010-09-15 02:30:11 +02:00
|
|
|
// Don't touch values marked with attribute(used).
|
|
|
|
UsedGlobals.count(GV))
|
2010-02-12 19:17:23 +01:00
|
|
|
continue;
|
2011-01-16 18:05:09 +01:00
|
|
|
|
2010-02-12 19:17:23 +01:00
|
|
|
Constant *Init = GV->getInitializer();
|
|
|
|
|
|
|
|
// Check to see if the initializer is already known.
|
|
|
|
GlobalVariable *&Slot = CMap[Init];
|
|
|
|
|
2011-01-16 18:05:09 +01:00
|
|
|
// If this is the first constant we find or if the old on is local,
|
|
|
|
// replace with the current one. It the current is externally visible
|
|
|
|
// it cannot be replace, but can be the canonical constant we merge with.
|
|
|
|
if (Slot == 0 || IsBetterCannonical(*GV, *Slot)) {
|
2010-02-12 19:17:23 +01:00
|
|
|
Slot = GV;
|
2011-01-15 19:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 18:05:09 +01: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-15 19:14:21 +01:00
|
|
|
for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
|
|
|
|
GVI != E; ) {
|
|
|
|
GlobalVariable *GV = GVI++;
|
|
|
|
|
|
|
|
// Only process constants with initializers in the default address space.
|
2011-01-15 19:42:52 +01:00
|
|
|
if (!GV->isConstant() || !GV->hasDefinitiveInitializer() ||
|
2011-01-15 19:14:21 +01:00
|
|
|
GV->getType()->getAddressSpace() != 0 || GV->hasSection() ||
|
|
|
|
// Don't touch values marked with attribute(used).
|
|
|
|
UsedGlobals.count(GV))
|
|
|
|
continue;
|
|
|
|
|
2011-01-16 18:05:09 +01:00
|
|
|
// We can only replace constant with local linkage.
|
2011-01-15 19:14:21 +01:00
|
|
|
if (!GV->hasLocalLinkage())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Constant *Init = GV->getInitializer();
|
|
|
|
|
|
|
|
// Check to see if the initializer is already known.
|
2011-01-16 18:05:09 +01:00
|
|
|
GlobalVariable *Slot = CMap[Init];
|
2011-01-15 19:14:21 +01:00
|
|
|
|
2011-01-16 18:05:09 +01:00
|
|
|
if (!Slot || Slot == GV)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Slot->hasUnnamedAddr() && !GV->hasUnnamedAddr())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!GV->hasUnnamedAddr())
|
|
|
|
Slot->setUnnamedAddr(false);
|
|
|
|
|
|
|
|
// Make all uses of the duplicate constant use the canonical version.
|
|
|
|
Replacements.push_back(std::make_pair(GV, Slot));
|
2007-04-14 03:11:54 +02:00
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-12-28 08:19:08 +01:00
|
|
|
if (Replacements.empty())
|
|
|
|
return MadeChange;
|
|
|
|
CMap.clear();
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-12-28 08:19:08 +01: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) {
|
2010-02-12 19:17:23 +01:00
|
|
|
// Eliminate any uses of the dead global.
|
2003-12-28 08:19:08 +01:00
|
|
|
Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2010-02-12 19:17:23 +01:00
|
|
|
// Delete the global value from the module.
|
2011-01-15 19:14:21 +01:00
|
|
|
assert(Replacements[i].first->hasLocalLinkage() &&
|
|
|
|
"Refusing to delete an externally visible global variable.");
|
2010-02-12 19:17:23 +01:00
|
|
|
Replacements[i].first->eraseFromParent();
|
2001-10-18 22:05:37 +02:00
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-12-28 08:19:08 +01:00
|
|
|
NumMerged += Replacements.size();
|
|
|
|
Replacements.clear();
|
2003-12-23 00:49:36 +01:00
|
|
|
}
|
|
|
|
}
|