Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 1 | //===- GlobalsModRef.cpp - Simple Mod/Ref Analysis for Globals ------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 26dff50 | 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 | 3a353e8 | 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 | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SCCIterator.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
| 21 | #include "llvm/Analysis/CallGraph.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/MemoryBuiltins.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 26 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
| 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 31 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 32 | #include <list> |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 33 | #include <set> |
| 34 | using namespace llvm; |
| 35 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "globalsmodref-aa" |
| 37 | |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 38 | STATISTIC(NumNonAddrTakenGlobalVars, |
| 39 | "Number of global vars without address taken"); |
| 40 | STATISTIC(NumNonAddrTakenFunctions,"Number of functions without address taken"); |
| 41 | STATISTIC(NumNoMemFunctions, "Number of functions that do not access memory"); |
| 42 | STATISTIC(NumReadMemFunctions, "Number of functions that only read memory"); |
| 43 | STATISTIC(NumIndirectGlobalVars, "Number of indirect global objects"); |
| 44 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 45 | // An option to enable unsafe alias results from the GlobalsModRef analysis. |
| 46 | // When enabled, GlobalsModRef will provide no-alias results which in extremely |
| 47 | // rare cases may not be conservatively correct. In particular, in the face of |
| 48 | // transforms which cause assymetry between how effective GetUnderlyingObject |
| 49 | // is for two pointers, it may produce incorrect results. |
| 50 | // |
| 51 | // These unsafe results have been returned by GMR for many years without |
| 52 | // causing significant issues in the wild and so we provide a mechanism to |
| 53 | // re-enable them for users of LLVM that have a particular performance |
| 54 | // sensitivity and no known issues. The option also makes it easy to evaluate |
| 55 | // the performance impact of these results. |
| 56 | static cl::opt<bool> EnableUnsafeGlobalsModRefAliasResults( |
| 57 | "enable-unsafe-globalsmodref-alias-results", cl::init(false), cl::Hidden); |
| 58 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 59 | namespace { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 60 | /// FunctionRecord - One instance of this structure is stored for every |
| 61 | /// function in the program. Later, the entries for these functions are |
| 62 | /// removed if the function is found to call an external function (in which |
| 63 | /// case we know nothing about it. |
| 64 | struct FunctionRecord { |
| 65 | /// GlobalInfo - Maintain mod/ref info for all of the globals without |
| 66 | /// addresses taken that are read or written (transitively) by this |
| 67 | /// function. |
| 68 | std::map<const GlobalValue *, unsigned> GlobalInfo; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 69 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 70 | /// MayReadAnyGlobal - May read global variables, but it is not known which. |
| 71 | bool MayReadAnyGlobal; |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 72 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 73 | unsigned getInfoForGlobal(const GlobalValue *GV) const { |
| 74 | unsigned Effect = MayReadAnyGlobal ? AliasAnalysis::Ref : 0; |
| 75 | std::map<const GlobalValue *, unsigned>::const_iterator I = |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 76 | GlobalInfo.find(GV); |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 77 | if (I != GlobalInfo.end()) |
| 78 | Effect |= I->second; |
| 79 | return Effect; |
| 80 | } |
| 81 | |
| 82 | /// FunctionEffect - Capture whether or not this function reads or writes to |
| 83 | /// ANY memory. If not, we can do a lot of aggressive analysis on it. |
| 84 | unsigned FunctionEffect; |
| 85 | |
| 86 | FunctionRecord() : MayReadAnyGlobal(false), FunctionEffect(0) {} |
| 87 | }; |
| 88 | |
| 89 | /// GlobalsModRef - The actual analysis pass. |
| 90 | class GlobalsModRef : public ModulePass, public AliasAnalysis { |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 91 | /// The globals that do not have their addresses taken. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 92 | std::set<const GlobalValue *> NonAddressTakenGlobals; |
| 93 | |
| 94 | /// IndirectGlobals - The memory pointed to by this global is known to be |
| 95 | /// 'owned' by the global. |
| 96 | std::set<const GlobalValue *> IndirectGlobals; |
| 97 | |
| 98 | /// AllocsForIndirectGlobals - If an instruction allocates memory for an |
| 99 | /// indirect global, this map indicates which one. |
| 100 | std::map<const Value *, const GlobalValue *> AllocsForIndirectGlobals; |
| 101 | |
| 102 | /// FunctionInfo - For each function, keep track of what globals are |
| 103 | /// modified or read. |
| 104 | std::map<const Function *, FunctionRecord> FunctionInfo; |
| 105 | |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 106 | /// Handle to clear this analysis on deletion of values. |
| 107 | struct DeletionCallbackHandle; |
| 108 | |
| 109 | /// List of callbacks for globals being tracked by this analysis. Note that |
| 110 | /// these objects are quite large, but we only anticipate having one per |
| 111 | /// global tracked by this analysis. There are numerous optimizations we |
| 112 | /// could perform to the memory utilization here if this becomes a problem. |
| 113 | std::list<DeletionCallbackHandle> Handles; |
| 114 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 115 | public: |
| 116 | static char ID; |
| 117 | GlobalsModRef() : ModulePass(ID) { |
| 118 | initializeGlobalsModRefPass(*PassRegistry::getPassRegistry()); |
| 119 | } |
| 120 | |
| 121 | bool runOnModule(Module &M) override { |
| 122 | InitializeAliasAnalysis(this, &M.getDataLayout()); |
| 123 | |
| 124 | // Find non-addr taken globals. |
| 125 | AnalyzeGlobals(M); |
| 126 | |
| 127 | // Propagate on CG. |
| 128 | AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 133 | AliasAnalysis::getAnalysisUsage(AU); |
| 134 | AU.addRequired<CallGraphWrapperPass>(); |
| 135 | AU.setPreservesAll(); // Does not transform code |
| 136 | } |
| 137 | |
| 138 | //------------------------------------------------ |
| 139 | // Implement the AliasAnalysis API |
| 140 | // |
| 141 | AliasResult alias(const MemoryLocation &LocA, |
| 142 | const MemoryLocation &LocB) override; |
| 143 | ModRefResult getModRefInfo(ImmutableCallSite CS, |
| 144 | const MemoryLocation &Loc) override; |
| 145 | ModRefResult getModRefInfo(ImmutableCallSite CS1, |
| 146 | ImmutableCallSite CS2) override { |
| 147 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 148 | } |
| 149 | |
| 150 | /// getModRefBehavior - Return the behavior of the specified function if |
| 151 | /// called from the specified call site. The call site may be null in which |
| 152 | /// case the most generic behavior of this function should be returned. |
| 153 | ModRefBehavior getModRefBehavior(const Function *F) override { |
| 154 | ModRefBehavior Min = UnknownModRefBehavior; |
| 155 | |
| 156 | if (FunctionRecord *FR = getFunctionInfo(F)) { |
| 157 | if (FR->FunctionEffect == 0) |
| 158 | Min = DoesNotAccessMemory; |
| 159 | else if ((FR->FunctionEffect & Mod) == 0) |
| 160 | Min = OnlyReadsMemory; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 161 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 162 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 163 | return ModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); |
| 164 | } |
Chris Lattner | b696462 | 2004-07-27 07:46:26 +0000 | [diff] [blame] | 165 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 166 | /// getModRefBehavior - Return the behavior of the specified function if |
| 167 | /// called from the specified call site. The call site may be null in which |
| 168 | /// case the most generic behavior of this function should be returned. |
| 169 | ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override { |
| 170 | ModRefBehavior Min = UnknownModRefBehavior; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 172 | if (const Function *F = CS.getCalledFunction()) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 173 | if (FunctionRecord *FR = getFunctionInfo(F)) { |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 174 | if (FR->FunctionEffect == 0) |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 175 | Min = DoesNotAccessMemory; |
Misha Brukman | 7745116 | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 176 | else if ((FR->FunctionEffect & Mod) == 0) |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 177 | Min = OnlyReadsMemory; |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 178 | } |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 179 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 180 | return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); |
| 181 | } |
Dan Gohman | 2694e14 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 182 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 183 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 184 | /// an analysis interface through multiple inheritance. If needed, it |
| 185 | /// should override this to adjust the this pointer as needed for the |
| 186 | /// specified pass info. |
| 187 | void *getAdjustedAnalysisPointer(AnalysisID PI) override { |
| 188 | if (PI == &AliasAnalysis::ID) |
| 189 | return (AliasAnalysis *)this; |
| 190 | return this; |
| 191 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 192 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 193 | private: |
| 194 | /// getFunctionInfo - Return the function info for the function, or null if |
| 195 | /// we don't have anything useful to say about it. |
| 196 | FunctionRecord *getFunctionInfo(const Function *F) { |
| 197 | std::map<const Function *, FunctionRecord>::iterator I = |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 198 | FunctionInfo.find(F); |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 199 | if (I != FunctionInfo.end()) |
| 200 | return &I->second; |
| 201 | return nullptr; |
| 202 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 203 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 204 | void AnalyzeGlobals(Module &M); |
| 205 | void AnalyzeCallGraph(CallGraph &CG, Module &M); |
| 206 | bool AnalyzeUsesOfPointer(Value *V, std::vector<Function *> &Readers, |
| 207 | std::vector<Function *> &Writers, |
| 208 | GlobalValue *OkayStoreDest = nullptr); |
| 209 | bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); |
| 210 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 211 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 212 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 213 | char GlobalsModRef::ID = 0; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 214 | INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 215 | "Simple mod/ref analysis for globals", false, true, |
| 216 | false) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 217 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 218 | INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 219 | "Simple mod/ref analysis for globals", false, true, |
| 220 | false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 222 | Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } |
| 223 | |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 224 | struct final GlobalsModRef::DeletionCallbackHandle : CallbackVH { |
| 225 | GlobalsModRef &GMR; |
| 226 | std::list<DeletionCallbackHandle>::iterator I; |
| 227 | |
| 228 | DeletionCallbackHandle(GlobalsModRef &GMR, Value *V) |
| 229 | : CallbackVH(V), GMR(GMR) {} |
| 230 | |
| 231 | void deleted() override { |
| 232 | Value *V = getValPtr(); |
| 233 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 234 | if (GMR.NonAddressTakenGlobals.erase(GV)) { |
| 235 | // This global might be an indirect global. If so, remove it and remove |
| 236 | // any AllocRelatedValues for it. |
| 237 | if (GMR.IndirectGlobals.erase(GV)) { |
| 238 | // Remove any entries in AllocsForIndirectGlobals for this global. |
| 239 | for (std::map<const Value *, const GlobalValue *>::iterator |
| 240 | I = GMR.AllocsForIndirectGlobals.begin(), |
| 241 | E = GMR.AllocsForIndirectGlobals.end(); |
| 242 | I != E;) { |
| 243 | if (I->second == GV) { |
| 244 | GMR.AllocsForIndirectGlobals.erase(I++); |
| 245 | } else { |
| 246 | ++I; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // If this is an allocation related to an indirect global, remove it. |
| 254 | GMR.AllocsForIndirectGlobals.erase(V); |
| 255 | |
| 256 | // And clear out the handle. |
| 257 | setValPtr(nullptr); |
| 258 | GMR.Handles.erase(I); |
| 259 | // This object is now destroyed! |
| 260 | } |
| 261 | }; |
| 262 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 263 | /// AnalyzeGlobals - Scan through the users of all of the internal |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 264 | /// GlobalValue's in the program. If none of them have their "address taken" |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 265 | /// (really, their address passed to something nontrivial), record this fact, |
| 266 | /// and record the functions that they are used directly in. |
| 267 | void GlobalsModRef::AnalyzeGlobals(Module &M) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 268 | std::vector<Function *> Readers, Writers; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 269 | for (Function &F : M) |
| 270 | if (F.hasLocalLinkage()) { |
| 271 | if (!AnalyzeUsesOfPointer(&F, Readers, Writers)) { |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 272 | // Remember that we are tracking this global. |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 273 | NonAddressTakenGlobals.insert(&F); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 274 | Handles.emplace_front(*this, &F); |
| 275 | Handles.front().I = Handles.begin(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 276 | ++NumNonAddrTakenFunctions; |
| 277 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 278 | Readers.clear(); |
| 279 | Writers.clear(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 282 | for (GlobalVariable &GV : M.globals()) |
| 283 | if (GV.hasLocalLinkage()) { |
| 284 | if (!AnalyzeUsesOfPointer(&GV, Readers, Writers)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 285 | // Remember that we are tracking this global, and the mod/ref fns |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 286 | NonAddressTakenGlobals.insert(&GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 287 | Handles.emplace_front(*this, &GV); |
| 288 | Handles.front().I = Handles.begin(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 289 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 290 | for (Function *Reader : Readers) |
| 291 | FunctionInfo[Reader].GlobalInfo[&GV] |= Ref; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 292 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 293 | if (!GV.isConstant()) // No need to keep track of writers to constants |
| 294 | for (Function *Writer : Writers) |
| 295 | FunctionInfo[Writer].GlobalInfo[&GV] |= Mod; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 296 | ++NumNonAddrTakenGlobalVars; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 297 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 298 | // If this global holds a pointer type, see if it is an indirect global. |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 299 | if (GV.getType()->getElementType()->isPointerTy() && |
| 300 | AnalyzeIndirectGlobalMemory(&GV)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 301 | ++NumIndirectGlobalVars; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 302 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 303 | Readers.clear(); |
| 304 | Writers.clear(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 308 | /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. |
| 309 | /// If this is used by anything complex (i.e., the address escapes), return |
| 310 | /// true. Also, while we are at it, keep track of those functions that read and |
| 311 | /// write to the value. |
| 312 | /// |
| 313 | /// If OkayStoreDest is non-null, stores into this global are allowed. |
| 314 | bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 315 | std::vector<Function *> &Readers, |
| 316 | std::vector<Function *> &Writers, |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 317 | GlobalValue *OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 318 | if (!V->getType()->isPointerTy()) |
| 319 | return true; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 320 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 321 | for (Use &U : V->uses()) { |
| 322 | User *I = U.getUser(); |
| 323 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 324 | Readers.push_back(LI->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 325 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 326 | if (V == SI->getOperand(1)) { |
| 327 | Writers.push_back(SI->getParent()->getParent()); |
| 328 | } else if (SI->getOperand(1) != OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 329 | return true; // Storing the pointer |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 330 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 331 | } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) { |
| 332 | if (AnalyzeUsesOfPointer(I, Readers, Writers)) |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 333 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 334 | } else if (Operator::getOpcode(I) == Instruction::BitCast) { |
| 335 | if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest)) |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 336 | return true; |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 337 | } else if (auto CS = CallSite(I)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 338 | // Make sure that this is just the function being called, not that it is |
| 339 | // passing into the function. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 340 | if (!CS.isCallee(&U)) { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 341 | // Detect calls to free. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 342 | if (isFreeCall(I, TLI)) |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 343 | Writers.push_back(CS->getParent()->getParent()); |
| 344 | else |
| 345 | return true; // Argument of an unknown call. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 346 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 347 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 348 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 349 | return true; // Allow comparison against null. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 350 | } else { |
| 351 | return true; |
| 352 | } |
Gabor Greif | 070b9a2 | 2010-07-09 15:53:42 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 358 | /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable |
| 359 | /// which holds a pointer type. See if the global always points to non-aliased |
| 360 | /// heap memory: that is, all initializers of the globals are allocations, and |
| 361 | /// those allocations have no use other than initialization of the global. |
| 362 | /// Further, all loads out of GV must directly use the memory, not store the |
| 363 | /// pointer somewhere. If this is true, we consider the memory pointed to by |
| 364 | /// GV to be owned by GV and can disambiguate other pointers from it. |
| 365 | bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { |
| 366 | // Keep track of values related to the allocation of the memory, f.e. the |
| 367 | // value produced by the malloc call and any casts. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 368 | std::vector<Value *> AllocRelatedValues; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 369 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 370 | // Walk the user list of the global. If we find anything other than a direct |
| 371 | // load or store, bail out. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 372 | for (User *U : GV->users()) { |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 373 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 374 | // The pointer loaded from the global can only be used in simple ways: |
| 375 | // we allow addressing of it and loading storing to it. We do *not* allow |
| 376 | // storing the loaded pointer somewhere else or passing to a function. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 377 | std::vector<Function *> ReadersWriters; |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 378 | if (AnalyzeUsesOfPointer(LI, ReadersWriters, ReadersWriters)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 379 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 380 | // TODO: Could try some IP mod/ref of the loaded pointer. |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 381 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 382 | // Storing the global itself. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 383 | if (SI->getOperand(0) == GV) |
| 384 | return false; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 385 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 386 | // If storing the null pointer, ignore it. |
| 387 | if (isa<ConstantPointerNull>(SI->getOperand(0))) |
| 388 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 389 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 390 | // Check the value being stored. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 391 | Value *Ptr = GetUnderlyingObject(SI->getOperand(0), |
| 392 | GV->getParent()->getDataLayout()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 393 | |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 394 | if (!isAllocLikeFn(Ptr, TLI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 395 | return false; // Too hard to analyze. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 396 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 397 | // Analyze all uses of the allocation. If any of them are used in a |
| 398 | // non-simple way (e.g. stored to another global) bail out. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 399 | std::vector<Function *> ReadersWriters; |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 400 | if (AnalyzeUsesOfPointer(Ptr, ReadersWriters, ReadersWriters, GV)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 401 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 402 | |
| 403 | // Remember that this allocation is related to the indirect global. |
| 404 | AllocRelatedValues.push_back(Ptr); |
| 405 | } else { |
| 406 | // Something complex, bail out. |
| 407 | return false; |
| 408 | } |
| 409 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 410 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 411 | // Okay, this is an indirect global. Remember all of the allocations for |
| 412 | // this global in AllocsForIndirectGlobals. |
| 413 | while (!AllocRelatedValues.empty()) { |
| 414 | AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 415 | Handles.emplace_front(*this, AllocRelatedValues.back()); |
| 416 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 417 | AllocRelatedValues.pop_back(); |
| 418 | } |
| 419 | IndirectGlobals.insert(GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame^] | 420 | Handles.emplace_front(*this, GV); |
| 421 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 422 | return true; |
| 423 | } |
| 424 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 425 | /// AnalyzeCallGraph - At this point, we know the functions where globals are |
| 426 | /// immediately stored to and read from. Propagate this information up the call |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 427 | /// graph to all callers and compute the mod/ref info for all memory for each |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 428 | /// function. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 429 | void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 430 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 431 | // visit all callees before callers (leaf-first). |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 432 | for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 433 | const std::vector<CallGraphNode *> &SCC = *I; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 434 | assert(!SCC.empty() && "SCC with no functions?"); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 435 | |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 436 | if (!SCC[0]->getFunction()) { |
| 437 | // Calls externally - can't say anything useful. Remove any existing |
| 438 | // function records (may have been created when scanning globals). |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 439 | for (auto *Node : SCC) |
| 440 | FunctionInfo.erase(Node->getFunction()); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 441 | continue; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | FunctionRecord &FR = FunctionInfo[SCC[0]->getFunction()]; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 445 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 446 | bool KnowNothing = false; |
| 447 | unsigned FunctionEffect = 0; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 448 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 449 | // Collect the mod/ref properties due to called functions. We only compute |
| 450 | // one mod-ref set. |
| 451 | for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { |
| 452 | Function *F = SCC[i]->getFunction(); |
| 453 | if (!F) { |
| 454 | KnowNothing = true; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 455 | break; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 457 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 458 | if (F->isDeclaration()) { |
| 459 | // Try to get mod/ref behaviour from function attributes. |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 460 | if (F->doesNotAccessMemory()) { |
| 461 | // Can't do better than that! |
| 462 | } else if (F->onlyReadsMemory()) { |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 463 | FunctionEffect |= Ref; |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 464 | if (!F->isIntrinsic()) |
Duncan Sands | e30b36f | 2008-09-11 15:43:12 +0000 | [diff] [blame] | 465 | // This function might call back into the module and read a global - |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 466 | // consider every global as possibly being read by this function. |
| 467 | FR.MayReadAnyGlobal = true; |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 468 | } else { |
Duncan Sands | d4133ac | 2008-09-11 19:35:55 +0000 | [diff] [blame] | 469 | FunctionEffect |= ModRef; |
| 470 | // Can't say anything useful unless it's an intrinsic - they don't |
| 471 | // read or write global variables of the kind considered here. |
| 472 | KnowNothing = !F->isIntrinsic(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 473 | } |
| 474 | continue; |
| 475 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 476 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 477 | for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 478 | CI != E && !KnowNothing; ++CI) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 479 | if (Function *Callee = CI->second->getFunction()) { |
| 480 | if (FunctionRecord *CalleeFR = getFunctionInfo(Callee)) { |
| 481 | // Propagate function effect up. |
| 482 | FunctionEffect |= CalleeFR->FunctionEffect; |
| 483 | |
| 484 | // Incorporate callee's effects on globals into our info. |
Rafael Espindola | 0c4eea7 | 2014-05-08 17:57:50 +0000 | [diff] [blame] | 485 | for (const auto &G : CalleeFR->GlobalInfo) |
| 486 | FR.GlobalInfo[G.first] |= G.second; |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 487 | FR.MayReadAnyGlobal |= CalleeFR->MayReadAnyGlobal; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 488 | } else { |
| 489 | // Can't say anything about it. However, if it is inside our SCC, |
| 490 | // then nothing needs to be done. |
| 491 | CallGraphNode *CalleeNode = CG[Callee]; |
| 492 | if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()) |
| 493 | KnowNothing = true; |
| 494 | } |
| 495 | } else { |
| 496 | KnowNothing = true; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | // If we can't say anything useful about this SCC, remove all SCC functions |
| 501 | // from the FunctionInfo map. |
| 502 | if (KnowNothing) { |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 503 | for (auto *Node : SCC) |
| 504 | FunctionInfo.erase(Node->getFunction()); |
Duncan Sands | e74d750 | 2008-09-03 16:10:55 +0000 | [diff] [blame] | 505 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Scan the function bodies for explicit loads or stores. |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 509 | for (auto *Node : SCC) { |
| 510 | if (FunctionEffect == ModRef) |
| 511 | break; // The mod/ref lattice saturates here. |
| 512 | for (Instruction &I : inst_range(Node->getFunction())) { |
| 513 | if (FunctionEffect == ModRef) |
| 514 | break; // The mod/ref lattice saturates here. |
| 515 | |
| 516 | // We handle calls specially because the graph-relevant aspects are |
| 517 | // handled above. |
| 518 | if (auto CS = CallSite(&I)) { |
| 519 | if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) { |
| 520 | // FIXME: It is completely unclear why this is necessary and not |
| 521 | // handled by the above graph code. |
| 522 | FunctionEffect |= ModRef; |
| 523 | } else if (Function *Callee = CS.getCalledFunction()) { |
| 524 | // The callgraph doesn't include intrinsic calls. |
| 525 | if (Callee->isIntrinsic()) { |
| 526 | ModRefBehavior Behaviour = |
| 527 | AliasAnalysis::getModRefBehavior(Callee); |
| 528 | FunctionEffect |= (Behaviour & ModRef); |
| 529 | } |
| 530 | } |
| 531 | continue; |
Duncan Sands | 9ddb314 | 2008-09-13 12:45:50 +0000 | [diff] [blame] | 532 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 533 | |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 534 | // All non-call instructions we use the primary predicates for whether |
| 535 | // thay read or write memory. |
| 536 | if (I.mayReadFromMemory()) |
| 537 | FunctionEffect |= Ref; |
| 538 | if (I.mayWriteToMemory()) |
| 539 | FunctionEffect |= Mod; |
| 540 | } |
| 541 | } |
| 542 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 543 | if ((FunctionEffect & Mod) == 0) |
| 544 | ++NumReadMemFunctions; |
| 545 | if (FunctionEffect == 0) |
| 546 | ++NumNoMemFunctions; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 547 | FR.FunctionEffect = FunctionEffect; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 548 | |
| 549 | // Finally, now that we know the full effect on this SCC, clone the |
| 550 | // information to each function in the SCC. |
| 551 | for (unsigned i = 1, e = SCC.size(); i != e; ++i) |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 552 | FunctionInfo[SCC[i]->getFunction()] = FR; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 553 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 556 | /// alias - If one of the pointers is to a global that we are tracking, and the |
| 557 | /// other is some random pointer, we know there cannot be an alias, because the |
| 558 | /// address of the global isn't taken. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 559 | AliasResult GlobalsModRef::alias(const MemoryLocation &LocA, |
| 560 | const MemoryLocation &LocB) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 561 | // Get the base object these pointers point to. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 562 | const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL); |
| 563 | const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 564 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 565 | // If either of the underlying values is a global, they may be non-addr-taken |
| 566 | // globals, which we can answer queries about. |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 567 | const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); |
| 568 | const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 569 | if (GV1 || GV2) { |
| 570 | // If the global's address is taken, pretend we don't know it's a pointer to |
| 571 | // the global. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 572 | if (GV1 && !NonAddressTakenGlobals.count(GV1)) |
| 573 | GV1 = nullptr; |
| 574 | if (GV2 && !NonAddressTakenGlobals.count(GV2)) |
| 575 | GV2 = nullptr; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 576 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 577 | // If the two pointers are derived from two different non-addr-taken |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 578 | // globals we know these can't alias. |
| 579 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 580 | return NoAlias; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 581 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 582 | // If one is and the other isn't, it isn't strictly safe but we can fake |
| 583 | // this result if necessary for performance. This does not appear to be |
| 584 | // a common problem in practice. |
| 585 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 586 | if ((GV1 || GV2) && GV1 != GV2) |
| 587 | return NoAlias; |
| 588 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 589 | // Otherwise if they are both derived from the same addr-taken global, we |
| 590 | // can't know the two accesses don't overlap. |
| 591 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 592 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 593 | // These pointers may be based on the memory owned by an indirect global. If |
| 594 | // so, we may be able to handle this. First check to see if the base pointer |
| 595 | // is a direct load from an indirect global. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 596 | GV1 = GV2 = nullptr; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 597 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV1)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 598 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
| 599 | if (IndirectGlobals.count(GV)) |
| 600 | GV1 = GV; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 601 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV2)) |
| 602 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 603 | if (IndirectGlobals.count(GV)) |
| 604 | GV2 = GV; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 605 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 606 | // These pointers may also be from an allocation for the indirect global. If |
| 607 | // so, also handle them. |
| 608 | if (AllocsForIndirectGlobals.count(UV1)) |
| 609 | GV1 = AllocsForIndirectGlobals[UV1]; |
| 610 | if (AllocsForIndirectGlobals.count(UV2)) |
| 611 | GV2 = AllocsForIndirectGlobals[UV2]; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 612 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 613 | // Now that we know whether the two pointers are related to indirect globals, |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 614 | // use this to disambiguate the pointers. If the pointers are based on |
| 615 | // different indirect globals they cannot alias. |
| 616 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 617 | return NoAlias; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 618 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 619 | // If one is based on an indirect global and the other isn't, it isn't |
| 620 | // strictly safe but we can fake this result if necessary for performance. |
| 621 | // This does not appear to be a common problem in practice. |
| 622 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 623 | if ((GV1 || GV2) && GV1 != GV2) |
| 624 | return NoAlias; |
| 625 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 626 | return AliasAnalysis::alias(LocA, LocB); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | AliasAnalysis::ModRefResult |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 630 | GlobalsModRef::getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 631 | unsigned Known = ModRef; |
| 632 | |
| 633 | // If we are asking for mod/ref info of a direct call with a pointer to a |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 634 | // global we are tracking, return information if we have it. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 635 | const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout(); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 636 | if (const GlobalValue *GV = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 637 | dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL))) |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 638 | if (GV->hasLocalLinkage()) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 639 | if (const Function *F = CS.getCalledFunction()) |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 640 | if (NonAddressTakenGlobals.count(GV)) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 641 | if (const FunctionRecord *FR = getFunctionInfo(F)) |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 642 | Known = FR->getInfoForGlobal(GV); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 643 | |
| 644 | if (Known == NoModRef) |
| 645 | return NoModRef; // No need to query other mod/ref analyses |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 646 | return ModRefResult(Known & AliasAnalysis::getModRefInfo(CS, Loc)); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 647 | } |