blob: 0774cf28942466e8f6ae3c4627a05b2ec2ab70cc [file] [log] [blame]
Chris Lattner156bcf32003-04-18 04:34:29 +00001//===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
Chris Lattner475becb2001-10-18 20:05:37 +00002//
3// This file defines the interface to a pass that merges duplicate global
4// constants together into a single constant that is shared. This is useful
5// because some passes (ie TraceValues) insert a lot of string constants into
Chris Lattnere4314ed2002-09-23 23:00:46 +00006// the program, regardless of whether or not an existing string is available.
Chris Lattner475becb2001-10-18 20:05:37 +00007//
8// Algorithm: ConstantMerge is designed to build up a map of available constants
Chris Lattnere4314ed2002-09-23 23:00:46 +00009// and eliminate duplicates when it is initialized.
Chris Lattner475becb2001-10-18 20:05:37 +000010//
11//===----------------------------------------------------------------------===//
12
Chris Lattner9cfea852002-07-23 19:57:40 +000013#include "llvm/Transforms/IPO.h"
Chris Lattner793c6b82002-01-31 00:45:11 +000014#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000017
Chris Lattner9234d032002-06-25 15:55:29 +000018namespace {
Chris Lattnerc67ebfa2002-10-09 23:16:04 +000019 Statistic<> NumMerged("constmerge", "Number of global constants merged");
20
Chris Lattner9234d032002-06-25 15:55:29 +000021 struct ConstantMerge : public Pass {
Chris Lattner9234d032002-06-25 15:55:29 +000022 // run - For this pass, process all of the globals in the module,
23 // eliminating duplicate constants.
24 //
25 bool run(Module &M);
Chris Lattner9234d032002-06-25 15:55:29 +000026 };
Chris Lattneraf41a122002-07-23 18:06:30 +000027
Chris Lattner1e435162002-07-26 21:12:44 +000028 RegisterOpt<ConstantMerge> X("constmerge","Merge Duplicate Global Constants");
Chris Lattner9234d032002-06-25 15:55:29 +000029}
30
31Pass *createConstantMergePass() { return new ConstantMerge(); }
32
33
Chris Lattner9234d032002-06-25 15:55:29 +000034bool ConstantMerge::run(Module &M) {
35 std::map<Constant*, GlobalVariable*> CMap;
Chris Lattner475becb2001-10-18 20:05:37 +000036 bool MadeChanges = false;
37
Chris Lattner9234d032002-06-25 15:55:29 +000038 for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV)
Chris Lattner156bcf32003-04-18 04:34:29 +000039 // Only process constants with initializers
40 if (GV->isConstant() && GV->hasInitializer()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +000041 Constant *Init = GV->getInitializer();
Chris Lattner475becb2001-10-18 20:05:37 +000042
43 // Check to see if the initializer is already known...
Chris Lattner697954c2002-01-20 22:54:45 +000044 std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
Chris Lattner475becb2001-10-18 20:05:37 +000045
46 if (I == CMap.end()) { // Nope, add it to the map
Chris Lattner9234d032002-06-25 15:55:29 +000047 CMap.insert(I, std::make_pair(Init, GV));
Chris Lattner475becb2001-10-18 20:05:37 +000048 } else { // Yup, this is a duplicate!
49 // Make all uses of the duplicate constant use the cannonical version...
Chris Lattnerc67ebfa2002-10-09 23:16:04 +000050 GV->replaceAllUsesWith(I->second);
Chris Lattner475becb2001-10-18 20:05:37 +000051
Chris Lattner9234d032002-06-25 15:55:29 +000052 // Delete the global value from the module... and back up iterator to
53 // not skip the next global...
54 GV = --M.getGlobalList().erase(GV);
Chris Lattner475becb2001-10-18 20:05:37 +000055
Chris Lattner3dec1f22002-05-10 15:38:35 +000056 ++NumMerged;
Chris Lattner475becb2001-10-18 20:05:37 +000057 MadeChanges = true;
58 }
59 }
Chris Lattner9234d032002-06-25 15:55:29 +000060
Chris Lattner475becb2001-10-18 20:05:37 +000061 return MadeChanges;
62}