2003-04-18 04:34:29 +00:00
|
|
|
//===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-18 20:05:37 +00: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-23 23:00:46 +00:00
|
|
|
// the program, regardless of whether or not an existing string is available.
|
2001-10-18 20:05:37 +00:00
|
|
|
//
|
|
|
|
// Algorithm: ConstantMerge is designed to build up a map of available constants
|
2002-09-23 23:00:46 +00:00
|
|
|
// and eliminate duplicates when it is initialized.
|
2001-10-18 20:05:37 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-23 19:57:40 +00:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-01-31 00:45:11 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-11-21 21:54:22 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-06-25 15:55:29 +00:00
|
|
|
namespace {
|
2002-10-09 23:16:04 +00:00
|
|
|
Statistic<> NumMerged("constmerge", "Number of global constants merged");
|
|
|
|
|
2002-06-25 15:55:29 +00:00
|
|
|
struct ConstantMerge : public Pass {
|
|
|
|
// run - For this pass, process all of the globals in the module,
|
|
|
|
// eliminating duplicate constants.
|
|
|
|
//
|
|
|
|
bool run(Module &M);
|
|
|
|
};
|
2002-07-23 18:06:30 +00:00
|
|
|
|
2002-07-26 21:12:44 +00:00
|
|
|
RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants");
|
2002-06-25 15:55:29 +00:00
|
|
|
}
|
|
|
|
|
2003-11-21 21:54:22 +00:00
|
|
|
Pass *llvm::createConstantMergePass() { return new ConstantMerge(); }
|
2002-06-25 15:55:29 +00:00
|
|
|
|
|
|
|
bool ConstantMerge::run(Module &M) {
|
|
|
|
std::map<Constant*, GlobalVariable*> CMap;
|
2001-10-18 20:05:37 +00:00
|
|
|
bool MadeChanges = false;
|
|
|
|
|
2002-06-25 15:55:29 +00:00
|
|
|
for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV)
|
2003-04-18 04:34:29 +00:00
|
|
|
// Only process constants with initializers
|
|
|
|
if (GV->isConstant() && GV->hasInitializer()) {
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *Init = GV->getInitializer();
|
2001-10-18 20:05:37 +00:00
|
|
|
|
|
|
|
// Check to see if the initializer is already known...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
|
2001-10-18 20:05:37 +00:00
|
|
|
|
|
|
|
if (I == CMap.end()) { // Nope, add it to the map
|
2002-06-25 15:55:29 +00:00
|
|
|
CMap.insert(I, std::make_pair(Init, GV));
|
2003-10-29 06:01:26 +00:00
|
|
|
} else if (GV->hasInternalLinkage()) { // Yup, this is a duplicate!
|
2003-09-10 05:29:43 +00:00
|
|
|
// Make all uses of the duplicate constant use the canonical version...
|
2002-10-09 23:16:04 +00:00
|
|
|
GV->replaceAllUsesWith(I->second);
|
2003-10-29 06:01:26 +00:00
|
|
|
|
2002-06-25 15:55:29 +00:00
|
|
|
// Delete the global value from the module... and back up iterator to
|
|
|
|
// not skip the next global...
|
|
|
|
GV = --M.getGlobalList().erase(GV);
|
2001-10-18 20:05:37 +00:00
|
|
|
|
2003-10-29 06:01:26 +00:00
|
|
|
++NumMerged;
|
|
|
|
MadeChanges = true;
|
|
|
|
} else if (I->second->hasInternalLinkage()) {
|
|
|
|
// Make all uses of the duplicate constant use the canonical version...
|
|
|
|
I->second->replaceAllUsesWith(GV);
|
|
|
|
|
|
|
|
// Delete the global value from the module... and back up iterator to
|
|
|
|
// not skip the next global...
|
|
|
|
M.getGlobalList().erase(I->second);
|
|
|
|
I->second = GV;
|
|
|
|
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumMerged;
|
2001-10-18 20:05:37 +00:00
|
|
|
MadeChanges = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-25 15:55:29 +00:00
|
|
|
return MadeChanges;
|
2001-10-18 20:05:37 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|