Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 1 | //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This simple pass provides alias and mod/ref information for global values |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 11 | // that do not have their address taken, and keeps track of whether functions |
| 12 | // read or write memory (are "pure"). For this simple (but very common) case, |
| 13 | // we can provide pretty accurate and useful information. |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
| 23 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/InstIterator.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/ADT/SCCIterator.h" |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 28 | #include <set> |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | Statistic<> |
| 33 | NumNonAddrTakenGlobalVars("globalsmodref-aa", |
| 34 | "Number of global vars without address taken"); |
| 35 | Statistic<> |
| 36 | NumNonAddrTakenFunctions("globalsmodref-aa", |
| 37 | "Number of functions without address taken"); |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 38 | Statistic<> |
| 39 | NumNoMemFunctions("globalsmodref-aa", |
| 40 | "Number of functions that do not access memory"); |
| 41 | Statistic<> |
| 42 | NumReadMemFunctions("globalsmodref-aa", |
| 43 | "Number of functions that only read memory"); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 44 | |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 45 | /// FunctionRecord - One instance of this structure is stored for every |
| 46 | /// function in the program. Later, the entries for these functions are |
| 47 | /// removed if the function is found to call an external function (in which |
| 48 | /// case we know nothing about it. |
| 49 | struct FunctionRecord { |
| 50 | /// GlobalInfo - Maintain mod/ref info for all of the globals without |
| 51 | /// addresses taken that are read or written (transitively) by this |
| 52 | /// function. |
| 53 | std::map<GlobalValue*, unsigned> GlobalInfo; |
| 54 | |
| 55 | unsigned getInfoForGlobal(GlobalValue *GV) const { |
| 56 | std::map<GlobalValue*, unsigned>::const_iterator I = GlobalInfo.find(GV); |
| 57 | if (I != GlobalInfo.end()) |
| 58 | return I->second; |
| 59 | return 0; |
| 60 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 61 | |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 62 | /// FunctionEffect - Capture whether or not this function reads or writes to |
| 63 | /// ANY memory. If not, we can do a lot of aggressive analysis on it. |
| 64 | unsigned FunctionEffect; |
Chris Lattner | 105d26a | 2004-07-27 07:46:26 +0000 | [diff] [blame] | 65 | |
| 66 | FunctionRecord() : FunctionEffect(0) {} |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /// GlobalsModRef - The actual analysis pass. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 70 | class GlobalsModRef : public ModulePass, public AliasAnalysis { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 71 | /// NonAddressTakenGlobals - The globals that do not have their addresses |
| 72 | /// taken. |
| 73 | std::set<GlobalValue*> NonAddressTakenGlobals; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 74 | |
| 75 | /// FunctionInfo - For each function, keep track of what globals are |
| 76 | /// modified or read. |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 77 | std::map<Function*, FunctionRecord> FunctionInfo; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 78 | |
| 79 | public: |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 80 | bool runOnModule(Module &M) { |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 81 | InitializeAliasAnalysis(this); // set up super class |
| 82 | AnalyzeGlobals(M); // find non-addr taken globals |
| 83 | AnalyzeCallGraph(getAnalysis<CallGraph>(), M); // Propagate on CG |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 88 | AliasAnalysis::getAnalysisUsage(AU); |
| 89 | AU.addRequired<CallGraph>(); |
| 90 | AU.setPreservesAll(); // Does not transform code |
| 91 | } |
| 92 | |
| 93 | //------------------------------------------------ |
| 94 | // Implement the AliasAnalysis API |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 95 | // |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 96 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 97 | const Value *V2, unsigned V2Size); |
| 98 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
Reid Spencer | 4a7ebfa | 2004-12-07 08:11:24 +0000 | [diff] [blame] | 99 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 100 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 101 | } |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 102 | bool hasNoModRefInfoForCalls() const { return false; } |
| 103 | |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 104 | /// getModRefBehavior - Return the behavior of the specified function if |
| 105 | /// called from the specified call site. The call site may be null in which |
| 106 | /// case the most generic behavior of this function should be returned. |
Chris Lattner | 41925f8 | 2004-12-17 17:12:24 +0000 | [diff] [blame] | 107 | virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, |
| 108 | std::vector<PointerAccessInfo> *Info) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 109 | if (FunctionRecord *FR = getFunctionInfo(F)) |
| 110 | if (FR->FunctionEffect == 0) |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 111 | return DoesNotAccessMemory; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 112 | else if ((FR->FunctionEffect & Mod) == 0) |
| 113 | return OnlyReadsMemory; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 114 | return AliasAnalysis::getModRefBehavior(F, CS, Info); |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 117 | virtual void deleteValue(Value *V); |
| 118 | virtual void copyValue(Value *From, Value *To); |
| 119 | |
| 120 | private: |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 121 | /// getFunctionInfo - Return the function info for the function, or null if |
| 122 | /// the function calls an external function (in which case we don't have |
| 123 | /// anything useful to say about it). |
| 124 | FunctionRecord *getFunctionInfo(Function *F) { |
| 125 | std::map<Function*, FunctionRecord>::iterator I = FunctionInfo.find(F); |
| 126 | if (I != FunctionInfo.end()) |
| 127 | return &I->second; |
| 128 | return 0; |
| 129 | } |
| 130 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 131 | void AnalyzeGlobals(Module &M); |
| 132 | void AnalyzeCallGraph(CallGraph &CG, Module &M); |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 133 | void AnalyzeSCC(std::vector<CallGraphNode *> &SCC); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 134 | bool AnalyzeUsesOfGlobal(Value *V, std::vector<Function*> &Readers, |
| 135 | std::vector<Function*> &Writers); |
| 136 | }; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 138 | RegisterOpt<GlobalsModRef> X("globalsmodref-aa", |
| 139 | "Simple mod/ref analysis for globals"); |
| 140 | RegisterAnalysisGroup<AliasAnalysis, GlobalsModRef> Y; |
| 141 | } |
| 142 | |
| 143 | Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } |
| 144 | |
| 145 | |
| 146 | /// AnalyzeGlobalUses - Scan through the users of all of the internal |
| 147 | /// GlobalValue's in the program. If none of them have their "Address taken" |
| 148 | /// (really, their address passed to something nontrivial), record this fact, |
| 149 | /// and record the functions that they are used directly in. |
| 150 | void GlobalsModRef::AnalyzeGlobals(Module &M) { |
| 151 | std::vector<Function*> Readers, Writers; |
| 152 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 153 | if (I->hasInternalLinkage()) { |
| 154 | if (!AnalyzeUsesOfGlobal(I, Readers, Writers)) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 155 | // Remember that we are tracking this global. |
| 156 | NonAddressTakenGlobals.insert(I); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 157 | ++NumNonAddrTakenFunctions; |
| 158 | } |
| 159 | Readers.clear(); Writers.clear(); |
| 160 | } |
| 161 | |
Chris Lattner | f5eaf3c | 2005-03-23 23:51:12 +0000 | [diff] [blame] | 162 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 163 | I != E; ++I) |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 164 | if (I->hasInternalLinkage()) { |
| 165 | if (!AnalyzeUsesOfGlobal(I, Readers, Writers)) { |
| 166 | // Remember that we are tracking this global, and the mod/ref fns |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 167 | NonAddressTakenGlobals.insert(I); |
| 168 | for (unsigned i = 0, e = Readers.size(); i != e; ++i) |
| 169 | FunctionInfo[Readers[i]].GlobalInfo[I] |= Ref; |
| 170 | |
| 171 | if (!I->isConstant()) // No need to keep track of writers to constants |
| 172 | for (unsigned i = 0, e = Writers.size(); i != e; ++i) |
| 173 | FunctionInfo[Writers[i]].GlobalInfo[I] |= Mod; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 174 | ++NumNonAddrTakenGlobalVars; |
| 175 | } |
| 176 | Readers.clear(); Writers.clear(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /// AnalyzeUsesOfGlobal - Look at all of the users of the specified global value |
| 181 | /// derived pointer. If this is used by anything complex (i.e., the address |
| 182 | /// escapes), return true. Also, while we are at it, keep track of those |
| 183 | /// functions that read and write to the value. |
| 184 | bool GlobalsModRef::AnalyzeUsesOfGlobal(Value *V, |
| 185 | std::vector<Function*> &Readers, |
| 186 | std::vector<Function*> &Writers) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 187 | if (!isa<PointerType>(V->getType())) return true; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 188 | |
| 189 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 190 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 191 | Readers.push_back(LI->getParent()->getParent()); |
| 192 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 193 | if (V == SI->getOperand(0)) return true; // Storing the pointer |
| 194 | Writers.push_back(SI->getParent()->getParent()); |
| 195 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) { |
| 196 | if (AnalyzeUsesOfGlobal(GEP, Readers, Writers)) return true; |
| 197 | } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
| 198 | // Make sure that this is just the function being called, not that it is |
| 199 | // passing into the function. |
| 200 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 201 | if (CI->getOperand(i) == V) return true; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 202 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
| 203 | // Make sure that this is just the function being called, not that it is |
| 204 | // passing into the function. |
| 205 | for (unsigned i = 3, e = II->getNumOperands(); i != e; ++i) |
| 206 | if (II->getOperand(i) == V) return true; |
| 207 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { |
| 208 | if (CE->getOpcode() == Instruction::GetElementPtr || |
| 209 | CE->getOpcode() == Instruction::Cast) { |
| 210 | if (AnalyzeUsesOfGlobal(CE, Readers, Writers)) |
| 211 | return true; |
| 212 | } else { |
| 213 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 214 | } |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 215 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*UI)) { |
| 216 | if (AnalyzeUsesOfGlobal(GV, Readers, Writers)) return true; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 217 | } else { |
| 218 | return true; |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /// AnalyzeCallGraph - At this point, we know the functions where globals are |
| 224 | /// immediately stored to and read from. Propagate this information up the call |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 225 | /// graph to all callers and compute the mod/ref info for all memory for each |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 226 | /// function. |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 227 | void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 228 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 229 | // visit all callees before callers (leaf-first). |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 230 | for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); I!=E; ++I) |
Chris Lattner | 105d26a | 2004-07-27 07:46:26 +0000 | [diff] [blame] | 231 | if ((*I).size() != 1) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 232 | AnalyzeSCC(*I); |
Chris Lattner | 105d26a | 2004-07-27 07:46:26 +0000 | [diff] [blame] | 233 | } else if (Function *F = (*I)[0]->getFunction()) { |
| 234 | if (!F->isExternal()) { |
| 235 | // Nonexternal function. |
| 236 | AnalyzeSCC(*I); |
| 237 | } else { |
| 238 | // Otherwise external function. Handle intrinsics and other special |
| 239 | // cases here. |
| 240 | if (getAnalysis<AliasAnalysis>().doesNotAccessMemory(F)) |
| 241 | // If it does not access memory, process the function, causing us to |
| 242 | // realize it doesn't do anything (the body is empty). |
| 243 | AnalyzeSCC(*I); |
| 244 | else { |
| 245 | // Otherwise, don't process it. This will cause us to conservatively |
| 246 | // assume the worst. |
| 247 | } |
| 248 | } |
| 249 | } else { |
| 250 | // Do not process the external node, assume the worst. |
| 251 | } |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 252 | } |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 253 | |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 254 | void GlobalsModRef::AnalyzeSCC(std::vector<CallGraphNode *> &SCC) { |
| 255 | assert(!SCC.empty() && "SCC with no functions?"); |
| 256 | FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; |
| 257 | |
| 258 | bool CallsExternal = false; |
| 259 | unsigned FunctionEffect = 0; |
| 260 | |
| 261 | // Collect the mod/ref properties due to called functions. We only compute |
| 262 | // one mod-ref set |
| 263 | for (unsigned i = 0, e = SCC.size(); i != e && !CallsExternal; ++i) |
| 264 | for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); |
| 265 | CI != E; ++CI) |
| 266 | if (Function *Callee = (*CI)->getFunction()) { |
| 267 | if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { |
| 268 | // Propagate function effect up. |
| 269 | FunctionEffect |= CalleeFR->FunctionEffect; |
| 270 | |
| 271 | // Incorporate callee's effects on globals into our info. |
| 272 | for (std::map<GlobalValue*, unsigned>::iterator GI = |
| 273 | CalleeFR->GlobalInfo.begin(), E = CalleeFR->GlobalInfo.end(); |
| 274 | GI != E; ++GI) |
| 275 | FR.GlobalInfo[GI->first] |= GI->second; |
| 276 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 277 | } else { |
Chris Lattner | ec6518d | 2005-03-23 23:49:47 +0000 | [diff] [blame] | 278 | // Okay, if we can't say anything about it, maybe some other alias |
| 279 | // analysis can. |
| 280 | ModRefBehavior MRB = |
| 281 | AliasAnalysis::getModRefBehavior(Callee, CallSite()); |
| 282 | if (MRB != DoesNotAccessMemory) { |
Chris Lattner | 62da315 | 2005-03-24 02:41:19 +0000 | [diff] [blame] | 283 | // FIXME: could make this more aggressive for functions that just |
| 284 | // read memory. We should just say they read all globals. |
| 285 | CallsExternal = true; |
| 286 | break; |
Chris Lattner | ec6518d | 2005-03-23 23:49:47 +0000 | [diff] [blame] | 287 | } |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 288 | } |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 289 | } else { |
| 290 | CallsExternal = true; |
| 291 | break; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 293 | |
| 294 | // If this SCC calls an external function, we can't say anything about it, so |
| 295 | // remove all SCC functions from the FunctionInfo map. |
| 296 | if (CallsExternal) { |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 297 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 298 | FunctionInfo.erase(SCC[i]->getFunction()); |
| 299 | return; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 300 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 301 | |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 302 | // Otherwise, unless we already know that this function mod/refs memory, scan |
| 303 | // the function bodies to see if there are any explicit loads or stores. |
| 304 | if (FunctionEffect != ModRef) { |
| 305 | for (unsigned i = 0, e = SCC.size(); i != e && FunctionEffect != ModRef;++i) |
| 306 | for (inst_iterator II = inst_begin(SCC[i]->getFunction()), |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 307 | E = inst_end(SCC[i]->getFunction()); |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 308 | II != E && FunctionEffect != ModRef; ++II) |
| 309 | if (isa<LoadInst>(*II)) |
| 310 | FunctionEffect |= Ref; |
| 311 | else if (isa<StoreInst>(*II)) |
| 312 | FunctionEffect |= Mod; |
Chris Lattner | 3fe4d3c | 2005-04-22 05:36:59 +0000 | [diff] [blame^] | 313 | else if (isa<MallocInst>(*II) || isa<FreeInst>(*II)) |
| 314 | FunctionEffect |= ModRef; |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if ((FunctionEffect & Mod) == 0) |
| 318 | ++NumReadMemFunctions; |
| 319 | if (FunctionEffect == 0) |
| 320 | ++NumNoMemFunctions; |
| 321 | FR.FunctionEffect = FunctionEffect; |
| 322 | |
| 323 | // Finally, now that we know the full effect on this SCC, clone the |
| 324 | // information to each function in the SCC. |
| 325 | for (unsigned i = 1, e = SCC.size(); i != e; ++i) |
| 326 | FunctionInfo[SCC[i]->getFunction()] = FR; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | /// getUnderlyingObject - This traverses the use chain to figure out what object |
| 332 | /// the specified value points to. If the value points to, or is derived from, |
| 333 | /// a global object, return it. |
| 334 | static const GlobalValue *getUnderlyingObject(const Value *V) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 335 | if (!isa<PointerType>(V->getType())) return 0; |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 336 | |
| 337 | // If we are at some type of object... return it. |
| 338 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 340 | // Traverse through different addressing mechanisms... |
| 341 | if (const Instruction *I = dyn_cast<Instruction>(V)) { |
| 342 | if (isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 343 | return getUnderlyingObject(I->getOperand(0)); |
| 344 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 345 | if (CE->getOpcode() == Instruction::Cast || |
| 346 | CE->getOpcode() == Instruction::GetElementPtr) |
| 347 | return getUnderlyingObject(CE->getOperand(0)); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /// alias - If one of the pointers is to a global that we are tracking, and the |
| 353 | /// other is some random pointer, we know there cannot be an alias, because the |
| 354 | /// address of the global isn't taken. |
| 355 | AliasAnalysis::AliasResult |
| 356 | GlobalsModRef::alias(const Value *V1, unsigned V1Size, |
| 357 | const Value *V2, unsigned V2Size) { |
| 358 | GlobalValue *GV1 = const_cast<GlobalValue*>(getUnderlyingObject(V1)); |
| 359 | GlobalValue *GV2 = const_cast<GlobalValue*>(getUnderlyingObject(V2)); |
| 360 | |
| 361 | // If the global's address is taken, pretend we don't know it's a pointer to |
| 362 | // the global. |
| 363 | if (GV1 && !NonAddressTakenGlobals.count(GV1)) GV1 = 0; |
| 364 | if (GV2 && !NonAddressTakenGlobals.count(GV2)) GV2 = 0; |
| 365 | |
| 366 | if ((GV1 || GV2) && GV1 != GV2) |
| 367 | return NoAlias; |
| 368 | |
| 369 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
| 370 | } |
| 371 | |
| 372 | AliasAnalysis::ModRefResult |
| 373 | GlobalsModRef::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 374 | unsigned Known = ModRef; |
| 375 | |
| 376 | // If we are asking for mod/ref info of a direct call with a pointer to a |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 377 | // global we are tracking, return information if we have it. |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 378 | if (GlobalValue *GV = const_cast<GlobalValue*>(getUnderlyingObject(P))) |
| 379 | if (GV->hasInternalLinkage()) |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 380 | if (Function *F = CS.getCalledFunction()) |
| 381 | if (NonAddressTakenGlobals.count(GV)) |
| 382 | if (FunctionRecord *FR = getFunctionInfo(F)) |
| 383 | Known = FR->getInfoForGlobal(GV); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 384 | |
| 385 | if (Known == NoModRef) |
| 386 | return NoModRef; // No need to query other mod/ref analyses |
| 387 | return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, P, Size)); |
| 388 | } |
| 389 | |
| 390 | |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | // Methods to update the analysis as a result of the client transformation. |
| 393 | // |
| 394 | void GlobalsModRef::deleteValue(Value *V) { |
Chris Lattner | fe98f27 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 395 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) |
| 396 | NonAddressTakenGlobals.erase(GV); |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void GlobalsModRef::copyValue(Value *From, Value *To) { |
Chris Lattner | 3b04a8a | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 400 | } |