2003-06-28 23:54:55 +02:00
|
|
|
//===- DataStructureOpt.cpp - Data Structure Analysis Based Optimizations -===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02: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.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-06-28 23:54:55 +02:00
|
|
|
//
|
|
|
|
// This pass uses DSA to a series of simple optimizations, like marking
|
|
|
|
// unwritten global variables 'constant'.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-07 08:32:21 +02:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2005-01-09 21:42:52 +01:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2003-06-28 23:54:55 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Constant.h"
|
2005-03-17 16:38:16 +01:00
|
|
|
#include "llvm/Type.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-13 00:11:14 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2003-06-28 23:54:55 +02:00
|
|
|
namespace {
|
|
|
|
Statistic<>
|
|
|
|
NumGlobalsConstanted("ds-opt", "Number of globals marked constant");
|
2003-06-29 00:10:58 +02:00
|
|
|
Statistic<>
|
|
|
|
NumGlobalsIsolated("ds-opt", "Number of globals with references dropped");
|
2003-06-28 23:54:55 +02:00
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
class DSOpt : public ModulePass {
|
2003-06-28 23:54:55 +02:00
|
|
|
TDDataStructures *TD;
|
|
|
|
public:
|
2004-09-20 06:48:05 +02:00
|
|
|
bool runOnModule(Module &M) {
|
2003-06-28 23:54:55 +02:00
|
|
|
TD = &getAnalysis<TDDataStructures>();
|
|
|
|
bool Changed = OptimizeGlobals(M);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
|
|
|
|
AU.addPreserved<LocalDataStructures>(); // Preserves local...
|
|
|
|
AU.addPreserved<TDDataStructures>(); // Preserves bu...
|
|
|
|
AU.addPreserved<BUDataStructures>(); // Preserves td...
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool OptimizeGlobals(Module &M);
|
|
|
|
};
|
|
|
|
|
|
|
|
RegisterOpt<DSOpt> X("ds-opt", "DSA-based simple optimizations");
|
|
|
|
}
|
|
|
|
|
2005-01-09 21:42:52 +01:00
|
|
|
ModulePass *llvm::createDSOptPass() { return new DSOpt(); }
|
|
|
|
|
2003-06-28 23:54:55 +02:00
|
|
|
/// OptimizeGlobals - This method uses information taken from DSA to optimize
|
|
|
|
/// global variables.
|
|
|
|
///
|
|
|
|
bool DSOpt::OptimizeGlobals(Module &M) {
|
|
|
|
DSGraph &GG = TD->getGlobalsGraph();
|
|
|
|
const DSGraph::ScalarMapTy &SM = GG.getScalarMap();
|
|
|
|
bool Changed = false;
|
|
|
|
|
2005-03-15 05:54:21 +01:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2003-06-28 23:54:55 +02:00
|
|
|
if (!I->isExternal()) { // Loop over all of the non-external globals...
|
|
|
|
// Look up the node corresponding to this global, if it exists.
|
|
|
|
DSNode *GNode = 0;
|
|
|
|
DSGraph::ScalarMapTy::const_iterator SMI = SM.find(I);
|
|
|
|
if (SMI != SM.end()) GNode = SMI->second.getNode();
|
2005-04-21 23:13:18 +02:00
|
|
|
|
2003-06-28 23:54:55 +02:00
|
|
|
if (GNode == 0 && I->hasInternalLinkage()) {
|
|
|
|
// If there is no entry in the scalar map for this global, it was never
|
|
|
|
// referenced in the program. If it has internal linkage, that means we
|
|
|
|
// can delete it. We don't ACTUALLY want to delete the global, just
|
|
|
|
// remove anything that references the global: later passes will take
|
|
|
|
// care of nuking it.
|
2003-06-29 00:10:58 +02:00
|
|
|
if (!I->use_empty()) {
|
|
|
|
I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
|
|
|
|
++NumGlobalsIsolated;
|
|
|
|
}
|
2003-06-28 23:54:55 +02:00
|
|
|
} else if (GNode && GNode->isComplete()) {
|
2003-06-29 00:10:58 +02:00
|
|
|
|
|
|
|
// If the node has not been read or written, and it is not externally
|
|
|
|
// visible, kill any references to it so it can be DCE'd.
|
|
|
|
if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){
|
|
|
|
if (!I->use_empty()) {
|
|
|
|
I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
|
|
|
|
++NumGlobalsIsolated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-28 23:54:55 +02:00
|
|
|
// We expect that there will almost always be a node for this global.
|
|
|
|
// If there is, and the node doesn't have the M bit set, we can set the
|
|
|
|
// 'constant' bit on the global.
|
|
|
|
if (!GNode->isModified() && !I->isConstant()) {
|
|
|
|
I->setConstant(true);
|
|
|
|
++NumGlobalsConstanted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|