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 | |
| 183 | private: |
Chandler Carruth | 8e4357d | 2015-07-23 07:50:52 +0000 | [diff] [blame^] | 184 | /// All of the information is encoded into a single pointer, with a three bit |
| 185 | /// integer in the low three bits. The high bit provides a flag for when this |
| 186 | /// function may read any global. The low two bits are the ModRefInfo. And |
| 187 | /// the pointer, when non-null, points to a map from GlobalValue to |
| 188 | /// ModRefInfo specific to that GlobalValue. |
| 189 | PointerIntPair<AlignedMap *, 3, unsigned, AlignedMapPointerTraits> Info; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | /// GlobalsModRef - The actual analysis pass. |
| 193 | class GlobalsModRef : public ModulePass, public AliasAnalysis { |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 194 | /// The globals that do not have their addresses taken. |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 195 | SmallPtrSet<const GlobalValue *, 8> NonAddressTakenGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 196 | |
| 197 | /// IndirectGlobals - The memory pointed to by this global is known to be |
| 198 | /// 'owned' by the global. |
Chandler Carruth | f3af4af | 2015-07-22 11:47:54 +0000 | [diff] [blame] | 199 | SmallPtrSet<const GlobalValue *, 8> IndirectGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 200 | |
| 201 | /// AllocsForIndirectGlobals - If an instruction allocates memory for an |
| 202 | /// indirect global, this map indicates which one. |
Chandler Carruth | 6919267 | 2015-07-22 11:36:09 +0000 | [diff] [blame] | 203 | DenseMap<const Value *, const GlobalValue *> AllocsForIndirectGlobals; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 204 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 205 | /// For each function, keep track of what globals are modified or read. |
| 206 | DenseMap<const Function *, FunctionInfo> FunctionInfos; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 207 | |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 208 | /// Handle to clear this analysis on deletion of values. |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 209 | struct DeletionCallbackHandle final : CallbackVH { |
| 210 | GlobalsModRef &GMR; |
| 211 | std::list<DeletionCallbackHandle>::iterator I; |
| 212 | |
| 213 | DeletionCallbackHandle(GlobalsModRef &GMR, Value *V) |
| 214 | : CallbackVH(V), GMR(GMR) {} |
| 215 | |
| 216 | void deleted() override { |
| 217 | Value *V = getValPtr(); |
| 218 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 219 | if (GMR.NonAddressTakenGlobals.erase(GV)) { |
| 220 | // This global might be an indirect global. If so, remove it and |
| 221 | // remove |
| 222 | // any AllocRelatedValues for it. |
| 223 | if (GMR.IndirectGlobals.erase(GV)) { |
| 224 | // Remove any entries in AllocsForIndirectGlobals for this global. |
Chandler Carruth | 6919267 | 2015-07-22 11:36:09 +0000 | [diff] [blame] | 225 | for (auto I = GMR.AllocsForIndirectGlobals.begin(), |
| 226 | E = GMR.AllocsForIndirectGlobals.end(); |
| 227 | I != E; ++I) |
| 228 | if (I->second == GV) |
| 229 | GMR.AllocsForIndirectGlobals.erase(I); |
Chandler Carruth | 8f1b63e | 2015-07-22 11:10:41 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // If this is an allocation related to an indirect global, remove it. |
| 235 | GMR.AllocsForIndirectGlobals.erase(V); |
| 236 | |
| 237 | // And clear out the handle. |
| 238 | setValPtr(nullptr); |
| 239 | GMR.Handles.erase(I); |
| 240 | // This object is now destroyed! |
| 241 | } |
| 242 | }; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 243 | |
| 244 | /// List of callbacks for globals being tracked by this analysis. Note that |
| 245 | /// these objects are quite large, but we only anticipate having one per |
| 246 | /// global tracked by this analysis. There are numerous optimizations we |
| 247 | /// could perform to the memory utilization here if this becomes a problem. |
| 248 | std::list<DeletionCallbackHandle> Handles; |
| 249 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 250 | public: |
| 251 | static char ID; |
| 252 | GlobalsModRef() : ModulePass(ID) { |
| 253 | initializeGlobalsModRefPass(*PassRegistry::getPassRegistry()); |
| 254 | } |
| 255 | |
| 256 | bool runOnModule(Module &M) override { |
| 257 | InitializeAliasAnalysis(this, &M.getDataLayout()); |
| 258 | |
| 259 | // Find non-addr taken globals. |
| 260 | AnalyzeGlobals(M); |
| 261 | |
| 262 | // Propagate on CG. |
| 263 | AnalyzeCallGraph(getAnalysis<CallGraphWrapperPass>().getCallGraph(), M); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 268 | AliasAnalysis::getAnalysisUsage(AU); |
| 269 | AU.addRequired<CallGraphWrapperPass>(); |
| 270 | AU.setPreservesAll(); // Does not transform code |
| 271 | } |
| 272 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 273 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 274 | /// an analysis interface through multiple inheritance. If needed, it |
| 275 | /// should override this to adjust the this pointer as needed for the |
| 276 | /// specified pass info. |
| 277 | void *getAdjustedAnalysisPointer(AnalysisID PI) override { |
| 278 | if (PI == &AliasAnalysis::ID) |
| 279 | return (AliasAnalysis *)this; |
| 280 | return this; |
| 281 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 282 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 283 | //------------------------------------------------ |
| 284 | // Implement the AliasAnalysis API |
| 285 | // |
| 286 | AliasResult alias(const MemoryLocation &LocA, |
| 287 | const MemoryLocation &LocB) override; |
| 288 | ModRefInfo getModRefInfo(ImmutableCallSite CS, |
| 289 | const MemoryLocation &Loc) override; |
| 290 | ModRefInfo getModRefInfo(ImmutableCallSite CS1, |
| 291 | ImmutableCallSite CS2) override { |
| 292 | return AliasAnalysis::getModRefInfo(CS1, CS2); |
| 293 | } |
| 294 | |
| 295 | /// getModRefBehavior - Return the behavior of the specified function if |
| 296 | /// called from the specified call site. The call site may be null in which |
| 297 | /// case the most generic behavior of this function should be returned. |
| 298 | FunctionModRefBehavior getModRefBehavior(const Function *F) override { |
| 299 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 300 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 301 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 302 | if (FI->getModRefInfo() == MRI_NoModRef) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 303 | Min = FMRB_DoesNotAccessMemory; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 304 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 305 | Min = FMRB_OnlyReadsMemory; |
| 306 | } |
| 307 | |
| 308 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(F) & Min); |
| 309 | } |
| 310 | |
| 311 | /// getModRefBehavior - Return the behavior of the specified function if |
| 312 | /// called from the specified call site. The call site may be null in which |
| 313 | /// case the most generic behavior of this function should be returned. |
| 314 | FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS) override { |
| 315 | FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior; |
| 316 | |
| 317 | if (const Function *F = CS.getCalledFunction()) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 318 | if (FunctionInfo *FI = getFunctionInfo(F)) { |
| 319 | if (FI->getModRefInfo() == MRI_NoModRef) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 320 | Min = FMRB_DoesNotAccessMemory; |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 321 | else if ((FI->getModRefInfo() & MRI_Mod) == 0) |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 322 | Min = FMRB_OnlyReadsMemory; |
| 323 | } |
| 324 | |
| 325 | return FunctionModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min); |
| 326 | } |
| 327 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 328 | private: |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 329 | /// Returns the function info for the function, or null if we don't have |
| 330 | /// anything useful to say about it. |
| 331 | FunctionInfo *getFunctionInfo(const Function *F) { |
| 332 | auto I = FunctionInfos.find(F); |
| 333 | if (I != FunctionInfos.end()) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 334 | return &I->second; |
| 335 | return nullptr; |
| 336 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 337 | |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 338 | void AnalyzeGlobals(Module &M); |
| 339 | void AnalyzeCallGraph(CallGraph &CG, Module &M); |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 340 | bool AnalyzeUsesOfPointer(Value *V, |
| 341 | SmallPtrSetImpl<Function *> *Readers = nullptr, |
| 342 | SmallPtrSetImpl<Function *> *Writers = nullptr, |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 343 | GlobalValue *OkayStoreDest = nullptr); |
| 344 | bool AnalyzeIndirectGlobalMemory(GlobalValue *GV); |
| 345 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 347 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 348 | char GlobalsModRef::ID = 0; |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 349 | INITIALIZE_AG_PASS_BEGIN(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 350 | "Simple mod/ref analysis for globals", false, true, |
| 351 | false) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 352 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 353 | INITIALIZE_AG_PASS_END(GlobalsModRef, AliasAnalysis, "globalsmodref-aa", |
| 354 | "Simple mod/ref analysis for globals", false, true, |
| 355 | false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 357 | Pass *llvm::createGlobalsModRefPass() { return new GlobalsModRef(); } |
| 358 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 359 | /// AnalyzeGlobals - Scan through the users of all of the internal |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 360 | /// 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] | 361 | /// (really, their address passed to something nontrivial), record this fact, |
| 362 | /// and record the functions that they are used directly in. |
| 363 | void GlobalsModRef::AnalyzeGlobals(Module &M) { |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 364 | for (Function &F : M) |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 365 | if (F.hasLocalLinkage()) |
| 366 | if (!AnalyzeUsesOfPointer(&F)) { |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 367 | // Remember that we are tracking this global. |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 368 | NonAddressTakenGlobals.insert(&F); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 369 | Handles.emplace_front(*this, &F); |
| 370 | Handles.front().I = Handles.begin(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 371 | ++NumNonAddrTakenFunctions; |
| 372 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 373 | |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 374 | SmallPtrSet<Function *, 64> Readers, Writers; |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 375 | for (GlobalVariable &GV : M.globals()) |
| 376 | if (GV.hasLocalLinkage()) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 377 | if (!AnalyzeUsesOfPointer(&GV, &Readers, |
| 378 | GV.isConstant() ? nullptr : &Writers)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 379 | // Remember that we are tracking this global, and the mod/ref fns |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 380 | NonAddressTakenGlobals.insert(&GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 381 | Handles.emplace_front(*this, &GV); |
| 382 | Handles.front().I = Handles.begin(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 383 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 384 | for (Function *Reader : Readers) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 385 | FunctionInfos[Reader].addModRefInfoForGlobal(GV, MRI_Ref); |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 386 | |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 387 | if (!GV.isConstant()) // No need to keep track of writers to constants |
| 388 | for (Function *Writer : Writers) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 389 | FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 390 | ++NumNonAddrTakenGlobalVars; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 391 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 392 | // 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] | 393 | if (GV.getType()->getElementType()->isPointerTy() && |
| 394 | AnalyzeIndirectGlobalMemory(&GV)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 395 | ++NumIndirectGlobalVars; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 396 | } |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 397 | Readers.clear(); |
| 398 | Writers.clear(); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 402 | /// AnalyzeUsesOfPointer - Look at all of the users of the specified pointer. |
| 403 | /// If this is used by anything complex (i.e., the address escapes), return |
| 404 | /// true. Also, while we are at it, keep track of those functions that read and |
| 405 | /// write to the value. |
| 406 | /// |
| 407 | /// If OkayStoreDest is non-null, stores into this global are allowed. |
| 408 | bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V, |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 409 | SmallPtrSetImpl<Function *> *Readers, |
| 410 | SmallPtrSetImpl<Function *> *Writers, |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 411 | GlobalValue *OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 412 | if (!V->getType()->isPointerTy()) |
| 413 | return true; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 414 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 415 | for (Use &U : V->uses()) { |
| 416 | User *I = U.getUser(); |
| 417 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 418 | if (Readers) |
| 419 | Readers->insert(LI->getParent()->getParent()); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 420 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 421 | if (V == SI->getOperand(1)) { |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 422 | if (Writers) |
| 423 | Writers->insert(SI->getParent()->getParent()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 424 | } else if (SI->getOperand(1) != OkayStoreDest) { |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 425 | return true; // Storing the pointer |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 426 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 427 | } else if (Operator::getOpcode(I) == Instruction::GetElementPtr) { |
| 428 | if (AnalyzeUsesOfPointer(I, Readers, Writers)) |
Victor Hernandez | 537d8d9 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 429 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 430 | } else if (Operator::getOpcode(I) == Instruction::BitCast) { |
| 431 | if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest)) |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 432 | return true; |
Benjamin Kramer | 3a09ef6 | 2015-04-10 14:50:08 +0000 | [diff] [blame] | 433 | } else if (auto CS = CallSite(I)) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 434 | // Make sure that this is just the function being called, not that it is |
| 435 | // passing into the function. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 436 | if (!CS.isCallee(&U)) { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 437 | // Detect calls to free. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 438 | if (isFreeCall(I, TLI)) { |
| 439 | if (Writers) |
| 440 | Writers->insert(CS->getParent()->getParent()); |
| 441 | } else { |
Benjamin Kramer | b8266d2 | 2014-02-10 14:17:30 +0000 | [diff] [blame] | 442 | return true; // Argument of an unknown call. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 443 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 444 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 445 | } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 446 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 447 | return true; // Allow comparison against null. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 448 | } else { |
| 449 | return true; |
| 450 | } |
Gabor Greif | 070b9a2 | 2010-07-09 15:53:42 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 456 | /// AnalyzeIndirectGlobalMemory - We found an non-address-taken global variable |
| 457 | /// which holds a pointer type. See if the global always points to non-aliased |
| 458 | /// heap memory: that is, all initializers of the globals are allocations, and |
| 459 | /// those allocations have no use other than initialization of the global. |
| 460 | /// Further, all loads out of GV must directly use the memory, not store the |
| 461 | /// pointer somewhere. If this is true, we consider the memory pointed to by |
| 462 | /// GV to be owned by GV and can disambiguate other pointers from it. |
| 463 | bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { |
| 464 | // Keep track of values related to the allocation of the memory, f.e. the |
| 465 | // value produced by the malloc call and any casts. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 466 | std::vector<Value *> AllocRelatedValues; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 467 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 468 | // Walk the user list of the global. If we find anything other than a direct |
| 469 | // load or store, bail out. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 470 | for (User *U : GV->users()) { |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 471 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 472 | // The pointer loaded from the global can only be used in simple ways: |
| 473 | // we allow addressing of it and loading storing to it. We do *not* allow |
| 474 | // storing the loaded pointer somewhere else or passing to a function. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 475 | if (AnalyzeUsesOfPointer(LI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 476 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 477 | // TODO: Could try some IP mod/ref of the loaded pointer. |
Gabor Greif | aa389f5 | 2010-07-09 16:22:36 +0000 | [diff] [blame] | 478 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 479 | // Storing the global itself. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 480 | if (SI->getOperand(0) == GV) |
| 481 | return false; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 482 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 483 | // If storing the null pointer, ignore it. |
| 484 | if (isa<ConstantPointerNull>(SI->getOperand(0))) |
| 485 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 486 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 487 | // Check the value being stored. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 488 | Value *Ptr = GetUnderlyingObject(SI->getOperand(0), |
| 489 | GV->getParent()->getDataLayout()); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 490 | |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 491 | if (!isAllocLikeFn(Ptr, TLI)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 492 | return false; // Too hard to analyze. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 493 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 494 | // Analyze all uses of the allocation. If any of them are used in a |
| 495 | // non-simple way (e.g. stored to another global) bail out. |
Chandler Carruth | 4cef26e | 2015-07-22 22:10:05 +0000 | [diff] [blame] | 496 | if (AnalyzeUsesOfPointer(Ptr, /*Readers*/ nullptr, /*Writers*/ nullptr, |
| 497 | GV)) |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 498 | return false; // Loaded pointer escapes. |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 499 | |
| 500 | // Remember that this allocation is related to the indirect global. |
| 501 | AllocRelatedValues.push_back(Ptr); |
| 502 | } else { |
| 503 | // Something complex, bail out. |
| 504 | return false; |
| 505 | } |
| 506 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 507 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 508 | // Okay, this is an indirect global. Remember all of the allocations for |
| 509 | // this global in AllocsForIndirectGlobals. |
| 510 | while (!AllocRelatedValues.empty()) { |
| 511 | AllocsForIndirectGlobals[AllocRelatedValues.back()] = GV; |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 512 | Handles.emplace_front(*this, AllocRelatedValues.back()); |
| 513 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 514 | AllocRelatedValues.pop_back(); |
| 515 | } |
| 516 | IndirectGlobals.insert(GV); |
Chandler Carruth | da7c191 | 2015-07-22 09:27:58 +0000 | [diff] [blame] | 517 | Handles.emplace_front(*this, GV); |
| 518 | Handles.front().I = Handles.begin(); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 519 | return true; |
| 520 | } |
| 521 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 522 | /// AnalyzeCallGraph - At this point, we know the functions where globals are |
| 523 | /// immediately stored to and read from. Propagate this information up the call |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 524 | /// 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] | 525 | /// function. |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 526 | void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) { |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 527 | // We do a bottom-up SCC traversal of the call graph. In other words, we |
| 528 | // visit all callees before callers (leaf-first). |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 529 | 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] | 530 | const std::vector<CallGraphNode *> &SCC = *I; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 531 | assert(!SCC.empty() && "SCC with no functions?"); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 532 | |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 533 | if (!SCC[0]->getFunction()) { |
| 534 | // Calls externally - can't say anything useful. Remove any existing |
| 535 | // function records (may have been created when scanning globals). |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 536 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 537 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 538 | continue; |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 541 | FunctionInfo &FI = FunctionInfos[SCC[0]->getFunction()]; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 542 | bool KnowNothing = false; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 543 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 544 | // Collect the mod/ref properties due to called functions. We only compute |
| 545 | // one mod-ref set. |
| 546 | for (unsigned i = 0, e = SCC.size(); i != e && !KnowNothing; ++i) { |
| 547 | Function *F = SCC[i]->getFunction(); |
| 548 | if (!F) { |
| 549 | KnowNothing = true; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 550 | break; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 551 | } |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 552 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 553 | if (F->isDeclaration()) { |
| 554 | // Try to get mod/ref behaviour from function attributes. |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 555 | if (F->doesNotAccessMemory()) { |
| 556 | // Can't do better than that! |
| 557 | } else if (F->onlyReadsMemory()) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 558 | FI.addModRefInfo(MRI_Ref); |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 559 | if (!F->isIntrinsic()) |
Duncan Sands | e30b36f | 2008-09-11 15:43:12 +0000 | [diff] [blame] | 560 | // This function might call back into the module and read a global - |
Duncan Sands | 06dbb12 | 2008-09-12 07:29:58 +0000 | [diff] [blame] | 561 | // consider every global as possibly being read by this function. |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 562 | FI.setMayReadAnyGlobal(); |
Duncan Sands | 0eca057 | 2008-09-03 15:31:24 +0000 | [diff] [blame] | 563 | } else { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 564 | FI.addModRefInfo(MRI_ModRef); |
Duncan Sands | d4133ac | 2008-09-11 19:35:55 +0000 | [diff] [blame] | 565 | // Can't say anything useful unless it's an intrinsic - they don't |
| 566 | // read or write global variables of the kind considered here. |
| 567 | KnowNothing = !F->isIntrinsic(); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 568 | } |
| 569 | continue; |
| 570 | } |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 571 | |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 572 | for (CallGraphNode::iterator CI = SCC[i]->begin(), E = SCC[i]->end(); |
Duncan Sands | 21a5799 | 2008-09-04 19:16:20 +0000 | [diff] [blame] | 573 | CI != E && !KnowNothing; ++CI) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 574 | if (Function *Callee = CI->second->getFunction()) { |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 575 | if (FunctionInfo *CalleeFI = getFunctionInfo(Callee)) { |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 576 | // Propagate function effect up. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 577 | FI.addFunctionInfo(*CalleeFI); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 578 | } else { |
| 579 | // Can't say anything about it. However, if it is inside our SCC, |
| 580 | // then nothing needs to be done. |
| 581 | CallGraphNode *CalleeNode = CG[Callee]; |
| 582 | if (std::find(SCC.begin(), SCC.end(), CalleeNode) == SCC.end()) |
| 583 | KnowNothing = true; |
| 584 | } |
| 585 | } else { |
| 586 | KnowNothing = true; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // 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] | 591 | // from the FunctionInfos map. |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 592 | if (KnowNothing) { |
Chandler Carruth | a033bbb | 2015-07-15 08:09:23 +0000 | [diff] [blame] | 593 | for (auto *Node : SCC) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 594 | FunctionInfos.erase(Node->getFunction()); |
Duncan Sands | e74d750 | 2008-09-03 16:10:55 +0000 | [diff] [blame] | 595 | continue; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // Scan the function bodies for explicit loads or stores. |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 599 | for (auto *Node : SCC) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 600 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 601 | break; // The mod/ref lattice saturates here. |
| 602 | for (Instruction &I : inst_range(Node->getFunction())) { |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 603 | if (FI.getModRefInfo() == MRI_ModRef) |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 604 | break; // The mod/ref lattice saturates here. |
| 605 | |
| 606 | // We handle calls specially because the graph-relevant aspects are |
| 607 | // handled above. |
| 608 | if (auto CS = CallSite(&I)) { |
| 609 | if (isAllocationFn(&I, TLI) || isFreeCall(&I, TLI)) { |
| 610 | // FIXME: It is completely unclear why this is necessary and not |
| 611 | // handled by the above graph code. |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 612 | FI.addModRefInfo(MRI_ModRef); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 613 | } else if (Function *Callee = CS.getCalledFunction()) { |
| 614 | // The callgraph doesn't include intrinsic calls. |
| 615 | if (Callee->isIntrinsic()) { |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 616 | FunctionModRefBehavior Behaviour = |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 617 | AliasAnalysis::getModRefBehavior(Callee); |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 618 | FI.addModRefInfo(ModRefInfo(Behaviour & MRI_ModRef)); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | continue; |
Duncan Sands | 9ddb314 | 2008-09-13 12:45:50 +0000 | [diff] [blame] | 622 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 623 | |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 624 | // All non-call instructions we use the primary predicates for whether |
| 625 | // thay read or write memory. |
| 626 | if (I.mayReadFromMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 627 | FI.addModRefInfo(MRI_Ref); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 628 | if (I.mayWriteToMemory()) |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 629 | FI.addModRefInfo(MRI_Mod); |
Chandler Carruth | 6af95d0 | 2015-07-15 08:53:29 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 633 | if ((FI.getModRefInfo() & MRI_Mod) == 0) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 634 | ++NumReadMemFunctions; |
Chandler Carruth | dbb4622 | 2015-07-23 00:12:32 +0000 | [diff] [blame] | 635 | if (FI.getModRefInfo() == MRI_NoModRef) |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 636 | ++NumNoMemFunctions; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 637 | |
| 638 | // Finally, now that we know the full effect on this SCC, clone the |
| 639 | // information to each function in the SCC. |
| 640 | for (unsigned i = 1, e = SCC.size(); i != e; ++i) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 641 | FunctionInfos[SCC[i]->getFunction()] = FI; |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 642 | } |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 645 | /// alias - If one of the pointers is to a global that we are tracking, and the |
| 646 | /// other is some random pointer, we know there cannot be an alias, because the |
| 647 | /// address of the global isn't taken. |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 648 | AliasResult GlobalsModRef::alias(const MemoryLocation &LocA, |
| 649 | const MemoryLocation &LocB) { |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 650 | // Get the base object these pointers point to. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 651 | const Value *UV1 = GetUnderlyingObject(LocA.Ptr, *DL); |
| 652 | const Value *UV2 = GetUnderlyingObject(LocB.Ptr, *DL); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 653 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 654 | // If either of the underlying values is a global, they may be non-addr-taken |
| 655 | // globals, which we can answer queries about. |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 656 | const GlobalValue *GV1 = dyn_cast<GlobalValue>(UV1); |
| 657 | const GlobalValue *GV2 = dyn_cast<GlobalValue>(UV2); |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 658 | if (GV1 || GV2) { |
| 659 | // If the global's address is taken, pretend we don't know it's a pointer to |
| 660 | // the global. |
Chandler Carruth | 466d7ad | 2015-07-14 08:42:39 +0000 | [diff] [blame] | 661 | if (GV1 && !NonAddressTakenGlobals.count(GV1)) |
| 662 | GV1 = nullptr; |
| 663 | if (GV2 && !NonAddressTakenGlobals.count(GV2)) |
| 664 | GV2 = nullptr; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 665 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 666 | // If the two pointers are derived from two different non-addr-taken |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 667 | // globals we know these can't alias. |
| 668 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 669 | return NoAlias; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 670 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 671 | // If one is and the other isn't, it isn't strictly safe but we can fake |
| 672 | // this result if necessary for performance. This does not appear to be |
| 673 | // a common problem in practice. |
| 674 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 675 | if ((GV1 || GV2) && GV1 != GV2) |
| 676 | return NoAlias; |
| 677 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 678 | // Otherwise if they are both derived from the same addr-taken global, we |
| 679 | // can't know the two accesses don't overlap. |
| 680 | } |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 681 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 682 | // These pointers may be based on the memory owned by an indirect global. If |
| 683 | // so, we may be able to handle this. First check to see if the base pointer |
| 684 | // is a direct load from an indirect global. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 685 | GV1 = GV2 = nullptr; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 686 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV1)) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 687 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
| 688 | if (IndirectGlobals.count(GV)) |
| 689 | GV1 = GV; |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 690 | if (const LoadInst *LI = dyn_cast<LoadInst>(UV2)) |
| 691 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(LI->getOperand(0))) |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 692 | if (IndirectGlobals.count(GV)) |
| 693 | GV2 = GV; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 694 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 695 | // These pointers may also be from an allocation for the indirect global. If |
| 696 | // so, also handle them. |
Chandler Carruth | 56e2c62 | 2015-07-22 11:43:24 +0000 | [diff] [blame] | 697 | if (!GV1) |
| 698 | GV1 = AllocsForIndirectGlobals.lookup(UV1); |
| 699 | if (!GV2) |
| 700 | GV2 = AllocsForIndirectGlobals.lookup(UV2); |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 701 | |
Chris Lattner | bfdd19b | 2006-10-01 22:36:45 +0000 | [diff] [blame] | 702 | // 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] | 703 | // use this to disambiguate the pointers. If the pointers are based on |
| 704 | // different indirect globals they cannot alias. |
| 705 | if (GV1 && GV2 && GV1 != GV2) |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 706 | return NoAlias; |
Duncan Sands | 42c644e | 2008-09-03 12:55:42 +0000 | [diff] [blame] | 707 | |
Chandler Carruth | f55803f | 2015-07-17 06:58:24 +0000 | [diff] [blame] | 708 | // If one is based on an indirect global and the other isn't, it isn't |
| 709 | // strictly safe but we can fake this result if necessary for performance. |
| 710 | // This does not appear to be a common problem in practice. |
| 711 | if (EnableUnsafeGlobalsModRefAliasResults) |
| 712 | if ((GV1 || GV2) && GV1 != GV2) |
| 713 | return NoAlias; |
| 714 | |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 715 | return AliasAnalysis::alias(LocA, LocB); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 718 | ModRefInfo GlobalsModRef::getModRefInfo(ImmutableCallSite CS, |
| 719 | const MemoryLocation &Loc) { |
| 720 | unsigned Known = MRI_ModRef; |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 721 | |
| 722 | // 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] | 723 | // global we are tracking, return information if we have it. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 724 | const DataLayout &DL = CS.getCaller()->getParent()->getDataLayout(); |
Dan Gohman | 41f14cf | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 725 | if (const GlobalValue *GV = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 726 | dyn_cast<GlobalValue>(GetUnderlyingObject(Loc.Ptr, DL))) |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 727 | if (GV->hasLocalLinkage()) |
Dan Gohman | 5442c71 | 2010-08-03 21:48:53 +0000 | [diff] [blame] | 728 | if (const Function *F = CS.getCalledFunction()) |
Chris Lattner | 3a353e8 | 2004-07-27 06:40:37 +0000 | [diff] [blame] | 729 | if (NonAddressTakenGlobals.count(GV)) |
Chandler Carruth | 5657b73 | 2015-07-22 23:56:31 +0000 | [diff] [blame] | 730 | if (const FunctionInfo *FI = getFunctionInfo(F)) |
| 731 | Known = FI->getModRefInfoForGlobal(*GV); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 732 | |
Chandler Carruth | 194f59c | 2015-07-22 23:15:57 +0000 | [diff] [blame] | 733 | if (Known == MRI_NoModRef) |
| 734 | return MRI_NoModRef; // No need to query other mod/ref analyses |
| 735 | return ModRefInfo(Known & AliasAnalysis::getModRefInfo(CS, Loc)); |
Chris Lattner | 26dff50 | 2004-06-28 06:33:13 +0000 | [diff] [blame] | 736 | } |