blob: d037b52145ec2cc48fdcc0a4ce4fba66be54a962 [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
15#include "llvm/Analysis/DataStructure.h"
16#include "llvm/Analysis/DSGraph.h"
17#include "llvm/Module.h"
18#include "llvm/Constant.h"
19#include "Support/Statistic.h"
20
Brian Gaeked0fde302003-11-11 22:41:34 +000021namespace llvm {
22
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
29 class DSOpt : public Pass {
30 TDDataStructures *TD;
31 public:
32 bool run(Module &M) {
33 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
Chris Lattner934fe852003-06-28 21:54:55 +000052/// OptimizeGlobals - This method uses information taken from DSA to optimize
53/// global variables.
54///
55bool DSOpt::OptimizeGlobals(Module &M) {
56 DSGraph &GG = TD->getGlobalsGraph();
57 const DSGraph::ScalarMapTy &SM = GG.getScalarMap();
58 bool Changed = false;
59
60 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
61 if (!I->isExternal()) { // Loop over all of the non-external globals...
62 // Look up the node corresponding to this global, if it exists.
63 DSNode *GNode = 0;
64 DSGraph::ScalarMapTy::const_iterator SMI = SM.find(I);
65 if (SMI != SM.end()) GNode = SMI->second.getNode();
66
67 if (GNode == 0 && I->hasInternalLinkage()) {
68 // If there is no entry in the scalar map for this global, it was never
69 // referenced in the program. If it has internal linkage, that means we
70 // can delete it. We don't ACTUALLY want to delete the global, just
71 // remove anything that references the global: later passes will take
72 // care of nuking it.
Chris Lattner5e459db2003-06-28 22:10:58 +000073 if (!I->use_empty()) {
74 I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
75 ++NumGlobalsIsolated;
76 }
Chris Lattner934fe852003-06-28 21:54:55 +000077 } else if (GNode && GNode->isComplete()) {
Chris Lattner5e459db2003-06-28 22:10:58 +000078
79 // If the node has not been read or written, and it is not externally
80 // visible, kill any references to it so it can be DCE'd.
81 if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){
82 if (!I->use_empty()) {
83 I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType()));
84 ++NumGlobalsIsolated;
85 }
86 }
87
Chris Lattner934fe852003-06-28 21:54:55 +000088 // We expect that there will almost always be a node for this global.
89 // If there is, and the node doesn't have the M bit set, we can set the
90 // 'constant' bit on the global.
91 if (!GNode->isModified() && !I->isConstant()) {
92 I->setConstant(true);
93 ++NumGlobalsConstanted;
94 Changed = true;
95 }
96 }
97 }
98 return Changed;
99}
Brian Gaeked0fde302003-11-11 22:41:34 +0000100
101} // End llvm namespace