mirror of
				https://github.com/c64scene-ar/llvm-6502.git
				synced 2025-11-04 05:17:07 +00:00 
			
		
		
		
	system correctly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2772 91177308-0d34-0410-b5e6-96231b3b80d8
		
			
				
	
	
		
			75 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
//===- 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
 | 
						|
// that checks for each function to see if constants have been added to the
 | 
						|
// constant pool since it was last run... if so, it processes them.
 | 
						|
//
 | 
						|
//===----------------------------------------------------------------------===//
 | 
						|
 | 
						|
#include "llvm/Transforms/ConstantMerge.h"
 | 
						|
#include "llvm/Module.h"
 | 
						|
#include "llvm/Pass.h"
 | 
						|
#include "Support/StatisticReporter.h"
 | 
						|
 | 
						|
static Statistic<> NumMerged("constmerge\t\t- Number of global constants merged");
 | 
						|
 | 
						|
namespace {
 | 
						|
  struct ConstantMerge : public Pass {
 | 
						|
    const char *getPassName() const {return "Merge Duplicate Global Constants";}
 | 
						|
    
 | 
						|
    // run - For this pass, process all of the globals in the module,
 | 
						|
    // eliminating duplicate constants.
 | 
						|
    //
 | 
						|
    bool run(Module &M);
 | 
						|
 | 
						|
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
 | 
						|
      AU.preservesCFG();
 | 
						|
    }
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
Pass *createConstantMergePass() { return new ConstantMerge(); }
 | 
						|
 | 
						|
 | 
						|
// ConstantMerge::run - Workhorse for the pass.  This eliminates duplicate
 | 
						|
// constants, starting at global ConstantNo, and adds vars to the map if they
 | 
						|
// are new and unique.
 | 
						|
//
 | 
						|
bool ConstantMerge::run(Module &M) {
 | 
						|
  std::map<Constant*, GlobalVariable*> CMap;
 | 
						|
  bool MadeChanges = false;
 | 
						|
  
 | 
						|
  for (Module::giterator GV = M.gbegin(), E = M.gend(); GV != E; ++GV)
 | 
						|
    if (GV->isConstant()) {  // Only process constants
 | 
						|
      assert(GV->hasInitializer() && "Globals constants must have inits!");
 | 
						|
      Constant *Init = GV->getInitializer();
 | 
						|
 | 
						|
      // Check to see if the initializer is already known...
 | 
						|
      std::map<Constant*, GlobalVariable*>::iterator I = CMap.find(Init);
 | 
						|
 | 
						|
      if (I == CMap.end()) {    // Nope, add it to the map
 | 
						|
        CMap.insert(I, std::make_pair(Init, GV));
 | 
						|
      } else {                  // Yup, this is a duplicate!
 | 
						|
        // Make all uses of the duplicate constant use the cannonical version...
 | 
						|
        GV->replaceAllUsesWith(I->second);
 | 
						|
 | 
						|
        // Delete the global value from the module... and back up iterator to
 | 
						|
        // not skip the next global...
 | 
						|
        GV = --M.getGlobalList().erase(GV);
 | 
						|
 | 
						|
        ++NumMerged;
 | 
						|
        MadeChanges = true;
 | 
						|
      }
 | 
						|
    }
 | 
						|
 | 
						|
  return MadeChanges;
 | 
						|
}
 |