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" |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
| 22 | #include "llvm/Analysis/CallGraph.h" |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/MemoryBuiltins.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 27 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/IntrinsicInst.h" |
| 30 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 33 | #include <list> |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 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 | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 60 | /// The mod/ref information collected for a particular function. |
| 61 | /// |
| 62 | /// We collect information about mod/ref behavior of a function here, both in |
| 63 | /// general and as pertains to specific globals. We only have this detailed |
| 64 | /// information when we know *something* useful about the behavior. If we |
| 65 | /// saturate to fully general mod/ref, we remove the info for the function. |
| 66 | class FunctionInfo { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 67 | typedef SmallDenseMap<const GlobalValue *, ModRefInfo, 16> GlobalInfoMapType; |
| 68 | |
| 69 | /// Build a wrapper struct that has 8-byte alignment. All heap allocations |
| 70 | /// should provide this much alignment at least, but this makes it clear we |
| 71 | /// specifically rely on this amount of alignment. |
| 72 | struct LLVM_ALIGNAS(8) AlignedMap { |
| 73 | AlignedMap() {} |
| 74 | AlignedMap(const AlignedMap &Arg) : Map(Arg.Map) {} |
| 75 | GlobalInfoMapType Map; |
| 76 | }; |
| 77 | |
| 78 | /// Pointer traits for our aligned map. |
| 79 | struct AlignedMapPointerTraits { |
| 80 | static inline void *getAsVoidPointer(AlignedMap *P) { return P; } |
| 81 | static inline AlignedMap *getFromVoidPointer(void *P) { |
| 82 | return (AlignedMap *)P; |
| 83 | } |
| 84 | enum { NumLowBitsAvailable = 3 }; |
| 85 | static_assert(AlignOf<AlignedMap>::Alignment >= (1 << NumLowBitsAvailable), |
| 86 | "AlignedMap insufficiently aligned to have enough low bits."); |
| 87 | }; |
| 88 | |
| 89 | /// The bit that flags that this function may read any global. This is |
| 90 | /// chosen to mix together with ModRefInfo bits. |
| 91 | enum { MayReadAnyGlobal = 4 }; |
| 92 | |
| 93 | /// Checks to document the invariants of the bit packing here. |
| 94 | static_assert((MayReadAnyGlobal & MRI_ModRef) == 0, |
| 95 | "ModRef and the MayReadAnyGlobal flag bits overlap."); |
| 96 | static_assert(((MayReadAnyGlobal | MRI_ModRef) >> |
| 97 | AlignedMapPointerTraits::NumLowBitsAvailable) == 0, |
| 98 | "Insufficient low bits to store our flag and ModRef info."); |
| 99 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 100 | public: |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 101 | FunctionInfo() : Info() {} |
| 102 | ~FunctionInfo() { |
| 103 | delete Info.getPointer(); |
| 104 | } |
| 105 | // Spell out the copy ond move constructors and assignment operators to get |
| 106 | // deep copy semantics and correct move semantics in the face of the |
| 107 | // pointer-int pair. |
| 108 | FunctionInfo(const FunctionInfo &Arg) |
| 109 | : Info(nullptr, Arg.Info.getInt()) { |
| 110 | if (const auto *ArgPtr = Arg.Info.getPointer()) |
| 111 | Info.setPointer(new AlignedMap(*ArgPtr)); |
| 112 | } |
| 113 | FunctionInfo(FunctionInfo &&Arg) |
| 114 | : Info(Arg.Info.getPointer(), Arg.Info.getInt()) { |
| 115 | Arg.Info.setPointerAndInt(nullptr, 0); |
| 116 | } |
| 117 | FunctionInfo &operator=(const FunctionInfo &RHS) { |
| 118 | delete Info.getPointer(); |
| 119 | Info.setPointerAndInt(nullptr, RHS.Info.getInt()); |
| 120 | if (const auto *RHSPtr = RHS.Info.getPointer()) |
| 121 | Info.setPointer(new AlignedMap(*RHSPtr)); |
| 122 | return *this; |
| 123 | } |
| 124 | FunctionInfo &operator=(FunctionInfo &&RHS) { |
| 125 | delete Info.getPointer(); |
| 126 | Info.setPointerAndInt(RHS.Info.getPointer(), RHS.Info.getInt()); |
| 127 | RHS.Info.setPointerAndInt(nullptr, 0); |
| 128 | return *this; |
| 129 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 131 | /// Returns the \c ModRefInfo info for this function. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 132 | ModRefInfo getModRefInfo() const { |
| 133 | return ModRefInfo(Info.getInt() & MRI_ModRef); |
| 134 | } |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 135 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 136 | /// Adds new \c ModRefInfo for this function to its state. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 137 | void addModRefInfo(ModRefInfo NewMRI) { |
| 138 | Info.setInt(Info.getInt() | NewMRI); |
| 139 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 140 | |
| 141 | /// Returns whether this function may read any global variable, and we don't |
| 142 | /// know which global. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 143 | bool mayReadAnyGlobal() const { return Info.getInt() & MayReadAnyGlobal; } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 144 | |
| 145 | /// Sets this function as potentially reading from any global. |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 146 | void setMayReadAnyGlobal() { Info.setInt(Info.getInt() | MayReadAnyGlobal); } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 147 | |
| 148 | /// Returns the \c ModRefInfo info for this function w.r.t. a particular |
| 149 | /// global, which may be more precise than the general information above. |
| 150 | ModRefInfo getModRefInfoForGlobal(const GlobalValue &GV) const { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 151 | ModRefInfo GlobalMRI = mayReadAnyGlobal() ? MRI_Ref : MRI_NoModRef; |
| 152 | if (AlignedMap *P = Info.getPointer()) { |
| 153 | auto I = P->Map.find(&GV); |
| 154 | if (I != P->Map.end()) |
| 155 | GlobalMRI = ModRefInfo(GlobalMRI | I->second); |
| 156 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 157 | return GlobalMRI; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 160 | /// Add mod/ref info from another function into ours, saturating towards |
| 161 | /// MRI_ModRef. |
| 162 | void addFunctionInfo(const FunctionInfo &FI) { |
| 163 | addModRefInfo(FI.getModRefInfo()); |
| 164 | |
| 165 | if (FI.mayReadAnyGlobal()) |
| 166 | setMayReadAnyGlobal(); |
| 167 | |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 168 | if (AlignedMap *P = FI.Info.getPointer()) |
| 169 | for (const auto &G : P->Map) |
| 170 | addModRefInfoForGlobal(*G.first, G.second); |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 171 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 172 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 173 | void addModRefInfoForGlobal(const GlobalValue &GV, ModRefInfo NewMRI) { |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 174 | AlignedMap *P = Info.getPointer(); |
| 175 | if (!P) { |
| 176 | P = new AlignedMap(); |
| 177 | Info.setPointer(P); |
| 178 | } |
| 179 | auto &GlobalMRI = P->Map[&GV]; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 180 | GlobalMRI = ModRefInfo(GlobalMRI | NewMRI); |
| 181 | } |
| 182 | |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 183 | /// Clear a global's ModRef info. Should be used when a global is being |
| 184 | /// deleted. |
| 185 | void eraseModRefInfoForGlobal(const GlobalValue &GV) { |
| 186 | if (AlignedMap *P = Info.getPointer()) |
| 187 | P->Map.erase(&GV); |
| 188 | } |
| 189 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 190 | private: |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame] | 191 | /// All of the information is encoded into a single pointer, with a three bit |
| 192 | /// integer in the low three bits. The high bit provides a flag for when this |
| 193 | /// function may read any global. The low two bits are the ModRefInfo. And |
| 194 | /// the pointer, when non-null, points to a map from GlobalValue to |
| 195 | /// ModRefInfo specific to that GlobalValue. |
| 196 | PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | /// GlobalsModRef - The actual analysis pass. |
| 200 | class GlobalsModRef : public ModulePass, public AliasAnalysis { |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 201 | /// The globals that do not have their addresses taken. |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 202 | SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 203 | |
| 204 | /// IndirectGlobals - The memory pointed to by this global is known to be |
| 205 | /// 'owned' by the global. |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 206 | SmallPtrSet<const GlobalValue *, 8> IndirectGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 207 | |
| 208 | /// AllocsForIndirectGlobals - If an instruction allocates memory for an |
| 209 | /// indirect global, this map indicates which one. |
Chandler Carruth | 6919267 | 2015-07-22 11:36:09 +0000 | [diff] [blame] | 210 | DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 211 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 212 | /// For each function, keep track of what globals are modified or read. |
| 213 | DenseMap<const Function *, FunctionInfo> FunctionInfos; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 214 | |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 215 | /// Handle to clear this analysis on deletion of values. |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 216 | struct DeletionCallbackHandle final : CallbackVH { |
| 217 | GlobalsModRef &GMR; |
| 218 | std::list<DeletionCallbackHandle>::iterator I; |
| 219 | |
| 220 | DeletionCallbackHandle(GlobalsModRef &GMR, Value *V) |
| 221 | : CallbackVH(V), GMR(GMR) {} |
| 222 | |
| 223 | void deleted() override { |
| 224 | Value *V = getValPtr(); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 225 | if (auto *F = dyn_cast<Function>(V)) |
| 226 | GMR.FunctionInfos.erase(F); |
| 227 | |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 228 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 229 | if (GMR.NonAddressTakenGlobals.erase(GV)) { |
| 230 | // This global might be an indirect global. If so, remove it and |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 231 | // remove any AllocRelatedValues for it. |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 232 | if (GMR.IndirectGlobals.erase(GV)) { |
| 233 | // Remove any entries in AllocsForIndirectGlobals for this global. |
Chandler Carruth | 6919267 | 2015-07-22 11:36:09 +0000 | [diff] [blame] | 234 | for (auto I = GMR.AllocsForIndirectGlobals.begin(), |
| 235 | E = GMR.AllocsForIndirectGlobals.end(); |
| 236 | I != E; ++I) |
| 237 | if (I->second == GV) |
| 238 | GMR.AllocsForIndirectGlobals.erase(I); |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 239 | } |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 240 | |
| 241 | // Scan the function info we have collected and remove this global |
| 242 | // from all of them. |
| 243 | for (auto &FIPair : GMR.FunctionInfos) |
| 244 | FIPair.second.eraseModRefInfoForGlobal(*GV); |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | // If this is an allocation related to an indirect global, remove it. |
| 249 | GMR.AllocsForIndirectGlobals.erase(V); |
| 250 | |
| 251 | // And clear out the handle. |
| 252 | setValPtr(nullptr); |
| 253 | GMR.Handles.erase(I); |
| 254 | // This object is now destroyed! |
| 255 | } |
| 256 | }; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 257 | |
| 258 | /// List of callbacks for globals being tracked by this analysis. Note that |
| 259 | /// these objects are quite large, but we only anticipate having one per |
| 260 | /// global tracked by this analysis. There are numerous optimizations we |
| 261 | /// could perform to the memory utilization here if this becomes a problem. |
| 262 | std::list<DeletionCallbackHandle> Handles; |
| 263 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 264 | public: |
| 265 | static char ID; |
| 266 | GlobalsModRef() : ModulePass(ID) { |
| 267 | initializeGlobalsModRefPass(*PassRegistry::getPassRegistry()); |
| 268 | } |
| 269 | |
| 270 | bool runOnModule(Module &M) override { |
| 271 | InitializeAliasAnalysis(this, &M.getDataLayout()); |
| 272 | |
| 273 | // Find non-addr taken globals. |
| 274 | AnalyzeGlobals(M); |
| 275 | |
| 276 | // Propagate on CG. |
| 277 | AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 282 | AliasAnalysis::getAnalysisUsage(AU); |
| 283 | AU.addRequired<CallGraphWrapperPass>(); |
| 284 | AU.setPreservesAll(); // Does not transform code |
| 285 | } |
| 286 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 287 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 288 | /// an analysis interface through multiple inheritance. If needed, it |
| 289 | /// should override this to adjust the this pointer as needed for the |
| 290 | /// specified pass info. |
| 291 | void *getAdjustedAnalysisPointer(AnalysisID PI) override { |
| 292 | if (PI == &AliasAnalysis::ID) |
| 293 | return (AliasAnalysis *)this; |
| 294 | return this; |
| 295 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 296 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 297 | //------------------------------------------------ |
| 298 | // Implement the AliasAnalysis API |
| 299 | // |
| 300 | AliasResult alias(const MemoryLocation &LocA, |
| 301 | const MemoryLocation &LocB) override; |
| 302 | ModRefInfo getModRefInfo(ImmutableCallSite CS, |
| 303 | const MemoryLocation &Loc) override; |
| 304 | ModRefInfo getModRefInfo(ImmutableCallSite CS1, |
| 305 | ImmutableCallSite CS2) override { |
| 306 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 307 | } |
| 308 | |
| 309 | /// getModRefBehavior - Return the behavior of the specified function if |
| 310 | /// called from the specified call site. The call site may be null in which |
| 311 | /// case the most generic behavior of this function should be returned. |
| 312 | FunctionModRefBehavior getModRefBehavior(const Function *F) override { |
| 313 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 314 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 315 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 316 | if (FI->getModRefInfo() == MRI_NoModRef) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 317 | Min = FMRB_DoesNotAccessMemory; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 318 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 319 | Min = FMRB_OnlyReadsMemory; |
| 320 | } |
| 321 | |
| 322 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); |
| 323 | } |
| 324 | |
| 325 | /// getModRefBehavior - Return the behavior of the specified function if |
| 326 | /// called from the specified call site. The call site may be null in which |
| 327 | /// case the most generic behavior of this function should be returned. |
| 328 | FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override { |
| 329 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 330 | |
| 331 | if (const Function *F = CS.getCalledFunction()) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 332 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 333 | if (FI->getModRefInfo() == MRI_NoModRef) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 334 | Min = FMRB_DoesNotAccessMemory; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 335 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 336 | Min = FMRB_OnlyReadsMemory; |
| 337 | } |
| 338 | |
| 339 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); |
| 340 | } |
| 341 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 342 | private: |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 343 | /// Returns the function info for the function, or null if we don't have |
| 344 | /// anything useful to say about it. |
| 345 | FunctionInfo *getFunctionInfo(const Function *F) { |
| 346 | auto I = FunctionInfos.find(F); |
| 347 | if (I != FunctionInfos.end()) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 348 | return &I->second; |
| 349 | return nullptr; |
| 350 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 351 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 352 | void AnalyzeGlobals(Module &M); |
| 353 | void AnalyzeCallGraph(CallGraph &CG, Module &M); |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 354 | bool AnalyzeUsesOfPointer(Value *V, |
| 355 | SmallPtrSetImpl<Function *> *Readers = nullptr, |
| 356 | SmallPtrSetImpl<Function *> *Writers = nullptr, |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 357 | GlobalValue *OkayStoreDest = nullptr); |
| 358 | bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 359 | |
| 360 | bool isNonEscapingGlobalNoAlias(const GlobalValue *GV, const Value *V); |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 361 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 363 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 364 | char GlobalsModRef::ID = 0; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 365 | INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 366 | "Simple mod/ref analysis for globals", false, true, |
| 367 | false) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 368 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 369 | INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 370 | "Simple mod/ref analysis for globals", false, true, |
| 371 | false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 372 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 373 | Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } |
| 374 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 375 | /// AnalyzeGlobals - Scan through the users of all of the internal |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 376 | /// 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] | 377 | /// (really, their address passed to something nontrivial), record this fact, |
| 378 | /// and record the functions that they are used directly in. |
| 379 | void GlobalsModRef::AnalyzeGlobals(Module &M) { |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 380 | SmallPtrSet<Function *, 64> TrackedFunctions; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 381 | for (Function &F : M) |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 382 | if (F.hasLocalLinkage()) |
| 383 | if (!AnalyzeUsesOfPointer(&F)) { |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 384 | // Remember that we are tracking this global. |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 385 | NonAddressTakenGlobals.insert(&F); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 386 | TrackedFunctions.insert(&F); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 387 | Handles.emplace_front(*this, &F); |
| 388 | Handles.front().I = Handles.begin(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 389 | ++NumNonAddrTakenFunctions; |
| 390 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 391 | |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 392 | SmallPtrSet<Function *, 64> Readers, Writers; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 393 | for (GlobalVariable &GV : M.globals()) |
| 394 | if (GV.hasLocalLinkage()) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 395 | if (!AnalyzeUsesOfPointer(&GV, &Readers, |
| 396 | GV.isConstant() ? nullptr : &Writers)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 397 | // Remember that we are tracking this global, and the mod/ref fns |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 398 | NonAddressTakenGlobals.insert(&GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 399 | Handles.emplace_front(*this, &GV); |
| 400 | Handles.front().I = Handles.begin(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 401 | |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 402 | for (Function *Reader : Readers) { |
| 403 | if (TrackedFunctions.insert(Reader).second) { |
| 404 | Handles.emplace_front(*this, Reader); |
| 405 | Handles.front().I = Handles.begin(); |
| 406 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 407 | FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 408 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 409 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 410 | if (!GV.isConstant()) // No need to keep track of writers to constants |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 411 | for (Function *Writer : Writers) { |
| 412 | if (TrackedFunctions.insert(Writer).second) { |
| 413 | Handles.emplace_front(*this, Writer); |
| 414 | Handles.front().I = Handles.begin(); |
| 415 | } |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 416 | FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod); |
Chandler Carruth | 786e6db | 2015-07-28 06:01:57 +0000 | [diff] [blame] | 417 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 418 | ++NumNonAddrTakenGlobalVars; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 419 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 420 | // 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] | 421 | if (GV.getType()->getElementType()->isPointerTy() && |
| 422 | AnalyzeIndirectGlobalMemory(&GV)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 423 | ++NumIndirectGlobalVars; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 424 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 425 | Readers.clear(); |
| 426 | Writers.clear(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 430 | /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. |
| 431 | /// If this is used by anything complex (i.e., the address escapes), return |
| 432 | /// true. Also, while we are at it, keep track of those functions that read and |
| 433 | /// write to the value. |
| 434 | /// |
| 435 | /// If OkayStoreDest is non-null, stores into this global are allowed. |
| 436 | bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 437 | SmallPtrSetImpl<Function *> *Readers, |
| 438 | SmallPtrSetImpl<Function *> *Writers, |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 439 | GlobalValue *OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 440 | if (!V->getType()->isPointerTy()) |
| 441 | return true; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 442 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 443 | for (Use &U : V->uses()) { |
| 444 | User *I = U.getUser(); |
| 445 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 446 | if (Readers) |
| 447 | Readers->insert(LI->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 448 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 449 | if (V == SI->getOperand(1)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 450 | if (Writers) |
| 451 | Writers->insert(SI->getParent()->getParent()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 452 | } else if (SI->getOperand(1) != OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 453 | return true; // Storing the pointer |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 454 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 455 | } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) { |
| 456 | if (AnalyzeUsesOfPointer(I, Readers, Writers)) |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 457 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 458 | } else if (Operator::getOpcode(I) == Instruction::BitCast) { |
| 459 | if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest)) |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 460 | return true; |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 461 | } else if (auto CS = CallSite(I)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 462 | // Make sure that this is just the function being called, not that it is |
| 463 | // passing into the function. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 464 | if (!CS.isCallee(&U)) { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 465 | // Detect calls to free. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 466 | if (isFreeCall(I, TLI)) { |
| 467 | if (Writers) |
| 468 | Writers->insert(CS->getParent()->getParent()); |
| 469 | } else { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 470 | return true; // Argument of an unknown call. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 471 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 472 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 473 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 474 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 475 | return true; // Allow comparison against null. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 476 | } else { |
| 477 | return true; |
| 478 | } |
Gabor Greif | 070b9a2 | 2010-07-09 15:53:42 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 481 | return false; |
| 482 | } |
| 483 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 484 | /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable |
| 485 | /// which holds a pointer type. See if the global always points to non-aliased |
| 486 | /// heap memory: that is, all initializers of the globals are allocations, and |
| 487 | /// those allocations have no use other than initialization of the global. |
| 488 | /// Further, all loads out of GV must directly use the memory, not store the |
| 489 | /// pointer somewhere. If this is true, we consider the memory pointed to by |
| 490 | /// GV to be owned by GV and can disambiguate other pointers from it. |
| 491 | bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { |
| 492 | // Keep track of values related to the allocation of the memory, f.e. the |
| 493 | // value produced by the malloc call and any casts. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 494 | std::vector<Value *> AllocRelatedValues; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 495 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 496 | // Walk the user list of the global. If we find anything other than a direct |
| 497 | // load or store, bail out. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 498 | for (User *U : GV->users()) { |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 499 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 500 | // The pointer loaded from the global can only be used in simple ways: |
| 501 | // we allow addressing of it and loading storing to it. We do *not* allow |
| 502 | // storing the loaded pointer somewhere else or passing to a function. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 503 | if (AnalyzeUsesOfPointer(LI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 504 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 505 | // TODO: Could try some IP mod/ref of the loaded pointer. |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 506 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 507 | // Storing the global itself. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 508 | if (SI->getOperand(0) == GV) |
| 509 | return false; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 510 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 511 | // If storing the null pointer, ignore it. |
| 512 | if (isa<ConstantPointerNull>(SI->getOperand(0))) |
| 513 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 514 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 515 | // Check the value being stored. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 516 | Value *Ptr = GetUnderlyingObject(SI->getOperand(0), |
| 517 | GV->getParent()->getDataLayout()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 518 | |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 519 | if (!isAllocLikeFn(Ptr, TLI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 520 | return false; // Too hard to analyze. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 521 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 522 | // Analyze all uses of the allocation. If any of them are used in a |
| 523 | // non-simple way (e.g. stored to another global) bail out. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 524 | if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr, |
| 525 | GV)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 526 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 527 | |
| 528 | // Remember that this allocation is related to the indirect global. |
| 529 | AllocRelatedValues.push_back(Ptr); |
| 530 | } else { |
| 531 | // Something complex, bail out. |
| 532 | return false; |
| 533 | } |
| 534 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 535 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 536 | // Okay, this is an indirect global. Remember all of the allocations for |
| 537 | // this global in AllocsForIndirectGlobals. |
| 538 | while (!AllocRelatedValues.empty()) { |
| 539 | AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 540 | Handles.emplace_front(*this, AllocRelatedValues.back()); |
| 541 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 542 | AllocRelatedValues.pop_back(); |
| 543 | } |
| 544 | IndirectGlobals.insert(GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 545 | Handles.emplace_front(*this, GV); |
| 546 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 547 | return true; |
| 548 | } |
| 549 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 550 | /// AnalyzeCallGraph - At this point, we know the functions where globals are |
| 551 | /// immediately stored to and read from. Propagate this information up the call |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 552 | /// 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] | 553 | /// function. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 554 | void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 555 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 556 | // visit all callees before callers (leaf-first). |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 557 | 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] | 558 | const std::vector<CallGraphNode *> &SCC = *I; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 559 | assert(!SCC.empty() && "SCC with no functions?"); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 560 | |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 561 | if (!SCC[0]->getFunction()) { |
| 562 | // Calls externally - can't say anything useful. Remove any existing |
| 563 | // function records (may have been created when scanning globals). |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 564 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 565 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 566 | continue; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 569 | FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()]; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 570 | bool KnowNothing = false; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 571 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 572 | // Collect the mod/ref properties due to called functions. We only compute |
| 573 | // one mod-ref set. |
| 574 | for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { |
| 575 | Function *F = SCC[i]->getFunction(); |
| 576 | if (!F) { |
| 577 | KnowNothing = true; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 578 | break; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 579 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 580 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 581 | if (F->isDeclaration()) { |
| 582 | // Try to get mod/ref behaviour from function attributes. |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 583 | if (F->doesNotAccessMemory()) { |
| 584 | // Can't do better than that! |
| 585 | } else if (F->onlyReadsMemory()) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 586 | FI.addModRefInfo(MRI_Ref); |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 587 | if (!F->isIntrinsic()) |
Duncan Sands | e30b36f | 2008-09-11 15:43:12 +0000 | [diff] [blame] | 588 | // This function might call back into the module and read a global - |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 589 | // consider every global as possibly being read by this function. |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 590 | FI.setMayReadAnyGlobal(); |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 591 | } else { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 592 | FI.addModRefInfo(MRI_ModRef); |
Duncan Sands | d4133ac | 2008-09-11 19:35:55 +0000 | [diff] [blame] | 593 | // Can't say anything useful unless it's an intrinsic - they don't |
| 594 | // read or write global variables of the kind considered here. |
| 595 | KnowNothing = !F->isIntrinsic(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 596 | } |
| 597 | continue; |
| 598 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 599 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 600 | for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 601 | CI != E && !KnowNothing; ++CI) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 602 | if (Function *Callee = CI->second->getFunction()) { |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 603 | if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) { |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 604 | // Propagate function effect up. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 605 | FI.addFunctionInfo(*CalleeFI); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 606 | } else { |
| 607 | // Can't say anything about it. However, if it is inside our SCC, |
| 608 | // then nothing needs to be done. |
| 609 | CallGraphNode *CalleeNode = CG[Callee]; |
| 610 | if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()) |
| 611 | KnowNothing = true; |
| 612 | } |
| 613 | } else { |
| 614 | KnowNothing = true; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | // If we can't say anything useful about this SCC, remove all SCC functions |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 619 | // from the FunctionInfos map. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 620 | if (KnowNothing) { |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 621 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 622 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | e74d750 | 2008-09-03 16:10:55 +0000 | [diff] [blame] | 623 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | // Scan the function bodies for explicit loads or stores. |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 627 | for (auto *Node : SCC) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 628 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 629 | break; // The mod/ref lattice saturates here. |
Nico Rieck | 7819951 | 2015-08-06 19:10:45 +0000 | [diff] [blame] | 630 | for (Instruction &I : instructions(Node->getFunction())) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 631 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 632 | break; // The mod/ref lattice saturates here. |
| 633 | |
| 634 | // We handle calls specially because the graph-relevant aspects are |
| 635 | // handled above. |
| 636 | if (auto CS = CallSite(&I)) { |
| 637 | if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) { |
| 638 | // FIXME: It is completely unclear why this is necessary and not |
| 639 | // handled by the above graph code. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 640 | FI.addModRefInfo(MRI_ModRef); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 641 | } else if (Function *Callee = CS.getCalledFunction()) { |
| 642 | // The callgraph doesn't include intrinsic calls. |
| 643 | if (Callee->isIntrinsic()) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 644 | FunctionModRefBehavior Behaviour = |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 645 | AliasAnalysis::getModRefBehavior(Callee); |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 646 | FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef)); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | continue; |
Duncan Sands | 9ddb314 | 2008-09-13 12:45:50 +0000 | [diff] [blame] | 650 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 651 | |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 652 | // All non-call instructions we use the primary predicates for whether |
| 653 | // thay read or write memory. |
| 654 | if (I.mayReadFromMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 655 | FI.addModRefInfo(MRI_Ref); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 656 | if (I.mayWriteToMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 657 | FI.addModRefInfo(MRI_Mod); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 661 | if ((FI.getModRefInfo() & MRI_Mod) == 0) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 662 | ++NumReadMemFunctions; |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 663 | if (FI.getModRefInfo() == MRI_NoModRef) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 664 | ++NumNoMemFunctions; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 665 | |
| 666 | // Finally, now that we know the full effect on this SCC, clone the |
| 667 | // information to each function in the SCC. |
| 668 | for (unsigned i = 1, e = SCC.size(); i != e; ++i) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 669 | FunctionInfos[SCC[i]->getFunction()] = FI; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 670 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 673 | // There are particular cases where we can conclude no-alias between |
| 674 | // a non-addr-taken global and some other underlying object. Specifically, |
| 675 | // a non-addr-taken global is known to not be escaped from any function. It is |
| 676 | // also incorrect for a transformation to introduce an escape of a global in |
| 677 | // a way that is observable when it was not there previously. One function |
| 678 | // being transformed to introduce an escape which could possibly be observed |
| 679 | // (via loading from a global or the return value for example) within another |
| 680 | // function is never safe. If the observation is made through non-atomic |
| 681 | // operations on different threads, it is a data-race and UB. If the |
| 682 | // observation is well defined, by being observed the transformation would have |
| 683 | // changed program behavior by introducing the observed escape, making it an |
| 684 | // invalid transform. |
| 685 | // |
| 686 | // This property does require that transformations which *temporarily* escape |
| 687 | // a global that was not previously escaped, prior to restoring it, cannot rely |
| 688 | // on the results of GMR::alias. This seems a reasonable restriction, although |
| 689 | // currently there is no way to enforce it. There is also no realistic |
| 690 | // optimization pass that would make this mistake. The closest example is |
| 691 | // a transformation pass which does reg2mem of SSA values but stores them into |
| 692 | // global variables temporarily before restoring the global variable's value. |
| 693 | // This could be useful to expose "benign" races for example. However, it seems |
| 694 | // reasonable to require that a pass which introduces escapes of global |
| 695 | // variables in this way to either not trust AA results while the escape is |
| 696 | // active, or to be forced to operate as a module pass that cannot co-exist |
| 697 | // with an alias analysis such as GMR. |
| 698 | bool GlobalsModRef::isNonEscapingGlobalNoAlias(const GlobalValue *GV, |
| 699 | const Value *V) { |
| 700 | // In order to know that the underlying object cannot alias the |
| 701 | // non-addr-taken global, we must know that it would have to be an escape. |
| 702 | // Thus if the underlying object is a function argument, a load from |
| 703 | // a global, or the return of a function, it cannot alias. We can also |
| 704 | // recurse through PHI nodes and select nodes provided all of their inputs |
| 705 | // resolve to one of these known-escaping roots. |
| 706 | SmallPtrSet<const Value *, 8> Visited; |
| 707 | SmallVector<const Value *, 8> Inputs; |
| 708 | Visited.insert(V); |
| 709 | Inputs.push_back(V); |
| 710 | int Depth = 0; |
| 711 | do { |
| 712 | const Value *Input = Inputs.pop_back_val(); |
| 713 | |
| 714 | if (auto *InputGV = dyn_cast<GlobalValue>(Input)) { |
| 715 | // If one input is the very global we're querying against, then we can't |
| 716 | // conclude no-alias. |
| 717 | if (InputGV == GV) |
| 718 | return false; |
| 719 | |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame^] | 720 | // Distinct GlobalVariables never alias, unless overriden or zero-sized. |
| 721 | // FIXME: The condition can be refined, but be conservative for now. |
| 722 | auto *GVar = dyn_cast<GlobalVariable>(GV); |
| 723 | auto *InputGVar = dyn_cast<GlobalVariable>(InputGV); |
| 724 | if (GVar && InputGVar && |
| 725 | !GVar->isDeclaration() && !InputGVar->isDeclaration() && |
| 726 | !GVar->mayBeOverridden() && !InputGVar->mayBeOverridden()) { |
| 727 | Type *GVType = GVar->getInitializer()->getType(); |
| 728 | Type *InputGVType = InputGVar->getInitializer()->getType(); |
| 729 | if (GVType->isSized() && InputGVType->isSized() && |
| 730 | (DL->getTypeAllocSize(GVType) > 0) && |
| 731 | (DL->getTypeAllocSize(InputGVType) > 0)) |
| 732 | continue; |
| 733 | } |
| 734 | |
| 735 | // Conservatively return false, even though we could be smarter |
| 736 | // (e.g. look through GlobalAliases). |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 737 | return false; |
| 738 | } |
| 739 | |
| 740 | if (isa<Argument>(Input) || isa<CallInst>(Input) || |
| 741 | isa<InvokeInst>(Input)) { |
| 742 | // Arguments to functions or returns from functions are inherently |
| 743 | // escaping, so we can immediately classify those as not aliasing any |
| 744 | // non-addr-taken globals. |
| 745 | continue; |
| 746 | } |
| 747 | if (auto *LI = dyn_cast<LoadInst>(Input)) { |
| 748 | // A pointer loaded from a global would have been captured, and we know |
| 749 | // that the global is non-escaping, so no alias. |
| 750 | if (isa<GlobalValue>(LI->getPointerOperand())) |
| 751 | continue; |
| 752 | |
| 753 | // Otherwise, a load could come from anywhere, so bail. |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | // Recurse through a limited number of selects and PHIs. This is an |
| 758 | // arbitrary depth of 4, lower numbers could be used to fix compile time |
| 759 | // issues if needed, but this is generally expected to be only be important |
| 760 | // for small depths. |
| 761 | if (++Depth > 4) |
| 762 | return false; |
| 763 | if (auto *SI = dyn_cast<SelectInst>(Input)) { |
| 764 | const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), *DL); |
| 765 | const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), *DL); |
| 766 | if (Visited.insert(LHS).second) |
| 767 | Inputs.push_back(LHS); |
| 768 | if (Visited.insert(RHS).second) |
| 769 | Inputs.push_back(RHS); |
| 770 | continue; |
| 771 | } |
| 772 | if (auto *PN = dyn_cast<PHINode>(Input)) { |
| 773 | for (const Value *Op : PN->incoming_values()) { |
| 774 | Op = GetUnderlyingObject(Op, *DL); |
| 775 | if (Visited.insert(Op).second) |
| 776 | Inputs.push_back(Op); |
| 777 | } |
| 778 | continue; |
| 779 | } |
| 780 | |
Michael Kuperstein | 07f31d9 | 2015-08-11 08:06:44 +0000 | [diff] [blame^] | 781 | // FIXME: It would be good to handle other obvious no-alias cases here, but |
| 782 | // it isn't clear how to do so reasonbly without building a small version |
| 783 | // of BasicAA into this code. We could recurse into AliasAnalysis::alias |
| 784 | // here but that seems likely to go poorly as we're inside the |
| 785 | // implementation of such a query. Until then, just conservatievly retun |
| 786 | // false. |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 787 | return false; |
| 788 | } while (!Inputs.empty()); |
| 789 | |
| 790 | // If all the inputs to V were definitively no-alias, then V is no-alias. |
| 791 | return true; |
| 792 | } |
| 793 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 794 | /// alias - If one of the pointers is to a global that we are tracking, and the |
| 795 | /// other is some random pointer, we know there cannot be an alias, because the |
| 796 | /// address of the global isn't taken. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 797 | AliasResult GlobalsModRef::alias(const MemoryLocation &LocA, |
| 798 | const MemoryLocation &LocB) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 799 | // Get the base object these pointers point to. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 800 | const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL); |
| 801 | const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 802 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 803 | // If either of the underlying values is a global, they may be non-addr-taken |
| 804 | // globals, which we can answer queries about. |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 805 | const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); |
| 806 | const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 807 | if (GV1 || GV2) { |
| 808 | // If the global's address is taken, pretend we don't know it's a pointer to |
| 809 | // the global. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 810 | if (GV1 && !NonAddressTakenGlobals.count(GV1)) |
| 811 | GV1 = nullptr; |
| 812 | if (GV2 && !NonAddressTakenGlobals.count(GV2)) |
| 813 | GV2 = nullptr; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 814 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 815 | // If the two pointers are derived from two different non-addr-taken |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 816 | // globals we know these can't alias. |
| 817 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 818 | return NoAlias; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 819 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 820 | // If one is and the other isn't, it isn't strictly safe but we can fake |
| 821 | // this result if necessary for performance. This does not appear to be |
| 822 | // a common problem in practice. |
| 823 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 824 | if ((GV1 || GV2) && GV1 != GV2) |
| 825 | return NoAlias; |
| 826 | |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 827 | // Check for a special case where a non-escaping global can be used to |
| 828 | // conclude no-alias. |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 829 | if ((GV1 || GV2) && GV1 != GV2) { |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 830 | const GlobalValue *GV = GV1 ? GV1 : GV2; |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 831 | const Value *UV = GV1 ? UV2 : UV1; |
Chandler Carruth | 405e4f9 | 2015-08-05 17:58:30 +0000 | [diff] [blame] | 832 | if (isNonEscapingGlobalNoAlias(GV, UV)) |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 833 | return NoAlias; |
Chandler Carruth | 99ad7bb | 2015-07-28 11:11:11 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 836 | // Otherwise if they are both derived from the same addr-taken global, we |
| 837 | // can't know the two accesses don't overlap. |
| 838 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 839 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 840 | // These pointers may be based on the memory owned by an indirect global. If |
| 841 | // so, we may be able to handle this. First check to see if the base pointer |
| 842 | // is a direct load from an indirect global. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 843 | GV1 = GV2 = nullptr; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 844 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV1)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 845 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
| 846 | if (IndirectGlobals.count(GV)) |
| 847 | GV1 = GV; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 848 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV2)) |
| 849 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 850 | if (IndirectGlobals.count(GV)) |
| 851 | GV2 = GV; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 852 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 853 | // These pointers may also be from an allocation for the indirect global. If |
| 854 | // so, also handle them. |
Chandler Carruth | 56e2c62 | 2015-07-22 11:43:24 +0000 | [diff] [blame] | 855 | if (!GV1) |
| 856 | GV1 = AllocsForIndirectGlobals.lookup(UV1); |
| 857 | if (!GV2) |
| 858 | GV2 = AllocsForIndirectGlobals.lookup(UV2); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 859 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 860 | // 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] | 861 | // use this to disambiguate the pointers. If the pointers are based on |
| 862 | // different indirect globals they cannot alias. |
| 863 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 864 | return NoAlias; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 865 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 866 | // If one is based on an indirect global and the other isn't, it isn't |
| 867 | // strictly safe but we can fake this result if necessary for performance. |
| 868 | // This does not appear to be a common problem in practice. |
| 869 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 870 | if ((GV1 || GV2) && GV1 != GV2) |
| 871 | return NoAlias; |
| 872 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 873 | return AliasAnalysis::alias(LocA, LocB); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 876 | ModRefInfo GlobalsModRef::getModRefInfo(ImmutableCallSite CS, |
| 877 | const MemoryLocation &Loc) { |
| 878 | unsigned Known = MRI_ModRef; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 879 | |
| 880 | // 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] | 881 | // global we are tracking, return information if we have it. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 882 | const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout(); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 883 | if (const GlobalValue *GV = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 884 | dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL))) |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 885 | if (GV->hasLocalLinkage()) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 886 | if (const Function *F = CS.getCalledFunction()) |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 887 | if (NonAddressTakenGlobals.count(GV)) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 888 | if (const FunctionInfo *FI = getFunctionInfo(F)) |
| 889 | Known = FI->getModRefInfoForGlobal(*GV); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 890 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 891 | if (Known == MRI_NoModRef) |
| 892 | return MRI_NoModRef; // No need to query other mod/ref analyses |
| 893 | return ModRefInfo(Known & AliasAnalysis::getModRefInfo(CS, Loc)); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 894 | } |