LLVM API Documentation

ConstantMerge.cpp

Go to the documentation of this file.
00001 //===- ConstantMerge.cpp - Merge duplicate global constants ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the interface to a pass that merges duplicate global
00011 // constants together into a single constant that is shared.  This is useful
00012 // because some passes (ie TraceValues) insert a lot of string constants into
00013 // the program, regardless of whether or not an existing string is available.
00014 //
00015 // Algorithm: ConstantMerge is designed to build up a map of available constants
00016 // and eliminate duplicates when it is initialized.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #define DEBUG_TYPE "constmerge"
00021 #include "llvm/Transforms/IPO.h"
00022 #include "llvm/Module.h"
00023 #include "llvm/Pass.h"
00024 #include "llvm/ADT/Statistic.h"
00025 #include "llvm/Support/Compiler.h"
00026 #include <map>
00027 using namespace llvm;
00028 
00029 STATISTIC(NumMerged, "Number of global constants merged");
00030 
00031 namespace {
00032   struct VISIBILITY_HIDDEN ConstantMerge : public ModulePass {
00033     static char ID; // Pass identification, replacement for typeid
00034     ConstantMerge() : ModulePass(&ID) {}
00035 
00036     // run - For this pass, process all of the globals in the module,
00037     // eliminating duplicate constants.
00038     //
00039     bool runOnModule(Module &M);
00040   };
00041 }
00042 
00043 char ConstantMerge::ID = 0;
00044 static RegisterPass<ConstantMerge>
00045 X("constmerge", "Merge Duplicate Global Constants");
00046 
00047 ModulePass *llvm::createConstantMergePass() { return new ConstantMerge(); }
00048 
00049 bool ConstantMerge::runOnModule(Module &M) {
00050   // Map unique constant/section pairs to globals.  We don't want to merge
00051   // globals in different sections.
00052   std::map<std::pair<Constant*, std::string>, GlobalVariable*> CMap;
00053 
00054   // Replacements - This vector contains a list of replacements to perform.
00055   std::vector<std::pair<GlobalVariable*, GlobalVariable*> > Replacements;
00056 
00057   bool MadeChange = false;
00058 
00059   // Iterate constant merging while we are still making progress.  Merging two
00060   // constants together may allow us to merge other constants together if the
00061   // second level constants have initializers which point to the globals that
00062   // were just merged.
00063   while (1) {
00064     // First pass: identify all globals that can be merged together, filling in
00065     // the Replacements vector.  We cannot do the replacement in this pass
00066     // because doing so may cause initializers of other globals to be rewritten,
00067     // invalidating the Constant* pointers in CMap.
00068     //
00069     for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
00070          GVI != E; ) {
00071       GlobalVariable *GV = GVI++;
00072       
00073       // If this GV is dead, remove it.
00074       GV->removeDeadConstantUsers();
00075       if (GV->use_empty() && GV->hasLocalLinkage()) {
00076         GV->eraseFromParent();
00077         continue;
00078       }
00079       
00080       // Only process constants with initializers.
00081       if (GV->isConstant() && GV->hasInitializer()) {
00082         Constant *Init = GV->getInitializer();
00083 
00084         // Check to see if the initializer is already known.
00085         GlobalVariable *&Slot = CMap[std::make_pair(Init, GV->getSection())];
00086 
00087         if (Slot == 0) {    // Nope, add it to the map.
00088           Slot = GV;
00089         } else if (GV->hasLocalLinkage()) {    // Yup, this is a duplicate!
00090           // Make all uses of the duplicate constant use the canonical version.
00091           Replacements.push_back(std::make_pair(GV, Slot));
00092         }
00093       }
00094     }
00095 
00096     if (Replacements.empty())
00097       return MadeChange;
00098     CMap.clear();
00099 
00100     // Now that we have figured out which replacements must be made, do them all
00101     // now.  This avoid invalidating the pointers in CMap, which are unneeded
00102     // now.
00103     for (unsigned i = 0, e = Replacements.size(); i != e; ++i) {
00104       // Eliminate any uses of the dead global...
00105       Replacements[i].first->replaceAllUsesWith(Replacements[i].second);
00106 
00107       // Delete the global value from the module...
00108       M.getGlobalList().erase(Replacements[i].first);
00109     }
00110 
00111     NumMerged += Replacements.size();
00112     Replacements.clear();
00113   }
00114 }