Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 1 | //===- DataStructureOpt.cpp - Data Structure Analysis Based Optimizations -===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass uses DSA to a series of simple optimizations, like marking |
| 11 | // unwritten global variables 'constant'. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
| 16 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Jeff Cohen | 1d7b5de | 2005-01-09 20:42:52 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Constant.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame^] | 20 | #include "llvm/Type.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | Statistic<> |
| 26 | NumGlobalsConstanted("ds-opt", "Number of globals marked constant"); |
Chris Lattner | 5e459db | 2003-06-28 22:10:58 +0000 | [diff] [blame] | 27 | Statistic<> |
| 28 | NumGlobalsIsolated("ds-opt", "Number of globals with references dropped"); |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 29 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 30 | class DSOpt : public ModulePass { |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 31 | TDDataStructures *TD; |
| 32 | public: |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 33 | bool runOnModule(Module &M) { |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 34 | TD = &getAnalysis<TDDataStructures>(); |
| 35 | bool Changed = OptimizeGlobals(M); |
| 36 | return Changed; |
| 37 | } |
| 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.addRequired<TDDataStructures>(); // Uses TD Datastructures |
| 41 | AU.addPreserved<LocalDataStructures>(); // Preserves local... |
| 42 | AU.addPreserved<TDDataStructures>(); // Preserves bu... |
| 43 | AU.addPreserved<BUDataStructures>(); // Preserves td... |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | bool OptimizeGlobals(Module &M); |
| 48 | }; |
| 49 | |
| 50 | RegisterOpt<DSOpt> X("ds-opt", "DSA-based simple optimizations"); |
| 51 | } |
| 52 | |
Jeff Cohen | 1d7b5de | 2005-01-09 20:42:52 +0000 | [diff] [blame] | 53 | ModulePass *llvm::createDSOptPass() { return new DSOpt(); } |
| 54 | |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 55 | /// OptimizeGlobals - This method uses information taken from DSA to optimize |
| 56 | /// global variables. |
| 57 | /// |
| 58 | bool DSOpt::OptimizeGlobals(Module &M) { |
| 59 | DSGraph &GG = TD->getGlobalsGraph(); |
| 60 | const DSGraph::ScalarMapTy &SM = GG.getScalarMap(); |
| 61 | bool Changed = false; |
| 62 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 63 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 64 | if (!I->isExternal()) { // Loop over all of the non-external globals... |
| 65 | // Look up the node corresponding to this global, if it exists. |
| 66 | DSNode *GNode = 0; |
| 67 | DSGraph::ScalarMapTy::const_iterator SMI = SM.find(I); |
| 68 | if (SMI != SM.end()) GNode = SMI->second.getNode(); |
| 69 | |
| 70 | if (GNode == 0 && I->hasInternalLinkage()) { |
| 71 | // If there is no entry in the scalar map for this global, it was never |
| 72 | // referenced in the program. If it has internal linkage, that means we |
| 73 | // can delete it. We don't ACTUALLY want to delete the global, just |
| 74 | // remove anything that references the global: later passes will take |
| 75 | // care of nuking it. |
Chris Lattner | 5e459db | 2003-06-28 22:10:58 +0000 | [diff] [blame] | 76 | if (!I->use_empty()) { |
| 77 | I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType())); |
| 78 | ++NumGlobalsIsolated; |
| 79 | } |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 80 | } else if (GNode && GNode->isComplete()) { |
Chris Lattner | 5e459db | 2003-06-28 22:10:58 +0000 | [diff] [blame] | 81 | |
| 82 | // If the node has not been read or written, and it is not externally |
| 83 | // visible, kill any references to it so it can be DCE'd. |
| 84 | if (!GNode->isModified() && !GNode->isRead() &&I->hasInternalLinkage()){ |
| 85 | if (!I->use_empty()) { |
| 86 | I->replaceAllUsesWith(Constant::getNullValue((Type*)I->getType())); |
| 87 | ++NumGlobalsIsolated; |
| 88 | } |
| 89 | } |
| 90 | |
Chris Lattner | 934fe85 | 2003-06-28 21:54:55 +0000 | [diff] [blame] | 91 | // We expect that there will almost always be a node for this global. |
| 92 | // If there is, and the node doesn't have the M bit set, we can set the |
| 93 | // 'constant' bit on the global. |
| 94 | if (!GNode->isModified() && !I->isConstant()) { |
| 95 | I->setConstant(true); |
| 96 | ++NumGlobalsConstanted; |
| 97 | Changed = true; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return Changed; |
| 102 | } |