Experiments show that looking through phi nodes
[oota-llvm.git] / lib / Transforms / IPO / AddReadAttrs.cpp
1 //===- 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/GlobalVariable.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Analysis/CallGraph.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/InstIterator.h"
27 using namespace llvm;
28
29 STATISTIC(NumReadNone, "Number of functions marked readnone");
30 STATISTIC(NumReadOnly, "Number of functions marked readonly");
31
32 namespace {
33   struct VISIBILITY_HIDDEN AddReadAttrs : public CallGraphSCCPass {
34     static char ID; // Pass identification, replacement for typeid
35     AddReadAttrs() : CallGraphSCCPass(&ID) {}
36
37     // runOnSCC - Analyze the SCC, performing the transformation if possible.
38     bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
39
40     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41       AU.setPreservesCFG();
42       CallGraphSCCPass::getAnalysisUsage(AU);
43     }
44
45     bool PointsToLocalMemory(Value *V);
46   };
47 }
48
49 char AddReadAttrs::ID = 0;
50 static RegisterPass<AddReadAttrs>
51 X("addreadattrs", "Mark functions readnone/readonly");
52
53 Pass *llvm::createAddReadAttrsPass() { return new AddReadAttrs(); }
54
55
56 /// PointsToLocalMemory - Returns whether the given pointer value points to
57 /// memory that is local to the function.  Global constants are considered
58 /// local to all functions.
59 bool AddReadAttrs::PointsToLocalMemory(Value *V) {
60   V = V->getUnderlyingObject();
61   // An alloca instruction defines local memory.
62   if (isa<AllocaInst>(V))
63     return true;
64   // A global constant counts as local memory for our purposes.
65   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
66     return GV->isConstant();
67   // Could look through phi nodes and selects here, but it doesn't seem
68   // to be useful in practice.
69   return false;
70 }
71
72 bool AddReadAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
73   SmallPtrSet<CallGraphNode *, 8> SCCNodes;
74   CallGraph &CG = getAnalysis<CallGraph>();
75
76   // Fill SCCNodes with the elements of the SCC.  Used for quickly
77   // looking up whether a given CallGraphNode is in this SCC.
78   for (unsigned i = 0, e = SCC.size(); i != e; ++i)
79     SCCNodes.insert(SCC[i]);
80
81   // Check if any of the functions in the SCC read or write memory.  If they
82   // write memory then they can't be marked readnone or readonly.
83   bool ReadsMemory = false;
84   for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
85     Function *F = SCC[i]->getFunction();
86
87     if (F == 0)
88       // External node - may write memory.  Just give up.
89       return false;
90
91     if (F->doesNotAccessMemory())
92       // Already perfect!
93       continue;
94
95     // Definitions with weak linkage may be overridden at linktime with
96     // something that writes memory, so treat them like declarations.
97     if (F->isDeclaration() || F->mayBeOverridden()) {
98       if (!F->onlyReadsMemory())
99         // May write memory.  Just give up.
100         return false;
101
102       ReadsMemory = true;
103       continue;
104     }
105
106     // Scan the function body for instructions that may read or write memory.
107     for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
108       Instruction *I = &*II;
109
110       // Some instructions can be ignored even if they read or write memory.
111       // Detect these now, skipping to the next instruction if one is found.
112       CallSite CS = CallSite::get(I);
113       if (CS.getInstruction()) {
114         // Ignore calls to functions in the same SCC.
115         if (SCCNodes.count(CG[CS.getCalledFunction()]))
116           continue;
117       } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
118         // Ignore loads from local memory.
119         if (PointsToLocalMemory(LI->getPointerOperand()))
120           continue;
121       } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
122         // Ignore stores to local memory.
123         if (PointsToLocalMemory(SI->getPointerOperand()))
124           continue;
125       }
126
127       // Any remaining instructions need to be taken seriously!  Check if they
128       // read or write memory.
129       if (I->mayWriteToMemory())
130         // Writes memory.  Just give up.
131         return false;
132       // If this instruction may read memory, remember that.
133       ReadsMemory |= I->mayReadFromMemory();
134     }
135   }
136
137   // Success!  Functions in this SCC do not access memory, or only read memory.
138   // Give them the appropriate attribute.
139   bool MadeChange = false;
140   for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
141     Function *F = SCC[i]->getFunction();
142
143     if (F->doesNotAccessMemory())
144       // Already perfect!
145       continue;
146
147     if (F->onlyReadsMemory() && ReadsMemory)
148       // No change.
149       continue;
150
151     MadeChange = true;
152
153     // Clear out any existing attributes.
154     F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
155
156     // Add in the new attribute.
157     F->addAttribute(~0, ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
158
159     if (ReadsMemory)
160       NumReadOnly++;
161     else
162       NumReadNone++;
163   }
164
165   return MadeChange;
166 }