2001-10-18 20:05:37 +00:00
|
|
|
//===- ConstantMerge.cpp - Merge duplicate global constants -----------------=//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the program, regardless of whether or not they duplicate an existing string.
|
|
|
|
//
|
|
|
|
// Algorithm: ConstantMerge is designed to build up a map of available constants
|
|
|
|
// and elminate duplicates when it is initialized.
|
|
|
|
//
|
|
|
|
// The DynamicConstantMerge method is a superset of the ConstantMerge algorithm
|
2002-04-07 20:49:59 +00:00
|
|
|
// that checks for each function to see if constants have been added to the
|
2001-10-18 20:05:37 +00:00
|
|
|
// constant pool since it was last run... if so, it processes them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/ConstantMerge.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
2002-01-31 00:45:11 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-05-10 15:38:35 +00:00
|
|
|
#include "Support/StatisticReporter.h"
|
|
|
|
|
|
|
|
static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
|
2001-10-18 20:05:37 +00:00
|
|
|
|
|
|
|
// mergeDuplicateConstants - Workhorse for the pass. This eliminates duplicate
|
|
|
|
// constants, starting at global ConstantNo, and adds vars to the map if they
|
|
|
|
// are new and unique.
|
|
|
|
//
|
|
|
|
static inline
|
|
|
|
bool mergeDuplicateConstants(Module *M, unsigned &ConstantNo,
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<Constant*, GlobalVariable*> &CMap) {
|
2001-10-18 20:05:37 +00:00
|
|
|
Module::GlobalListType &GList = M->getGlobalList();
|
|
|
|
if (GList.size() <= ConstantNo) return false; // No new constants
|
|
|
|
bool MadeChanges = false;
|
|
|
|
|
|
|
|
for (; ConstantNo < GList.size(); ++ConstantNo) {
|
|
|
|
GlobalVariable *GV = GList[ConstantNo];
|
|
|
|
if (GV->isConstant()) { // Only process constants
|
|
|
|
assert(GV->hasInitializer() && "Globals constants must have inits!");
|
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-01-20 22:54:45 +00:00
|
|
|
CMap.insert(std::make_pair(Init, GV));
|
2001-10-18 20:05:37 +00:00
|
|
|
} else { // Yup, this is a duplicate!
|
|
|
|
// Make all uses of the duplicate constant use the cannonical version...
|
|
|
|
GV->replaceAllUsesWith(I->second);
|
|
|
|
|
|
|
|
// Remove and delete the global value from the module...
|
|
|
|
delete GList.remove(GList.begin()+ConstantNo);
|
|
|
|
|
|
|
|
--ConstantNo; // Don't skip the next constant.
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumMerged;
|
2001-10-18 20:05:37 +00:00
|
|
|
MadeChanges = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChanges;
|
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
// FIXME: ConstantMerge should not be a FunctionPass!!!
|
|
|
|
class ConstantMerge : public FunctionPass {
|
2002-02-26 21:46:54 +00:00
|
|
|
protected:
|
|
|
|
std::map<Constant*, GlobalVariable*> Constants;
|
|
|
|
unsigned LastConstantSeen;
|
|
|
|
public:
|
|
|
|
inline ConstantMerge() : LastConstantSeen(0) {}
|
2002-04-29 14:57:45 +00:00
|
|
|
|
|
|
|
const char *getPassName() const {return "Merge Duplicate Global Constants";}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
// doInitialization - For this pass, process all of the globals in the
|
|
|
|
// module, eliminating duplicate constants.
|
|
|
|
//
|
|
|
|
bool doInitialization(Module *M) {
|
|
|
|
return ::mergeDuplicateConstants(M, LastConstantSeen, Constants);
|
|
|
|
}
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
bool runOnFunction(Function *) { return false; }
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
// doFinalization - Clean up internal state for this module
|
|
|
|
//
|
|
|
|
bool doFinalization(Module *M) {
|
|
|
|
LastConstantSeen = 0;
|
|
|
|
Constants.clear();
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-28 21:27:06 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DynamicConstantMerge : public ConstantMerge {
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Dynamic Constant Merge"; }
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
// runOnFunction - Check to see if any globals have been added to the
|
2002-02-26 21:46:54 +00:00
|
|
|
// global list for the module. If so, eliminate them.
|
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
bool runOnFunction(Function *F) {
|
2002-04-07 20:49:59 +00:00
|
|
|
return ::mergeDuplicateConstants(F->getParent(), LastConstantSeen,
|
2002-02-26 21:46:54 +00:00
|
|
|
Constants);
|
|
|
|
}
|
|
|
|
};
|
2001-10-18 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
Pass *createConstantMergePass() { return new ConstantMerge(); }
|
|
|
|
Pass *createDynamicConstantMergePass() { return new DynamicConstantMerge(); }
|