blob: 4c6158c65e8fc9dec5bae64fe877d1ab8ec80148 [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);
38 };
39}
40
41char AddReadAttrs::ID = 0;
42static RegisterPass<AddReadAttrs>
43X("addreadattrs", "Mark functions readnone/readonly");
44
45Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); }
46
47
48bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
Duncan Sandsf726bc72008-09-29 14:59:04 +000049 SmallPtrSet<CallGraphNode *, 8> SCCNodes;
Duncan Sands58f38772008-09-19 08:17:05 +000050 CallGraph &CG = getAnalysis<CallGraph>();
51
Duncan Sandsf726bc72008-09-29 14:59:04 +000052 // Fill SCCNodes with the elements of the SCC. Used for quickly
53 // looking up whether a given CallGraphNode is in this SCC.
54 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
55 SCCNodes.insert(SCC[i]);
56
Duncan Sands120f8f92008-09-29 13:35:31 +000057 // Check if any of the functions in the SCC read or write memory. If they
58 // write memory then they can't be marked readnone or readonly.
Duncan Sands58f38772008-09-19 08:17:05 +000059 bool ReadsMemory = false;
60 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
61 Function *F = SCC[i]->getFunction();
62
63 if (F == 0)
Duncan Sands120f8f92008-09-29 13:35:31 +000064 // External node - may write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000065 return false;
66
67 if (F->doesNotAccessMemory())
68 // Already perfect!
69 continue;
70
71 // Definitions with weak linkage may be overridden at linktime with
72 // something that writes memory, so treat them like declarations.
Duncan Sandseb3f45f2008-09-29 11:25:42 +000073 if (F->isDeclaration() || F->mayBeOverridden()) {
Duncan Sands58f38772008-09-19 08:17:05 +000074 if (!F->onlyReadsMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000075 // May write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000076 return false;
77
78 ReadsMemory = true;
79 continue;
80 }
81
Duncan Sands42b0e852008-09-20 16:45:58 +000082 // Scan the function body for instructions that may read or write memory.
Duncan Sands58f38772008-09-19 08:17:05 +000083 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
Duncan Sands42b0e852008-09-20 16:45:58 +000084 CallSite CS = CallSite::get(&*II);
85
86 // Ignore calls to functions in the same SCC.
Duncan Sandsf726bc72008-09-29 14:59:04 +000087 if (CS.getInstruction() && SCCNodes.count(CG[CS.getCalledFunction()]))
Duncan Sands42b0e852008-09-20 16:45:58 +000088 continue;
89
90 if (II->mayWriteToMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000091 // Writes memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000092 return false;
Duncan Sands120f8f92008-09-29 13:35:31 +000093
Duncan Sands42b0e852008-09-20 16:45:58 +000094 ReadsMemory |= II->mayReadFromMemory();
Duncan Sands58f38772008-09-19 08:17:05 +000095 }
96 }
97
98 // Success! Functions in this SCC do not access memory, or only read memory.
99 // Give them the appropriate attribute.
100 bool MadeChange = false;
101 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
102 Function *F = SCC[i]->getFunction();
103
104 if (F->doesNotAccessMemory())
105 // Already perfect!
106 continue;
107
108 if (F->onlyReadsMemory() && ReadsMemory)
109 // No change.
110 continue;
111
112 MadeChange = true;
113
114 // Clear out any existing attributes.
Devang Patelf2a4a922008-09-26 22:53:05 +0000115 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000116
117 // Add in the new attribute.
Devang Patelf2a4a922008-09-26 22:53:05 +0000118 F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000119
120 if (ReadsMemory)
121 NumReadOnly++;
122 else
123 NumReadNone++;
124 }
125
126 return MadeChange;
127}