Duncan Sands | 9f07a29 | 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" |
| 22 | #include "llvm/ADT/Statistic.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/Support/InstIterator.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 28 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 29 | |
| 30 | namespace { |
| 31 | struct VISIBILITY_HIDDEN AddReadAttrs : public CallGraphSCCPass { |
| 32 | static char ID; // Pass identification, replacement for typeid |
| 33 | AddReadAttrs() : CallGraphSCCPass(&ID) {} |
| 34 | |
| 35 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
| 36 | bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | char AddReadAttrs::ID = 0; |
| 41 | static RegisterPass<AddReadAttrs> |
| 42 | X("addreadattrs", "Mark functions readnone/readonly"); |
| 43 | |
| 44 | Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); } |
| 45 | |
| 46 | |
| 47 | bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
| 48 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 49 | |
| 50 | // Check if any of the functions in the SCC read or write memory. |
| 51 | // If they write memory then just give up. |
| 52 | bool ReadsMemory = false; |
| 53 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 54 | Function *F = SCC[i]->getFunction(); |
| 55 | |
| 56 | if (F == 0) |
Duncan Sands | f95f945 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 57 | // External node - may write memory. |
Duncan Sands | 9f07a29 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 58 | return false; |
| 59 | |
| 60 | if (F->doesNotAccessMemory()) |
| 61 | // Already perfect! |
| 62 | continue; |
| 63 | |
| 64 | // Definitions with weak linkage may be overridden at linktime with |
| 65 | // something that writes memory, so treat them like declarations. |
| 66 | if (F->isDeclaration() || F->hasWeakLinkage()) { |
| 67 | if (!F->onlyReadsMemory()) |
| 68 | // May write memory. |
| 69 | return false; |
| 70 | |
| 71 | ReadsMemory = true; |
| 72 | continue; |
| 73 | } |
| 74 | |
Duncan Sands | f95f945 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 75 | // Scan the function body for instructions that may read or write memory. |
Duncan Sands | 9f07a29 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 76 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
Duncan Sands | f95f945 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 77 | CallSite CS = CallSite::get(&*II); |
| 78 | |
| 79 | // Ignore calls to functions in the same SCC. |
| 80 | if (CS.getInstruction() && |
| 81 | std::find(SCC.begin(), SCC.end(), CG[CS.getCalledFunction()]) != |
| 82 | SCC.end()) |
| 83 | continue; |
| 84 | |
| 85 | if (II->mayWriteToMemory()) |
Duncan Sands | 9f07a29 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 86 | return false; |
Duncan Sands | f95f945 | 2008-09-20 16:45:58 +0000 | [diff] [blame] | 87 | ReadsMemory |= II->mayReadFromMemory(); |
Duncan Sands | 9f07a29 | 2008-09-19 08:17:05 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 92 | // Give them the appropriate attribute. |
| 93 | bool MadeChange = false; |
| 94 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 95 | Function *F = SCC[i]->getFunction(); |
| 96 | |
| 97 | if (F->doesNotAccessMemory()) |
| 98 | // Already perfect! |
| 99 | continue; |
| 100 | |
| 101 | if (F->onlyReadsMemory() && ReadsMemory) |
| 102 | // No change. |
| 103 | continue; |
| 104 | |
| 105 | MadeChange = true; |
| 106 | |
| 107 | // Clear out any existing attributes. |
| 108 | F->removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone); |
| 109 | |
| 110 | // Add in the new attribute. |
| 111 | F->addParamAttr(0, ReadsMemory ? ParamAttr::ReadOnly : ParamAttr::ReadNone); |
| 112 | |
| 113 | if (ReadsMemory) |
| 114 | NumReadOnly++; |
| 115 | else |
| 116 | NumReadNone++; |
| 117 | } |
| 118 | |
| 119 | return MadeChange; |
| 120 | } |