blob: 7af071c40bdf7c7b77dba7ea1153ae1f0e9c4194 [file] [log] [blame]
Duncan Sands58f38772008-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"
Duncan Sandsf726bc72008-09-29 14:59:04 +000022#include "llvm/ADT/SmallPtrSet.h"
Duncan Sands58f38772008-09-19 08:17:05 +000023#include "llvm/ADT/Statistic.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/InstIterator.h"
26using namespace llvm;
27
28STATISTIC(NumReadNone, "Number of functions marked readnone");
29STATISTIC(NumReadOnly, "Number of functions marked readonly");
30
31namespace {
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 Lopese7235932008-09-30 18:34:38 +000038
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesCFG();
41 CallGraphSCCPass::getAnalysisUsage(AU);
42 }
Duncan Sands58f38772008-09-19 08:17:05 +000043 };
44}
45
46char AddReadAttrs::ID = 0;
47static RegisterPass<AddReadAttrs>
48X("addreadattrs", "Mark functions readnone/readonly");
49
50Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); }
51
52
53bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
Duncan Sandsf726bc72008-09-29 14:59:04 +000054 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Duncan Sands58f38772008-09-19 08:17:05 +000055 CallGraph &CG = getAnalysis<CallGraph>();
56
Duncan Sandsf726bc72008-09-29 14:59:04 +000057 // 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 Sands120f8f92008-09-29 13:35:31 +000062 // 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 Sands58f38772008-09-19 08:17:05 +000064 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 Sands120f8f92008-09-29 13:35:31 +000069 // External node - may write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000070 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 Sandseb3f45f2008-09-29 11:25:42 +000078 if (F->isDeclaration() || F->mayBeOverridden()) {
Duncan Sands58f38772008-09-19 08:17:05 +000079 if (!F->onlyReadsMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000080 // May write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000081 return false;
82
83 ReadsMemory = true;
84 continue;
85 }
86
Duncan Sands42b0e852008-09-20 16:45:58 +000087 // Scan the function body for instructions that may read or write memory.
Duncan Sands58f38772008-09-19 08:17:05 +000088 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
Duncan Sands42b0e852008-09-20 16:45:58 +000089 CallSite CS = CallSite::get(&*II);
90
91 // Ignore calls to functions in the same SCC.
Duncan Sandsf726bc72008-09-29 14:59:04 +000092 if (CS.getInstruction() && SCCNodes.count(CG[CS.getCalledFunction()]))
Duncan Sands42b0e852008-09-20 16:45:58 +000093 continue;
94
95 if (II->mayWriteToMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000096 // Writes memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000097 return false;
Duncan Sands120f8f92008-09-29 13:35:31 +000098
Duncan Sands42b0e852008-09-20 16:45:58 +000099 ReadsMemory |= II->mayReadFromMemory();
Duncan Sands58f38772008-09-19 08:17:05 +0000100 }
101 }
102
103 // Success! Functions in this SCC do not access memory, or only read memory.
104 // Give them the appropriate attribute.
105 bool MadeChange = false;
106 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
107 Function *F = SCC[i]->getFunction();
108
109 if (F->doesNotAccessMemory())
110 // Already perfect!
111 continue;
112
113 if (F->onlyReadsMemory() && ReadsMemory)
114 // No change.
115 continue;
116
117 MadeChange = true;
118
119 // Clear out any existing attributes.
Devang Patelf2a4a922008-09-26 22:53:05 +0000120 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000121
122 // Add in the new attribute.
Devang Patelf2a4a922008-09-26 22:53:05 +0000123 F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000124
125 if (ReadsMemory)
126 NumReadOnly++;
127 else
128 NumReadNone++;
129 }
130
131 return MadeChange;
132}