blob: 47485737ab82bf1a046cbb7166e8946000f1747d [file] [log] [blame]
Duncan Sands9e89ba32008-12-31 16:14:43 +00001//===- FunctionAttrs.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
Duncan Sandsb2f22792009-01-02 11:46:24 +000012// non-local memory, and marking them readnone/readonly. In addition,
13// it marks function arguments (of pointer type) 'nocapture' if a call
14// to the function does not create any copies of the pointer value that
15// outlive the call. This more or less means that the pointer is only
16// dereferenced, and not returned from the function or stored in a global.
17// This pass is implemented as a bottom-up traversal of the call-graph.
Duncan Sands9e89ba32008-12-31 16:14:43 +000018//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "functionattrs"
22#include "llvm/Transforms/IPO.h"
23#include "llvm/CallGraphSCCPass.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Instructions.h"
26#include "llvm/Analysis/CallGraph.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000027#include "llvm/Analysis/CaptureTracking.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000028#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/InstIterator.h"
32using namespace llvm;
33
34STATISTIC(NumReadNone, "Number of functions marked readnone");
35STATISTIC(NumReadOnly, "Number of functions marked readonly");
36STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
37
38namespace {
39 struct VISIBILITY_HIDDEN FunctionAttrs : public CallGraphSCCPass {
40 static char ID; // Pass identification, replacement for typeid
41 FunctionAttrs() : CallGraphSCCPass(&ID) {}
42
43 // runOnSCC - Analyze the SCC, performing the transformation if possible.
44 bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
45
46 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
47 bool AddReadAttrs(const std::vector<CallGraphNode *> &SCC);
48
49 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
50 bool AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC);
51
Duncan Sands9e89ba32008-12-31 16:14:43 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesCFG();
54 CallGraphSCCPass::getAnalysisUsage(AU);
55 }
56
57 bool PointsToLocalMemory(Value *V);
58 };
59}
60
61char FunctionAttrs::ID = 0;
62static RegisterPass<FunctionAttrs>
63X("functionattrs", "Deduce function attributes");
64
65Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
66
67
68/// PointsToLocalMemory - Returns whether the given pointer value points to
69/// memory that is local to the function. Global constants are considered
70/// local to all functions.
71bool FunctionAttrs::PointsToLocalMemory(Value *V) {
72 V = V->getUnderlyingObject();
73 // An alloca instruction defines local memory.
74 if (isa<AllocaInst>(V))
75 return true;
76 // A global constant counts as local memory for our purposes.
77 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
78 return GV->isConstant();
79 // Could look through phi nodes and selects here, but it doesn't seem
80 // to be useful in practice.
81 return false;
82}
83
84/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
85bool FunctionAttrs::AddReadAttrs(const std::vector<CallGraphNode *> &SCC) {
86 SmallPtrSet<CallGraphNode*, 8> SCCNodes;
87 CallGraph &CG = getAnalysis<CallGraph>();
88
89 // Fill SCCNodes with the elements of the SCC. Used for quickly
90 // looking up whether a given CallGraphNode is in this SCC.
91 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
92 SCCNodes.insert(SCC[i]);
93
94 // Check if any of the functions in the SCC read or write memory. If they
95 // write memory then they can't be marked readnone or readonly.
96 bool ReadsMemory = false;
97 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
98 Function *F = SCC[i]->getFunction();
99
100 if (F == 0)
101 // External node - may write memory. Just give up.
102 return false;
103
104 if (F->doesNotAccessMemory())
105 // Already perfect!
106 continue;
107
108 // Definitions with weak linkage may be overridden at linktime with
109 // something that writes memory, so treat them like declarations.
110 if (F->isDeclaration() || F->mayBeOverridden()) {
111 if (!F->onlyReadsMemory())
112 // May write memory. Just give up.
113 return false;
114
115 ReadsMemory = true;
116 continue;
117 }
118
119 // Scan the function body for instructions that may read or write memory.
120 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
121 Instruction *I = &*II;
122
123 // Some instructions can be ignored even if they read or write memory.
124 // Detect these now, skipping to the next instruction if one is found.
125 CallSite CS = CallSite::get(I);
126 if (CS.getInstruction()) {
127 // Ignore calls to functions in the same SCC.
128 if (SCCNodes.count(CG[CS.getCalledFunction()]))
129 continue;
130 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
131 // Ignore loads from local memory.
132 if (PointsToLocalMemory(LI->getPointerOperand()))
133 continue;
134 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
135 // Ignore stores to local memory.
136 if (PointsToLocalMemory(SI->getPointerOperand()))
137 continue;
138 }
139
140 // Any remaining instructions need to be taken seriously! Check if they
141 // read or write memory.
142 if (I->mayWriteToMemory())
143 // Writes memory. Just give up.
144 return false;
145 // If this instruction may read memory, remember that.
146 ReadsMemory |= I->mayReadFromMemory();
147 }
148 }
149
150 // Success! Functions in this SCC do not access memory, or only read memory.
151 // Give them the appropriate attribute.
152 bool MadeChange = false;
153 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
154 Function *F = SCC[i]->getFunction();
155
156 if (F->doesNotAccessMemory())
157 // Already perfect!
158 continue;
159
160 if (F->onlyReadsMemory() && ReadsMemory)
161 // No change.
162 continue;
163
164 MadeChange = true;
165
166 // Clear out any existing attributes.
167 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
168
169 // Add in the new attribute.
170 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone);
171
172 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000173 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000174 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000175 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000176 }
177
178 return MadeChange;
179}
180
Duncan Sands9e89ba32008-12-31 16:14:43 +0000181/// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
182bool FunctionAttrs::AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC) {
183 bool Changed = false;
184
185 // Check each function in turn, determining which pointer arguments are not
186 // captured.
187 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
188 Function *F = SCC[i]->getFunction();
189
190 if (F == 0)
191 // External node - skip it;
192 continue;
193
194 // Definitions with weak linkage may be overridden at linktime with
195 // something that writes memory, so treat them like declarations.
196 if (F->isDeclaration() || F->mayBeOverridden())
197 continue;
198
199 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A)
Duncan Sands17da06f2008-12-31 18:08:59 +0000200 if (isa<PointerType>(A->getType()) && !A->hasNoCaptureAttr() &&
Duncan Sands8556d2a2009-01-18 12:19:30 +0000201 !PointerMayBeCaptured(A, true)) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000202 A->addAttr(Attribute::NoCapture);
Nick Lewycky6b056862009-01-02 03:46:56 +0000203 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000204 Changed = true;
205 }
206 }
207
208 return Changed;
209}
210
211bool FunctionAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
212 bool Changed = AddReadAttrs(SCC);
213 Changed |= AddNoCaptureAttrs(SCC);
214 return Changed;
215}