1 //===- AddReadAttrs.cpp - Pass which marks functions readnone or readonly -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "addreadattrs"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/CallGraphSCCPass.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Analysis/CallGraph.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/InstIterator.h"
29 STATISTIC(NumReadNone, "Number of functions marked readnone");
30 STATISTIC(NumReadOnly, "Number of functions marked readonly");
33 struct VISIBILITY_HIDDEN AddReadAttrs : public CallGraphSCCPass {
34 static char ID; // Pass identification, replacement for typeid
35 AddReadAttrs() : CallGraphSCCPass(&ID) {}
37 // runOnSCC - Analyze the SCC, performing the transformation if possible.
38 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 CallGraphSCCPass::getAnalysisUsage(AU);
45 bool PointsToLocalMemory(Value *V);
49 char AddReadAttrs::ID = 0;
50 static RegisterPass<AddReadAttrs>
51 X("addreadattrs", "Mark functions readnone/readonly");
53 Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); }
56 /// PointsToLocalMemory - Returns whether the given pointer value points to
57 /// memory that is local to the function. Global constants are considered
58 /// local to all functions.
59 bool AddReadAttrs::PointsToLocalMemory(Value *V) {
60 V = V->getUnderlyingObject();
61 // An alloca instruction defines local memory.
62 if (isa<AllocaInst>(V))
64 // A global constant counts as local memory for our purposes.
65 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
66 return GV->isConstant();
70 bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
71 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
72 CallGraph &CG = getAnalysis<CallGraph>();
74 // Fill SCCNodes with the elements of the SCC. Used for quickly
75 // looking up whether a given CallGraphNode is in this SCC.
76 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
77 SCCNodes.insert(SCC[i]);
79 // Check if any of the functions in the SCC read or write memory. If they
80 // write memory then they can't be marked readnone or readonly.
81 bool ReadsMemory = false;
82 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
83 Function *F = SCC[i]->getFunction();
86 // External node - may write memory. Just give up.
89 if (F->doesNotAccessMemory())
93 // Definitions with weak linkage may be overridden at linktime with
94 // something that writes memory, so treat them like declarations.
95 if (F->isDeclaration() || F->mayBeOverridden()) {
96 if (!F->onlyReadsMemory())
97 // May write memory. Just give up.
104 // Scan the function body for instructions that may read or write memory.
105 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
106 Instruction *I = &*II;
108 // Some instructions can be ignored even if they read or write memory.
109 // Detect these now, skipping to the next instruction if one is found.
110 CallSite CS = CallSite::get(I);
111 if (CS.getInstruction()) {
112 // Ignore calls to functions in the same SCC.
113 if (SCCNodes.count(CG[CS.getCalledFunction()]))
115 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
116 // Ignore loads from local memory.
117 if (PointsToLocalMemory(LI->getPointerOperand()))
119 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
120 // Ignore stores to local memory.
121 if (PointsToLocalMemory(SI->getPointerOperand()))
125 // Any remaining instructions need to be taken seriously! Check if they
126 // read or write memory.
127 if (I->mayWriteToMemory())
128 // Writes memory. Just give up.
130 // If this instruction may read memory, remember that.
131 ReadsMemory |= I->mayReadFromMemory();
135 // Success! Functions in this SCC do not access memory, or only read memory.
136 // Give them the appropriate attribute.
137 bool MadeChange = false;
138 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
139 Function *F = SCC[i]->getFunction();
141 if (F->doesNotAccessMemory())
145 if (F->onlyReadsMemory() && ReadsMemory)
151 // Clear out any existing attributes.
152 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
154 // Add in the new attribute.
155 F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);