blob: c75784b9647d0edafe3239b2fa4ba6003fd0b275 [file] [log] [blame]
Chris Lattner934fe852003-06-28 21:54:55 +00001//===- DataStructureOpt.cpp - Data Structure Analysis Based Optimizations -===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner934fe852003-06-28 21:54:55 +00009//
10// This pass uses DSA to a series of simple optimizations, like marking
11// unwritten global variables 'constant'.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4dabb2c2004-07-07 06:32:21 +000015#include "llvm/Analysis/DataStructure/DataStructure.h"
16#include "llvm/Analysis/DataStructure/DSGraph.h"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000017#include "llvm/Analysis/Passes.h"
Chris Lattner934fe852003-06-28 21:54:55 +000018#include "llvm/Module.h"
19#include "llvm/Constant.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/Statistic.h"
Chris Lattner9a927292003-11-12 23:11:14 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner934fe852003-06-28 21:54:55 +000023namespace {
24 Statistic<>
25 NumGlobalsConstanted("ds-opt", "Number of globals marked constant");
Chris Lattner5e459db2003-06-28 22:10:58 +000026 Statistic<>
27 NumGlobalsIsolated("ds-opt", "Number of globals with references dropped");
Chris Lattner934fe852003-06-28 21:54:55 +000028
Chris Lattnerb12914b2004-09-20 04:48:05 +000029 class DSOpt : public ModulePass {
Chris Lattner934fe852003-06-28 21:54:55 +000030 TDDataStructures *TD;
31 public:
Chris Lattnerb12914b2004-09-20 04:48:05 +000032 bool runOnModule(Module &M) {
Chris Lattner934fe852003-06-28 21:54:55 +000033 TD = &getAnalysis<TDDataStructures>();
34 bool Changed = OptimizeGlobals(M);
35 return Changed;
36 }
37
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
40 AU.addPreserved<LocalDataStructures>(); // Preserves local...
41 AU.addPreserved<TDDataStructures>(); // Preserves bu...
42 AU.addPreserved<BUDataStructures>(); // Preserves td...
43 }
44
45 private:
46 bool OptimizeGlobals(Module &M);
47 };
48
49 RegisterOpt<DSOpt> X("ds-opt", "DSA-based simple optimizations");
50}
51
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000052ModulePass *llvm::createDSOptPass() { return new DSOpt(); }
53
Chris Lattner934fe852003-06-28 21:54:55 +000054/// OptimizeGlobals - This method uses information taken from DSA to optimize
55/// global variables.
56///
57bool DSOpt::OptimizeGlobals(Module &M) {
58 DSGraph &GG = TD->getGlobalsGraph();
59 const DSGraph::ScalarMapTy &SM = GG.getScalarMap();
60 bool Changed = false;
61
Chris Lattnere4d5c442005-03-15 04:54:21 +000062 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattner934fe852003-06-28 21:54:55 +000063 if (!I->isExternal()) { // Loop over all of the non-external globals...
64 // Look up the node corresponding to this global, if it exists.
65 DSNode *GNode = 0;
66 DSGraph::ScalarMapTy::const_iterator SMI = SM.find(I);
67 if (SMI != SM.end()) GNode = SMI->second.getNode();
68
69 if (GNode == 0 && I->hasInternalLinkage()) {
70 // If there is no entry in the scalar map for this global, it was never
71 // referenced in the program. If it has internal linkage, that means we
72 // can delete it. We don't ACTUALLY want to delete the global, just
73 // remove anything that references the global: later passes will take
74 // care of nuking it.
Chris Lattner5e459db2003-06-28 22:10:58 +000075 if (!I->use_empty()) {
76 I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
77 ++NumGlobalsIsolated;
78 }
Chris Lattner934fe852003-06-28 21:54:55 +000079 } else if (GNode && GNode->isComplete()) {
Chris Lattner5e459db2003-06-28 22:10:58 +000080
81 // If the node has not been read or written, and it is not externally
82 // visible, kill any references to it so it can be DCE'd.
83 if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){
84 if (!I->use_empty()) {
85 I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
86 ++NumGlobalsIsolated;
87 }
88 }
89
Chris Lattner934fe852003-06-28 21:54:55 +000090 // We expect that there will almost always be a node for this global.
91 // If there is, and the node doesn't have the M bit set, we can set the
92 // 'constant' bit on the global.
93 if (!GNode->isModified() && !I->isConstant()) {
94 I->setConstant(true);
95 ++NumGlobalsConstanted;
96 Changed = true;
97 }
98 }
99 }
100 return Changed;
101}