Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 1 | //===- AddReadAttrs.cpp - Pass which marks functions readnone or readonly -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a simple interprocedural pass which walks the |
| 11 | // call-graph, looking for functions which do not access or only read |
| 12 | // non-local memory, and marking them readnone/readonly. It implements |
| 13 | // this as a bottom-up traversal of the call-graph. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "addreadattrs" |
| 18 | #include "llvm/Transforms/IPO.h" |
| 19 | #include "llvm/CallGraphSCCPass.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Analysis/CallGraph.h" |
Duncan Sands | f726bc7 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/InstIterator.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 29 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 30 | |
| 31 | namespace { |
| 32 | struct VISIBILITY_HIDDEN AddReadAttrs : public CallGraphSCCPass { |
| 33 | static char ID; // Pass identification, replacement for typeid |
| 34 | AddReadAttrs() : CallGraphSCCPass(&ID) {} |
| 35 | |
| 36 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
| 37 | bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
Nuno Lopes | e723593 | 2008-09-30 18:34:38 +0000 | [diff] [blame] | 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.setPreservesCFG(); |
| 41 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 42 | } |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 43 | }; |
| 44 | } |
| 45 | |
| 46 | char AddReadAttrs::ID = 0; |
| 47 | static RegisterPass<AddReadAttrs> |
| 48 | X("addreadattrs", "Mark functions readnone/readonly"); |
| 49 | |
| 50 | Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); } |
| 51 | |
| 52 | |
| 53 | bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
Duncan Sands | f726bc7 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 54 | SmallPtrSet<CallGraphNode *, 8> SCCNodes; |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 55 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 56 | |
Duncan Sands | f726bc7 | 2008-09-29 14:59:04 +0000 | [diff] [blame] | 57 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 58 | // looking up whether a given CallGraphNode is in this SCC. |
| 59 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 60 | SCCNodes.insert(SCC[i]); |
| 61 | |
Duncan Sands | 120f8f9 | 2008-09-29 13:35:31 +0000 | [diff] [blame] | 62 | // Check if any of the functions in the SCC read or write memory. If they |
| 63 | // write memory then they can't be marked readnone or readonly. |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 64 | bool ReadsMemory = false; |
| 65 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 66 | Function *F = SCC[i]->getFunction(); |
| 67 | |
| 68 | if (F == 0) |
Duncan Sands | 120f8f9 | 2008-09-29 13:35:31 +0000 | [diff] [blame] | 69 | // External node - may write memory. Just give up. |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 70 | return false; |
| 71 | |
| 72 | if (F->doesNotAccessMemory()) |
| 73 | // Already perfect! |
| 74 | continue; |
| 75 | |
| 76 | // Definitions with weak linkage may be overridden at linktime with |
| 77 | // something that writes memory, so treat them like declarations. |
Duncan Sands | eb3f45f | 2008-09-29 11:25:42 +0000 | [diff] [blame] | 78 | if (F->isDeclaration() || F->mayBeOverridden()) { |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 79 | if (!F->onlyReadsMemory()) |
Duncan Sands | 120f8f9 | 2008-09-29 13:35:31 +0000 | [diff] [blame] | 80 | // May write memory. Just give up. |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 81 | return false; |
| 82 | |
| 83 | ReadsMemory = true; |
| 84 | continue; |
| 85 | } |
| 86 | |
Duncan Sands | 42b0e85 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 87 | // Scan the function body for instructions that may read or write memory. |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 88 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
Duncan Sands | 1d650ee | 2008-10-04 13:24:24 +0000 | [diff] [blame^] | 89 | Instruction *I = &*II; |
Duncan Sands | 42b0e85 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 90 | |
Duncan Sands | 1d650ee | 2008-10-04 13:24:24 +0000 | [diff] [blame^] | 91 | // Some instructions can be ignored even if they read or write memory. |
| 92 | // Detect these now, skipping to the next instruction if one is found. |
| 93 | CallSite CS = CallSite::get(I); |
| 94 | if (CS.getInstruction()) { |
| 95 | // Ignore calls to functions in the same SCC. |
| 96 | if (SCCNodes.count(CG[CS.getCalledFunction()])) |
| 97 | continue; |
| 98 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 99 | Value *Target = LI->getPointerOperand()->getUnderlyingObject(); |
| 100 | // Ignore loads from local memory. |
| 101 | if (isa<AllocaInst>(Target)) |
| 102 | continue; |
| 103 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 104 | Value *Target = SI->getPointerOperand()->getUnderlyingObject(); |
| 105 | // Ignore stores to local memory. |
| 106 | if (isa<AllocaInst>(Target)) |
| 107 | continue; |
| 108 | } |
Duncan Sands | 42b0e85 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 109 | |
Duncan Sands | 1d650ee | 2008-10-04 13:24:24 +0000 | [diff] [blame^] | 110 | // Any remaining instructions need to be taken seriously! Check if they |
| 111 | // read or write memory. |
| 112 | if (I->mayWriteToMemory()) |
Duncan Sands | 120f8f9 | 2008-09-29 13:35:31 +0000 | [diff] [blame] | 113 | // Writes memory. Just give up. |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 114 | return false; |
Duncan Sands | 1d650ee | 2008-10-04 13:24:24 +0000 | [diff] [blame^] | 115 | // If this instruction may read memory, remember that. |
| 116 | ReadsMemory |= I->mayReadFromMemory(); |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 121 | // Give them the appropriate attribute. |
| 122 | bool MadeChange = false; |
| 123 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 124 | Function *F = SCC[i]->getFunction(); |
| 125 | |
| 126 | if (F->doesNotAccessMemory()) |
| 127 | // Already perfect! |
| 128 | continue; |
| 129 | |
| 130 | if (F->onlyReadsMemory() && ReadsMemory) |
| 131 | // No change. |
| 132 | continue; |
| 133 | |
| 134 | MadeChange = true; |
| 135 | |
| 136 | // Clear out any existing attributes. |
Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 137 | F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 138 | |
| 139 | // Add in the new attribute. |
Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 140 | F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); |
Duncan Sands | 58f3877 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 141 | |
| 142 | if (ReadsMemory) |
| 143 | NumReadOnly++; |
| 144 | else |
| 145 | NumReadNone++; |
| 146 | } |
| 147 | |
| 148 | return MadeChange; |
| 149 | } |