blob: b70bdaf763a4aaa3febf417ae0905e857aaa37f8 [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"
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
Duncan Sands120f8f92008-09-29 13:35:31 +000050 // Check if any of the functions in the SCC read or write memory. If they
51 // write memory then they can't be marked readnone or readonly.
Duncan Sands58f38772008-09-19 08:17:05 +000052 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)
Duncan Sands120f8f92008-09-29 13:35:31 +000057 // External node - may write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000058 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.
Duncan Sandseb3f45f2008-09-29 11:25:42 +000066 if (F->isDeclaration() || F->mayBeOverridden()) {
Duncan Sands58f38772008-09-19 08:17:05 +000067 if (!F->onlyReadsMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000068 // May write memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000069 return false;
70
71 ReadsMemory = true;
72 continue;
73 }
74
Duncan Sands42b0e852008-09-20 16:45:58 +000075 // Scan the function body for instructions that may read or write memory.
Duncan Sands58f38772008-09-19 08:17:05 +000076 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
Duncan Sands42b0e852008-09-20 16:45:58 +000077 CallSite CS = CallSite::get(&*II);
78
79 // Ignore calls to functions in the same SCC.
80 if (CS.getInstruction() &&
81 std::find(SCC.begin(), SCC.end(), CG[CS.getCalledFunction()]) !=
82 SCC.end())
83 continue;
84
85 if (II->mayWriteToMemory())
Duncan Sands120f8f92008-09-29 13:35:31 +000086 // Writes memory. Just give up.
Duncan Sands58f38772008-09-19 08:17:05 +000087 return false;
Duncan Sands120f8f92008-09-29 13:35:31 +000088
Duncan Sands42b0e852008-09-20 16:45:58 +000089 ReadsMemory |= II->mayReadFromMemory();
Duncan Sands58f38772008-09-19 08:17:05 +000090 }
91 }
92
93 // Success! Functions in this SCC do not access memory, or only read memory.
94 // Give them the appropriate attribute.
95 bool MadeChange = false;
96 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
97 Function *F = SCC[i]->getFunction();
98
99 if (F->doesNotAccessMemory())
100 // Already perfect!
101 continue;
102
103 if (F->onlyReadsMemory() && ReadsMemory)
104 // No change.
105 continue;
106
107 MadeChange = true;
108
109 // Clear out any existing attributes.
Devang Patelf2a4a922008-09-26 22:53:05 +0000110 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000111
112 // Add in the new attribute.
Devang Patelf2a4a922008-09-26 22:53:05 +0000113 F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands58f38772008-09-19 08:17:05 +0000114
115 if (ReadsMemory)
116 NumReadOnly++;
117 else
118 NumReadNone++;
119 }
120
121 return MadeChange;
122}