blob: 1052e06b72f76d3318025a2482ee37bec06def83 [file] [log] [blame]
Duncan Sands9f07a292008-09-19 08:17:05 +00001//===- 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"
25using namespace llvm;
26
27STATISTIC(NumReadNone, "Number of functions marked readnone");
28STATISTIC(NumReadOnly, "Number of functions marked readonly");
29
30namespace {
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
40char AddReadAttrs::ID = 0;
41static RegisterPass<AddReadAttrs>
42X("addreadattrs", "Mark functions readnone/readonly");
43
44Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); }
45
46
47bool 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)
57 // May write memory.
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
75 // Scan the function body for explicit loads and stores, or calls to
76 // functions that may read or write memory.
77 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
78 Instruction *I = &*II;
79 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
80 if (LI->isVolatile())
81 // Volatile loads may have side-effects, so treat them as writing
82 // memory.
83 return false;
84 ReadsMemory = true;
85 } else if (isa<StoreInst>(I) || isa<MallocInst>(I) || isa<FreeInst>(I)) {
86 // Writes memory.
87 return false;
88 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
89 CallSite CS(I);
90
91 if (std::find(SCC.begin(), SCC.end(), CG[CS.getCalledFunction()]) !=
92 SCC.end())
93 // The callee is inside our current SCC - ignore it.
94 continue;
95
96 if (!CS.onlyReadsMemory())
97 // May write memory.
98 return false;
99
100 if (!CS.doesNotAccessMemory())
101 ReadsMemory = true;
102 }
103 }
104 }
105
106 // Success! Functions in this SCC do not access memory, or only read memory.
107 // Give them the appropriate attribute.
108 bool MadeChange = false;
109 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
110 Function *F = SCC[i]->getFunction();
111
112 if (F->doesNotAccessMemory())
113 // Already perfect!
114 continue;
115
116 if (F->onlyReadsMemory() && ReadsMemory)
117 // No change.
118 continue;
119
120 MadeChange = true;
121
122 // Clear out any existing attributes.
123 F->removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
124
125 // Add in the new attribute.
126 F->addParamAttr(0, ReadsMemory ? ParamAttr::ReadOnly : ParamAttr::ReadNone);
127
128 if (ReadsMemory)
129 NumReadOnly++;
130 else
131 NumReadNone++;
132 }
133
134 return MadeChange;
135}