Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1 | //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===// |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file implements interprocedural passes which walk the |
| 12 | /// call-graph deducing and/or propagating function attributes. |
| 13 | /// |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO.h" |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SCCIterator.h" |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SetVector.h" |
Duncan Sands | b193a37 | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
James Molloy | 0ecdbe7 | 2015-11-19 08:49:57 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AssumptionCache.h" |
| 24 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 31 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IntrinsicInst.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Hans Wennborg | 043bf5b | 2015-08-31 21:19:18 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "functionattrs" |
| 40 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 41 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 42 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 43 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 44 | STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); |
| 45 | STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 46 | STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 47 | STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 48 | STATISTIC(NumNoRecurse, "Number of functions marked as norecurse"); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 49 | |
| 50 | namespace { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 51 | typedef SmallSetVector<Function *, 8> SCCNodeSet; |
| 52 | } |
| 53 | |
| 54 | namespace { |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 55 | struct PostOrderFunctionAttrs : public CallGraphSCCPass { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 56 | static char ID; // Pass identification, replacement for typeid |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 57 | PostOrderFunctionAttrs() : CallGraphSCCPass(ID) { |
| 58 | initializePostOrderFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 61 | bool runOnSCC(CallGraphSCC &SCC) override; |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 62 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 63 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 64 | AU.setPreservesCFG(); |
| 65 | AU.addRequired<AssumptionCacheTracker>(); |
| 66 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Sanjoy Das | 1c481f5 | 2016-02-09 01:21:57 +0000 | [diff] [blame] | 67 | addUsedAAAnalyses(AU); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 68 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 69 | } |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 71 | private: |
| 72 | TargetLibraryInfo *TLI; |
| 73 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 74 | } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 75 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 76 | char PostOrderFunctionAttrs::ID = 0; |
| 77 | INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrs, "functionattrs", |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 78 | "Deduce function attributes", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 79 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 80 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 81 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 82 | INITIALIZE_PASS_END(PostOrderFunctionAttrs, "functionattrs", |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 83 | "Deduce function attributes", false, false) |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 84 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 85 | Pass *llvm::createPostOrderFunctionAttrsPass() { return new PostOrderFunctionAttrs(); } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 87 | namespace { |
| 88 | /// The three kinds of memory access relevant to 'readonly' and |
| 89 | /// 'readnone' attributes. |
| 90 | enum MemoryAccessKind { |
| 91 | MAK_ReadNone = 0, |
| 92 | MAK_ReadOnly = 1, |
| 93 | MAK_MayWrite = 2 |
| 94 | }; |
| 95 | } |
| 96 | |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 97 | static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR, |
| 98 | const SCCNodeSet &SCCNodes) { |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 99 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); |
| 100 | if (MRB == FMRB_DoesNotAccessMemory) |
| 101 | // Already perfect! |
| 102 | return MAK_ReadNone; |
| 103 | |
| 104 | // Definitions with weak linkage may be overridden at linktime with |
| 105 | // something that writes memory, so treat them like declarations. |
| 106 | if (F.isDeclaration() || F.mayBeOverridden()) { |
| 107 | if (AliasAnalysis::onlyReadsMemory(MRB)) |
| 108 | return MAK_ReadOnly; |
| 109 | |
| 110 | // Conservatively assume it writes to memory. |
| 111 | return MAK_MayWrite; |
| 112 | } |
| 113 | |
| 114 | // Scan the function body for instructions that may read or write memory. |
| 115 | bool ReadsMemory = false; |
| 116 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 117 | Instruction *I = &*II; |
| 118 | |
| 119 | // Some instructions can be ignored even if they read or write memory. |
| 120 | // Detect these now, skipping to the next instruction if one is found. |
| 121 | CallSite CS(cast<Value>(I)); |
| 122 | if (CS) { |
Sanjoy Das | 10c8a04 | 2016-02-09 18:40:40 +0000 | [diff] [blame] | 123 | // Ignore calls to functions in the same SCC, as long as the call sites |
| 124 | // don't have operand bundles. Calls with operand bundles are allowed to |
| 125 | // have memory effects not described by the memory effects of the call |
| 126 | // target. |
| 127 | if (!CS.hasOperandBundles() && CS.getCalledFunction() && |
| 128 | SCCNodes.count(CS.getCalledFunction())) |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 129 | continue; |
| 130 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS); |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 131 | |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 132 | // If the call doesn't access memory, we're done. |
| 133 | if (!(MRB & MRI_ModRef)) |
| 134 | continue; |
| 135 | |
| 136 | if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 137 | // The call could access any memory. If that includes writes, give up. |
| 138 | if (MRB & MRI_Mod) |
| 139 | return MAK_MayWrite; |
| 140 | // If it reads, note it. |
| 141 | if (MRB & MRI_Ref) |
| 142 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 143 | continue; |
| 144 | } |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 145 | |
| 146 | // Check whether all pointer arguments point to local memory, and |
| 147 | // ignore calls that only access local memory. |
| 148 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 149 | CI != CE; ++CI) { |
| 150 | Value *Arg = *CI; |
Elena Demikhovsky | 3ec9e15 | 2015-11-17 19:30:51 +0000 | [diff] [blame] | 151 | if (!Arg->getType()->isPtrOrPtrVectorTy()) |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 152 | continue; |
| 153 | |
| 154 | AAMDNodes AAInfo; |
| 155 | I->getAAMetadata(AAInfo); |
| 156 | MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); |
| 157 | |
| 158 | // Skip accesses to local or constant memory as they don't impact the |
| 159 | // externally visible mod/ref behavior. |
| 160 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 161 | continue; |
| 162 | |
| 163 | if (MRB & MRI_Mod) |
| 164 | // Writes non-local memory. Give up. |
| 165 | return MAK_MayWrite; |
| 166 | if (MRB & MRI_Ref) |
| 167 | // Ok, it reads non-local memory. |
| 168 | ReadsMemory = true; |
| 169 | } |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 170 | continue; |
| 171 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 172 | // Ignore non-volatile loads from local memory. (Atomic is okay here.) |
| 173 | if (!LI->isVolatile()) { |
| 174 | MemoryLocation Loc = MemoryLocation::get(LI); |
| 175 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 176 | continue; |
| 177 | } |
| 178 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 179 | // Ignore non-volatile stores to local memory. (Atomic is okay here.) |
| 180 | if (!SI->isVolatile()) { |
| 181 | MemoryLocation Loc = MemoryLocation::get(SI); |
| 182 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 183 | continue; |
| 184 | } |
| 185 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { |
| 186 | // Ignore vaargs on local memory. |
| 187 | MemoryLocation Loc = MemoryLocation::get(VI); |
| 188 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | // Any remaining instructions need to be taken seriously! Check if they |
| 193 | // read or write memory. |
| 194 | if (I->mayWriteToMemory()) |
| 195 | // Writes memory. Just give up. |
| 196 | return MAK_MayWrite; |
| 197 | |
| 198 | // If this instruction may read memory, remember that. |
| 199 | ReadsMemory |= I->mayReadFromMemory(); |
| 200 | } |
| 201 | |
| 202 | return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; |
| 203 | } |
| 204 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 205 | /// Deduce readonly/readnone attributes for the SCC. |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 206 | template <typename AARGetterT> |
| 207 | static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 208 | // Check if any of the functions in the SCC read or write memory. If they |
| 209 | // write memory then they can't be marked readnone or readonly. |
| 210 | bool ReadsMemory = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 211 | for (Function *F : SCCNodes) { |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 212 | // Call the callable parameter to look up AA results for this function. |
| 213 | AAResults &AAR = AARGetter(*F); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 214 | |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 215 | switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) { |
| 216 | case MAK_MayWrite: |
| 217 | return false; |
| 218 | case MAK_ReadOnly: |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 219 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 220 | break; |
| 221 | case MAK_ReadNone: |
| 222 | // Nothing to do! |
| 223 | break; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 228 | // Give them the appropriate attribute. |
| 229 | bool MadeChange = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 230 | for (Function *F : SCCNodes) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 231 | if (F->doesNotAccessMemory()) |
| 232 | // Already perfect! |
| 233 | continue; |
| 234 | |
| 235 | if (F->onlyReadsMemory() && ReadsMemory) |
| 236 | // No change. |
| 237 | continue; |
| 238 | |
| 239 | MadeChange = true; |
| 240 | |
| 241 | // Clear out any existing attributes. |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 242 | AttrBuilder B; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 243 | B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
| 244 | F->removeAttributes( |
| 245 | AttributeSet::FunctionIndex, |
| 246 | AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B)); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 247 | |
| 248 | // Add in the new attribute. |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 249 | F->addAttribute(AttributeSet::FunctionIndex, |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 250 | ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 251 | |
| 252 | if (ReadsMemory) |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 253 | ++NumReadOnly; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 254 | else |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 255 | ++NumReadNone; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return MadeChange; |
| 259 | } |
| 260 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 261 | namespace { |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 262 | /// For a given pointer Argument, this retains a list of Arguments of functions |
| 263 | /// in the same SCC that the pointer data flows into. We use this to build an |
| 264 | /// SCC of the arguments. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 265 | struct ArgumentGraphNode { |
| 266 | Argument *Definition; |
| 267 | SmallVector<ArgumentGraphNode *, 4> Uses; |
| 268 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 269 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 270 | class ArgumentGraph { |
| 271 | // We store pointers to ArgumentGraphNode objects, so it's important that |
| 272 | // that they not move around upon insert. |
| 273 | typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 274 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 275 | ArgumentMapTy ArgumentMap; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 276 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 277 | // There is no root node for the argument graph, in fact: |
| 278 | // void f(int *x, int *y) { if (...) f(x, y); } |
| 279 | // is an example where the graph is disconnected. The SCCIterator requires a |
| 280 | // single entry point, so we maintain a fake ("synthetic") root node that |
| 281 | // uses every node. Because the graph is directed and nothing points into |
| 282 | // the root, it will not participate in any SCCs (except for its own). |
| 283 | ArgumentGraphNode SyntheticRoot; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 284 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 285 | public: |
| 286 | ArgumentGraph() { SyntheticRoot.Definition = nullptr; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 287 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 288 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 289 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 290 | iterator begin() { return SyntheticRoot.Uses.begin(); } |
| 291 | iterator end() { return SyntheticRoot.Uses.end(); } |
| 292 | ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 293 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 294 | ArgumentGraphNode *operator[](Argument *A) { |
| 295 | ArgumentGraphNode &Node = ArgumentMap[A]; |
| 296 | Node.Definition = A; |
| 297 | SyntheticRoot.Uses.push_back(&Node); |
| 298 | return &Node; |
| 299 | } |
| 300 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 301 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 302 | /// This tracker checks whether callees are in the SCC, and if so it does not |
| 303 | /// consider that a capture, instead adding it to the "Uses" list and |
| 304 | /// continuing with the analysis. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 305 | struct ArgumentUsesTracker : public CaptureTracker { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 306 | ArgumentUsesTracker(const SCCNodeSet &SCCNodes) |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 307 | : Captured(false), SCCNodes(SCCNodes) {} |
| 308 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 309 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 310 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 311 | bool captured(const Use *U) override { |
| 312 | CallSite CS(U->getUser()); |
| 313 | if (!CS.getInstruction()) { |
| 314 | Captured = true; |
| 315 | return true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 318 | Function *F = CS.getCalledFunction(); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 319 | if (!F || F->isDeclaration() || F->mayBeOverridden() || |
| 320 | !SCCNodes.count(F)) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 321 | Captured = true; |
| 322 | return true; |
| 323 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 324 | |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 325 | // Note: the callee and the two successor blocks *follow* the argument |
| 326 | // operands. This means there is no need to adjust UseIndex to account for |
| 327 | // these. |
| 328 | |
| 329 | unsigned UseIndex = |
| 330 | std::distance(const_cast<const Use *>(CS.arg_begin()), U); |
| 331 | |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 332 | assert(UseIndex < CS.data_operands_size() && |
| 333 | "Indirect function calls should have been filtered above!"); |
| 334 | |
| 335 | if (UseIndex >= CS.getNumArgOperands()) { |
| 336 | // Data operand, but not a argument operand -- must be a bundle operand |
| 337 | assert(CS.hasOperandBundles() && "Must be!"); |
| 338 | |
| 339 | // CaptureTracking told us that we're being captured by an operand bundle |
| 340 | // use. In this case it does not matter if the callee is within our SCC |
| 341 | // or not -- we've been captured in some unknown way, and we have to be |
| 342 | // conservative. |
| 343 | Captured = true; |
| 344 | return true; |
| 345 | } |
| 346 | |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 347 | if (UseIndex >= F->arg_size()) { |
| 348 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 349 | Captured = true; |
| 350 | return true; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 351 | } |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 352 | |
Duncan P. N. Exon Smith | 83c4b68 | 2015-11-07 00:01:16 +0000 | [diff] [blame] | 353 | Uses.push_back(&*std::next(F->arg_begin(), UseIndex)); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | |
| 357 | bool Captured; // True only if certainly captured (used outside our SCC). |
| 358 | SmallVector<Argument *, 4> Uses; // Uses within our SCC. |
| 359 | |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 360 | const SCCNodeSet &SCCNodes; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 361 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 362 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 363 | |
| 364 | namespace llvm { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 365 | template <> struct GraphTraits<ArgumentGraphNode *> { |
| 366 | typedef ArgumentGraphNode NodeType; |
| 367 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 368 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 369 | static inline NodeType *getEntryNode(NodeType *A) { return A; } |
| 370 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 371 | return N->Uses.begin(); |
| 372 | } |
| 373 | static inline ChildIteratorType child_end(NodeType *N) { |
| 374 | return N->Uses.end(); |
| 375 | } |
| 376 | }; |
| 377 | template <> |
| 378 | struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { |
| 379 | static NodeType *getEntryNode(ArgumentGraph *AG) { |
| 380 | return AG->getEntryNode(); |
| 381 | } |
| 382 | static ChildIteratorType nodes_begin(ArgumentGraph *AG) { |
| 383 | return AG->begin(); |
| 384 | } |
| 385 | static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } |
| 386 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 387 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 388 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 389 | /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 390 | static Attribute::AttrKind |
| 391 | determinePointerReadAttrs(Argument *A, |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 392 | const SmallPtrSet<Argument *, 8> &SCCNodes) { |
| 393 | |
| 394 | SmallVector<Use *, 32> Worklist; |
| 395 | SmallSet<Use *, 32> Visited; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 396 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 397 | // inalloca arguments are always clobbered by the call. |
| 398 | if (A->hasInAllocaAttr()) |
| 399 | return Attribute::None; |
| 400 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 401 | bool IsRead = false; |
| 402 | // We don't need to track IsWritten. If A is written to, return immediately. |
| 403 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 404 | for (Use &U : A->uses()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 405 | Visited.insert(&U); |
| 406 | Worklist.push_back(&U); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | while (!Worklist.empty()) { |
| 410 | Use *U = Worklist.pop_back_val(); |
| 411 | Instruction *I = cast<Instruction>(U->getUser()); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 412 | |
| 413 | switch (I->getOpcode()) { |
| 414 | case Instruction::BitCast: |
| 415 | case Instruction::GetElementPtr: |
| 416 | case Instruction::PHI: |
| 417 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 418 | case Instruction::AddrSpaceCast: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 419 | // The original value is not read/written via this if the new value isn't. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 420 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 421 | if (Visited.insert(&UU).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 422 | Worklist.push_back(&UU); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case Instruction::Call: |
| 426 | case Instruction::Invoke: { |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 427 | bool Captures = true; |
| 428 | |
| 429 | if (I->getType()->isVoidTy()) |
| 430 | Captures = false; |
| 431 | |
| 432 | auto AddUsersToWorklistIfCapturing = [&] { |
| 433 | if (Captures) |
| 434 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 435 | if (Visited.insert(&UU).second) |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 436 | Worklist.push_back(&UU); |
| 437 | }; |
| 438 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 439 | CallSite CS(I); |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 440 | if (CS.doesNotAccessMemory()) { |
| 441 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 442 | continue; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 443 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 444 | |
| 445 | Function *F = CS.getCalledFunction(); |
| 446 | if (!F) { |
| 447 | if (CS.onlyReadsMemory()) { |
| 448 | IsRead = true; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 449 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 450 | continue; |
| 451 | } |
| 452 | return Attribute::None; |
| 453 | } |
| 454 | |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 455 | // Note: the callee and the two successor blocks *follow* the argument |
| 456 | // operands. This means there is no need to adjust UseIndex to account |
| 457 | // for these. |
| 458 | |
| 459 | unsigned UseIndex = std::distance(CS.arg_begin(), U); |
| 460 | |
Sanjoy Das | ea1df7f | 2015-11-07 01:56:07 +0000 | [diff] [blame] | 461 | // U cannot be the callee operand use: since we're exploring the |
| 462 | // transitive uses of an Argument, having such a use be a callee would |
| 463 | // imply the CallSite is an indirect call or invoke; and we'd take the |
| 464 | // early exit above. |
| 465 | assert(UseIndex < CS.data_operands_size() && |
| 466 | "Data operand use expected!"); |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 467 | |
| 468 | bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands(); |
| 469 | |
| 470 | if (UseIndex >= F->arg_size() && !IsOperandBundleUse) { |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 471 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 472 | return Attribute::None; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 473 | } |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 474 | |
Tilmann Scheller | 925b193 | 2015-11-20 19:17:10 +0000 | [diff] [blame] | 475 | Captures &= !CS.doesNotCapture(UseIndex); |
| 476 | |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 477 | // Since the optimizer (by design) cannot see the data flow corresponding |
| 478 | // to a operand bundle use, these cannot participate in the optimistic SCC |
| 479 | // analysis. Instead, we model the operand bundle uses as arguments in |
| 480 | // call to a function external to the SCC. |
Sanjoy Das | 76dd243 | 2015-11-07 02:26:53 +0000 | [diff] [blame] | 481 | if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) || |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 482 | IsOperandBundleUse) { |
| 483 | |
| 484 | // The accessors used on CallSite here do the right thing for calls and |
| 485 | // invokes with operand bundles. |
| 486 | |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 487 | if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex)) |
| 488 | return Attribute::None; |
| 489 | if (!CS.doesNotAccessMemory(UseIndex)) |
| 490 | IsRead = true; |
| 491 | } |
| 492 | |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 493 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | |
| 497 | case Instruction::Load: |
| 498 | IsRead = true; |
| 499 | break; |
| 500 | |
| 501 | case Instruction::ICmp: |
| 502 | case Instruction::Ret: |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | return Attribute::None; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; |
| 511 | } |
| 512 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 513 | /// Deduce nocapture attributes for the SCC. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 514 | static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 515 | bool Changed = false; |
| 516 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 517 | ArgumentGraph AG; |
| 518 | |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 519 | AttrBuilder B; |
| 520 | B.addAttribute(Attribute::NoCapture); |
| 521 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 522 | // Check each function in turn, determining which pointer arguments are not |
| 523 | // captured. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 524 | for (Function *F : SCCNodes) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 525 | // Definitions with weak linkage may be overridden at linktime with |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 526 | // something that captures pointers, so treat them like declarations. |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 527 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 528 | continue; |
| 529 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 530 | // Functions that are readonly (or readnone) and nounwind and don't return |
| 531 | // a value can't capture arguments. Don't analyze them. |
| 532 | if (F->onlyReadsMemory() && F->doesNotThrow() && |
| 533 | F->getReturnType()->isVoidTy()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 534 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 535 | ++A) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 536 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
| 537 | A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); |
| 538 | ++NumNoCapture; |
| 539 | Changed = true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 540 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 541 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 542 | continue; |
Benjamin Kramer | 76b7bd0 | 2013-06-22 15:51:19 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 545 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 546 | ++A) { |
| 547 | if (!A->getType()->isPointerTy()) |
| 548 | continue; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 549 | bool HasNonLocalUses = false; |
| 550 | if (!A->hasNoCaptureAttr()) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 551 | ArgumentUsesTracker Tracker(SCCNodes); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 552 | PointerMayBeCaptured(&*A, &Tracker); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 553 | if (!Tracker.Captured) { |
| 554 | if (Tracker.Uses.empty()) { |
| 555 | // If it's trivially not captured, mark it nocapture now. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 556 | A->addAttr( |
| 557 | AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 558 | ++NumNoCapture; |
| 559 | Changed = true; |
| 560 | } else { |
| 561 | // If it's not trivially captured and not trivially not captured, |
| 562 | // then it must be calling into another function in our SCC. Save |
| 563 | // its particulars for Argument-SCC analysis later. |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 564 | ArgumentGraphNode *Node = AG[&*A]; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 565 | for (SmallVectorImpl<Argument *>::iterator |
| 566 | UI = Tracker.Uses.begin(), |
| 567 | UE = Tracker.Uses.end(); |
| 568 | UI != UE; ++UI) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 569 | Node->Uses.push_back(AG[*UI]); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 570 | if (*UI != A) |
| 571 | HasNonLocalUses = true; |
| 572 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | // Otherwise, it's captured. Don't bother doing SCC analysis on it. |
| 576 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 577 | if (!HasNonLocalUses && !A->onlyReadsMemory()) { |
| 578 | // Can we determine that it's readonly/readnone without doing an SCC? |
| 579 | // Note that we don't allow any calls at all here, or else our result |
| 580 | // will be dependent on the iteration order through the functions in the |
| 581 | // SCC. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 582 | SmallPtrSet<Argument *, 8> Self; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 583 | Self.insert(&*A); |
| 584 | Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 585 | if (R != Attribute::None) { |
| 586 | AttrBuilder B; |
| 587 | B.addAttribute(R); |
| 588 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
| 589 | Changed = true; |
| 590 | R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 591 | } |
| 592 | } |
| 593 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // The graph we've collected is partial because we stopped scanning for |
| 597 | // argument uses once we solved the argument trivially. These partial nodes |
| 598 | // show up as ArgumentGraphNode objects with an empty Uses list, and for |
| 599 | // these nodes the final decision about whether they capture has already been |
| 600 | // made. If the definition doesn't have a 'nocapture' attribute by now, it |
| 601 | // captures. |
| 602 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 603 | for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) { |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 604 | const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 605 | if (ArgumentSCC.size() == 1) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 606 | if (!ArgumentSCC[0]->Definition) |
| 607 | continue; // synthetic root node |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 608 | |
| 609 | // eg. "void f(int* x) { if (...) f(x); }" |
| 610 | if (ArgumentSCC[0]->Uses.size() == 1 && |
| 611 | ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 612 | Argument *A = ArgumentSCC[0]->Definition; |
| 613 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 7e82055 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 614 | ++NumNoCapture; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 615 | Changed = true; |
| 616 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 617 | continue; |
| 618 | } |
| 619 | |
| 620 | bool SCCCaptured = false; |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 621 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 622 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 623 | ArgumentGraphNode *Node = *I; |
| 624 | if (Node->Uses.empty()) { |
| 625 | if (!Node->Definition->hasNoCaptureAttr()) |
| 626 | SCCCaptured = true; |
| 627 | } |
| 628 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 629 | if (SCCCaptured) |
| 630 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 631 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 632 | SmallPtrSet<Argument *, 8> ArgumentSCCNodes; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 633 | // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for |
| 634 | // quickly looking up whether a given Argument is in this ArgumentSCC. |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 635 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 636 | ArgumentSCCNodes.insert((*I)->Definition); |
| 637 | } |
| 638 | |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 639 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 640 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 641 | ArgumentGraphNode *N = *I; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 642 | for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(), |
| 643 | UE = N->Uses.end(); |
| 644 | UI != UE; ++UI) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 645 | Argument *A = (*UI)->Definition; |
| 646 | if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) |
| 647 | continue; |
| 648 | SCCCaptured = true; |
| 649 | break; |
| 650 | } |
| 651 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 652 | if (SCCCaptured) |
| 653 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 654 | |
Nick Lewycky | f740db3 | 2012-01-05 22:21:45 +0000 | [diff] [blame] | 655 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 656 | Argument *A = ArgumentSCC[i]->Definition; |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 657 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 658 | ++NumNoCapture; |
| 659 | Changed = true; |
| 660 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 661 | |
| 662 | // We also want to compute readonly/readnone. With a small number of false |
| 663 | // negatives, we can assume that any pointer which is captured isn't going |
| 664 | // to be provably readonly or readnone, since by definition we can't |
| 665 | // analyze all uses of a captured pointer. |
| 666 | // |
| 667 | // The false negatives happen when the pointer is captured by a function |
| 668 | // that promises readonly/readnone behaviour on the pointer, then the |
| 669 | // pointer's lifetime ends before anything that writes to arbitrary memory. |
| 670 | // Also, a readonly/readnone pointer may be returned, but returning a |
| 671 | // pointer is capturing it. |
| 672 | |
| 673 | Attribute::AttrKind ReadAttr = Attribute::ReadNone; |
| 674 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 675 | Argument *A = ArgumentSCC[i]->Definition; |
| 676 | Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); |
| 677 | if (K == Attribute::ReadNone) |
| 678 | continue; |
| 679 | if (K == Attribute::ReadOnly) { |
| 680 | ReadAttr = Attribute::ReadOnly; |
| 681 | continue; |
| 682 | } |
| 683 | ReadAttr = K; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | if (ReadAttr != Attribute::None) { |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 688 | AttrBuilder B, R; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 689 | B.addAttribute(ReadAttr); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 690 | R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 691 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 692 | Argument *A = ArgumentSCC[i]->Definition; |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 693 | // Clear out existing readonly/readnone attributes |
| 694 | A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R)); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 695 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
| 696 | ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 697 | Changed = true; |
| 698 | } |
| 699 | } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | return Changed; |
| 703 | } |
| 704 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 705 | /// Tests whether a function is "malloc-like". |
| 706 | /// |
| 707 | /// A function is "malloc-like" if it returns either null or a pointer that |
| 708 | /// doesn't alias any other pointer visible to the caller. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 709 | static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 710 | SmallSetVector<Value *, 8> FlowsToReturn; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 711 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 712 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 713 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 714 | |
| 715 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 716 | Value *RetVal = FlowsToReturn[i]; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 717 | |
| 718 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 719 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 720 | return false; |
| 721 | |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | if (isa<Argument>(RetVal)) |
| 726 | return false; |
| 727 | |
| 728 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 729 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 730 | // Extend the analysis by looking upwards. |
| 731 | case Instruction::BitCast: |
| 732 | case Instruction::GetElementPtr: |
| 733 | case Instruction::AddrSpaceCast: |
| 734 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 735 | continue; |
| 736 | case Instruction::Select: { |
| 737 | SelectInst *SI = cast<SelectInst>(RVI); |
| 738 | FlowsToReturn.insert(SI->getTrueValue()); |
| 739 | FlowsToReturn.insert(SI->getFalseValue()); |
| 740 | continue; |
| 741 | } |
| 742 | case Instruction::PHI: { |
| 743 | PHINode *PN = cast<PHINode>(RVI); |
| 744 | for (Value *IncValue : PN->incoming_values()) |
| 745 | FlowsToReturn.insert(IncValue); |
| 746 | continue; |
| 747 | } |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 748 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 749 | // Check whether the pointer came from an allocation. |
| 750 | case Instruction::Alloca: |
| 751 | break; |
| 752 | case Instruction::Call: |
| 753 | case Instruction::Invoke: { |
| 754 | CallSite CS(RVI); |
| 755 | if (CS.paramHasAttr(0, Attribute::NoAlias)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 756 | break; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 757 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
| 758 | break; |
| 759 | } // fall-through |
| 760 | default: |
| 761 | return false; // Did not come from an allocation. |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 764 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | |
| 768 | return true; |
| 769 | } |
| 770 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 771 | /// Deduce noalias attributes for the SCC. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 772 | static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) { |
Nick Lewycky | 9ec96d1 | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 773 | // Check each function in turn, determining which functions return noalias |
| 774 | // pointers. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 775 | for (Function *F : SCCNodes) { |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 776 | // Already noalias. |
| 777 | if (F->doesNotAlias(0)) |
| 778 | continue; |
| 779 | |
| 780 | // Definitions with weak linkage may be overridden at linktime, so |
| 781 | // treat them like declarations. |
| 782 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 783 | return false; |
| 784 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 785 | // We annotate noalias return values, which are only applicable to |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 786 | // pointer types. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 787 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 788 | continue; |
| 789 | |
Chandler Carruth | 3824f85 | 2015-09-13 08:23:27 +0000 | [diff] [blame] | 790 | if (!isFunctionMallocLike(F, SCCNodes)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 791 | return false; |
| 792 | } |
| 793 | |
| 794 | bool MadeChange = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 795 | for (Function *F : SCCNodes) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 796 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 797 | continue; |
| 798 | |
| 799 | F->setDoesNotAlias(0); |
| 800 | ++NumNoAlias; |
| 801 | MadeChange = true; |
| 802 | } |
| 803 | |
| 804 | return MadeChange; |
| 805 | } |
| 806 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 807 | /// Tests whether this function is known to not return null. |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 808 | /// |
| 809 | /// Requires that the function returns a pointer. |
| 810 | /// |
| 811 | /// Returns true if it believes the function will not return a null, and sets |
| 812 | /// \p Speculative based on whether the returned conclusion is a speculative |
| 813 | /// conclusion due to SCC calls. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 814 | static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes, |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 815 | const TargetLibraryInfo &TLI, bool &Speculative) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 816 | assert(F->getReturnType()->isPointerTy() && |
| 817 | "nonnull only meaningful on pointer types"); |
| 818 | Speculative = false; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 819 | |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 820 | SmallSetVector<Value *, 8> FlowsToReturn; |
| 821 | for (BasicBlock &BB : *F) |
| 822 | if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) |
| 823 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 824 | |
| 825 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 826 | Value *RetVal = FlowsToReturn[i]; |
| 827 | |
| 828 | // If this value is locally known to be non-null, we're good |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 829 | if (isKnownNonNull(RetVal, &TLI)) |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 830 | continue; |
| 831 | |
| 832 | // Otherwise, we need to look upwards since we can't make any local |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 833 | // conclusions. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 834 | Instruction *RVI = dyn_cast<Instruction>(RetVal); |
| 835 | if (!RVI) |
| 836 | return false; |
| 837 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 838 | // Extend the analysis by looking upwards. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 839 | case Instruction::BitCast: |
| 840 | case Instruction::GetElementPtr: |
| 841 | case Instruction::AddrSpaceCast: |
| 842 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 843 | continue; |
| 844 | case Instruction::Select: { |
| 845 | SelectInst *SI = cast<SelectInst>(RVI); |
| 846 | FlowsToReturn.insert(SI->getTrueValue()); |
| 847 | FlowsToReturn.insert(SI->getFalseValue()); |
| 848 | continue; |
| 849 | } |
| 850 | case Instruction::PHI: { |
| 851 | PHINode *PN = cast<PHINode>(RVI); |
| 852 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 853 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
| 854 | continue; |
| 855 | } |
| 856 | case Instruction::Call: |
| 857 | case Instruction::Invoke: { |
| 858 | CallSite CS(RVI); |
| 859 | Function *Callee = CS.getCalledFunction(); |
| 860 | // A call to a node within the SCC is assumed to return null until |
| 861 | // proven otherwise |
| 862 | if (Callee && SCCNodes.count(Callee)) { |
| 863 | Speculative = true; |
| 864 | continue; |
| 865 | } |
| 866 | return false; |
| 867 | } |
| 868 | default: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 869 | return false; // Unknown source, may be null |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 870 | }; |
| 871 | llvm_unreachable("should have either continued or returned"); |
| 872 | } |
| 873 | |
| 874 | return true; |
| 875 | } |
| 876 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 877 | /// Deduce nonnull attributes for the SCC. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 878 | static bool addNonNullAttrs(const SCCNodeSet &SCCNodes, |
| 879 | const TargetLibraryInfo &TLI) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 880 | // Speculative that all functions in the SCC return only nonnull |
| 881 | // pointers. We may refute this as we analyze functions. |
| 882 | bool SCCReturnsNonNull = true; |
| 883 | |
| 884 | bool MadeChange = false; |
| 885 | |
| 886 | // Check each function in turn, determining which functions return nonnull |
| 887 | // pointers. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 888 | for (Function *F : SCCNodes) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 889 | // Already nonnull. |
| 890 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 891 | Attribute::NonNull)) |
| 892 | continue; |
| 893 | |
| 894 | // Definitions with weak linkage may be overridden at linktime, so |
| 895 | // treat them like declarations. |
| 896 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 897 | return false; |
| 898 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 899 | // We annotate nonnull return values, which are only applicable to |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 900 | // pointer types. |
| 901 | if (!F->getReturnType()->isPointerTy()) |
| 902 | continue; |
| 903 | |
| 904 | bool Speculative = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 905 | if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 906 | if (!Speculative) { |
| 907 | // Mark the function eagerly since we may discover a function |
| 908 | // which prevents us from speculating about the entire SCC |
| 909 | DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); |
| 910 | F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
| 911 | ++NumNonNullReturn; |
| 912 | MadeChange = true; |
| 913 | } |
| 914 | continue; |
| 915 | } |
| 916 | // At least one function returns something which could be null, can't |
| 917 | // speculate any more. |
| 918 | SCCReturnsNonNull = false; |
| 919 | } |
| 920 | |
| 921 | if (SCCReturnsNonNull) { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 922 | for (Function *F : SCCNodes) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 923 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 924 | Attribute::NonNull) || |
| 925 | !F->getReturnType()->isPointerTy()) |
| 926 | continue; |
| 927 | |
| 928 | DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); |
| 929 | F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
| 930 | ++NumNonNullReturn; |
| 931 | MadeChange = true; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return MadeChange; |
| 936 | } |
| 937 | |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 938 | /// Removes convergent attributes where we can prove that none of the SCC's |
| 939 | /// callees are themselves convergent. Returns true if successful at removing |
| 940 | /// the attribute. |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 941 | static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) { |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 942 | // Determines whether a function can be made non-convergent, ignoring all |
| 943 | // other functions in SCC. (A function can *actually* be made non-convergent |
| 944 | // only if all functions in its SCC can be made convergent.) |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 945 | auto CanRemoveConvergent = [&](Function *F) { |
Chandler Carruth | bbbbec0 | 2016-02-12 03:07:50 +0000 | [diff] [blame] | 946 | if (!F->isConvergent()) |
| 947 | return true; |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 948 | |
| 949 | // Can't remove convergent from declarations. |
Chandler Carruth | bbbbec0 | 2016-02-12 03:07:50 +0000 | [diff] [blame] | 950 | if (F->isDeclaration()) |
| 951 | return false; |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 952 | |
Chandler Carruth | 057df3d | 2016-02-12 09:23:53 +0000 | [diff] [blame] | 953 | for (Instruction &I : instructions(*F)) |
| 954 | if (auto CS = CallSite(&I)) { |
| 955 | // Can't remove convergent if any of F's callees -- ignoring functions |
| 956 | // in the SCC itself -- are convergent. This needs to consider both |
| 957 | // function calls and intrinsic calls. We also assume indirect calls |
| 958 | // might call a convergent function. |
| 959 | // FIXME: We should revisit this when we put convergent onto calls |
| 960 | // instead of functions so that indirect calls which should be |
| 961 | // convergent are required to be marked as such. |
| 962 | Function *Callee = CS.getCalledFunction(); |
| 963 | if (!Callee || (SCCNodes.count(Callee) == 0 && Callee->isConvergent())) |
| 964 | return false; |
| 965 | } |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 966 | |
Chandler Carruth | 057df3d | 2016-02-12 09:23:53 +0000 | [diff] [blame] | 967 | return true; |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 968 | }; |
| 969 | |
Chandler Carruth | bbbbec0 | 2016-02-12 03:07:50 +0000 | [diff] [blame] | 970 | // We can remove the convergent attr from functions in the SCC if they all |
| 971 | // can be made non-convergent (because they call only non-convergent |
| 972 | // functions, other than each other). |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 973 | if (!llvm::all_of(SCCNodes, CanRemoveConvergent)) |
Chandler Carruth | bbbbec0 | 2016-02-12 03:07:50 +0000 | [diff] [blame] | 974 | return false; |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 975 | |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 976 | // If we got here, all of the SCC's callees are non-convergent. Therefore all |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 977 | // of the SCC's functions can be marked as non-convergent. |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 978 | for (Function *F : SCCNodes) { |
| 979 | if (F->isConvergent()) |
| 980 | DEBUG(dbgs() << "Removing convergent attr from " << F->getName() << "\n"); |
| 981 | F->setNotConvergent(); |
| 982 | } |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 983 | return true; |
| 984 | } |
| 985 | |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 986 | static bool setDoesNotRecurse(Function &F) { |
| 987 | if (F.doesNotRecurse()) |
| 988 | return false; |
| 989 | F.setDoesNotRecurse(); |
| 990 | ++NumNoRecurse; |
| 991 | return true; |
| 992 | } |
| 993 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 994 | static bool addNoRecurseAttrs(const CallGraphSCC &SCC) { |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 995 | // Try and identify functions that do not recurse. |
| 996 | |
| 997 | // If the SCC contains multiple nodes we know for sure there is recursion. |
| 998 | if (!SCC.isSingular()) |
| 999 | return false; |
| 1000 | |
| 1001 | const CallGraphNode *CGN = *SCC.begin(); |
| 1002 | Function *F = CGN->getFunction(); |
| 1003 | if (!F || F->isDeclaration() || F->doesNotRecurse()) |
| 1004 | return false; |
| 1005 | |
| 1006 | // If all of the calls in F are identifiable and are to norecurse functions, F |
| 1007 | // is norecurse. This check also detects self-recursion as F is not currently |
| 1008 | // marked norecurse, so any called from F to F will not be marked norecurse. |
| 1009 | if (std::all_of(CGN->begin(), CGN->end(), |
| 1010 | [](const CallGraphNode::CallRecord &CR) { |
| 1011 | Function *F = CR.second->getFunction(); |
| 1012 | return F && F->doesNotRecurse(); |
| 1013 | })) |
| 1014 | // Function calls a potentially recursive function. |
| 1015 | return setDoesNotRecurse(*F); |
| 1016 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1017 | // Nothing else we can deduce usefully during the postorder traversal. |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
| 1020 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1021 | bool PostOrderFunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1022 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chandler Carruth | cada2d8 | 2015-10-31 00:28:37 +0000 | [diff] [blame] | 1023 | bool Changed = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1024 | |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 1025 | // We compute dedicated AA results for each function in the SCC as needed. We |
| 1026 | // use a lambda referencing external objects so that they live long enough to |
| 1027 | // be queried, but we re-use them each time. |
| 1028 | Optional<BasicAAResult> BAR; |
| 1029 | Optional<AAResults> AAR; |
| 1030 | auto AARGetter = [&](Function &F) -> AAResults & { |
| 1031 | BAR.emplace(createLegacyPMBasicAAResult(*this, F)); |
| 1032 | AAR.emplace(createLegacyPMAAResults(*this, F, *BAR)); |
| 1033 | return *AAR; |
| 1034 | }; |
| 1035 | |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1036 | // Fill SCCNodes with the elements of the SCC. Used for quickly looking up |
| 1037 | // whether a given CallGraphNode is in this SCC. Also track whether there are |
| 1038 | // any external or opt-none nodes that will prevent us from optimizing any |
| 1039 | // part of the SCC. |
| 1040 | SCCNodeSet SCCNodes; |
| 1041 | bool ExternalNode = false; |
| 1042 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 1043 | Function *F = (*I)->getFunction(); |
| 1044 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) { |
| 1045 | // External node or function we're trying not to optimize - we both avoid |
| 1046 | // transform them and avoid leveraging information they provide. |
| 1047 | ExternalNode = true; |
| 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | SCCNodes.insert(F); |
| 1052 | } |
| 1053 | |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 1054 | Changed |= addReadAttrs(SCCNodes, AARGetter); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1055 | Changed |= addArgumentAttrs(SCCNodes); |
| 1056 | |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 1057 | // If we have no external nodes participating in the SCC, we can deduce some |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1058 | // more precise attributes as well. |
| 1059 | if (!ExternalNode) { |
| 1060 | Changed |= addNoAliasAttrs(SCCNodes); |
| 1061 | Changed |= addNonNullAttrs(SCCNodes, *TLI); |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 1062 | Changed |= removeConvergentAttrs(SCCNodes); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1063 | } |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1064 | |
| 1065 | Changed |= addNoRecurseAttrs(SCC); |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1066 | return Changed; |
| 1067 | } |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1068 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1069 | namespace { |
| 1070 | /// A pass to do RPO deduction and propagation of function attributes. |
| 1071 | /// |
| 1072 | /// This pass provides a general RPO or "top down" propagation of |
| 1073 | /// function attributes. For a few (rare) cases, we can deduce significantly |
| 1074 | /// more about function attributes by working in RPO, so this pass |
| 1075 | /// provides the compliment to the post-order pass above where the majority of |
| 1076 | /// deduction is performed. |
| 1077 | // FIXME: Currently there is no RPO CGSCC pass structure to slide into and so |
| 1078 | // this is a boring module pass, but eventually it should be an RPO CGSCC pass |
| 1079 | // when such infrastructure is available. |
| 1080 | struct ReversePostOrderFunctionAttrs : public ModulePass { |
| 1081 | static char ID; // Pass identification, replacement for typeid |
| 1082 | ReversePostOrderFunctionAttrs() : ModulePass(ID) { |
| 1083 | initializeReversePostOrderFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
| 1084 | } |
| 1085 | |
| 1086 | bool runOnModule(Module &M) override; |
| 1087 | |
| 1088 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1089 | AU.setPreservesCFG(); |
| 1090 | AU.addRequired<CallGraphWrapperPass>(); |
| 1091 | } |
| 1092 | }; |
| 1093 | } |
| 1094 | |
| 1095 | char ReversePostOrderFunctionAttrs::ID = 0; |
| 1096 | INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrs, "rpo-functionattrs", |
| 1097 | "Deduce function attributes in RPO", false, false) |
| 1098 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
| 1099 | INITIALIZE_PASS_END(ReversePostOrderFunctionAttrs, "rpo-functionattrs", |
| 1100 | "Deduce function attributes in RPO", false, false) |
| 1101 | |
| 1102 | Pass *llvm::createReversePostOrderFunctionAttrsPass() { |
| 1103 | return new ReversePostOrderFunctionAttrs(); |
| 1104 | } |
| 1105 | |
| 1106 | static bool addNoRecurseAttrsTopDown(Function &F) { |
| 1107 | // We check the preconditions for the function prior to calling this to avoid |
| 1108 | // the cost of building up a reversible post-order list. We assert them here |
| 1109 | // to make sure none of the invariants this relies on were violated. |
| 1110 | assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!"); |
| 1111 | assert(!F.doesNotRecurse() && |
| 1112 | "This function has already been deduced as norecurs!"); |
| 1113 | assert(F.hasInternalLinkage() && |
| 1114 | "Can only do top-down deduction for internal linkage functions!"); |
| 1115 | |
| 1116 | // If F is internal and all of its uses are calls from a non-recursive |
| 1117 | // functions, then none of its calls could in fact recurse without going |
| 1118 | // through a function marked norecurse, and so we can mark this function too |
| 1119 | // as norecurse. Note that the uses must actually be calls -- otherwise |
| 1120 | // a pointer to this function could be returned from a norecurse function but |
| 1121 | // this function could be recursively (indirectly) called. Note that this |
| 1122 | // also detects if F is directly recursive as F is not yet marked as |
| 1123 | // a norecurse function. |
| 1124 | for (auto *U : F.users()) { |
| 1125 | auto *I = dyn_cast<Instruction>(U); |
| 1126 | if (!I) |
| 1127 | return false; |
| 1128 | CallSite CS(I); |
| 1129 | if (!CS || !CS.getParent()->getParent()->doesNotRecurse()) |
| 1130 | return false; |
| 1131 | } |
| 1132 | return setDoesNotRecurse(F); |
| 1133 | } |
| 1134 | |
| 1135 | bool ReversePostOrderFunctionAttrs::runOnModule(Module &M) { |
| 1136 | // We only have a post-order SCC traversal (because SCCs are inherently |
| 1137 | // discovered in post-order), so we accumulate them in a vector and then walk |
| 1138 | // it in reverse. This is simpler than using the RPO iterator infrastructure |
| 1139 | // because we need to combine SCC detection and the PO walk of the call |
| 1140 | // graph. We can also cheat egregiously because we're primarily interested in |
| 1141 | // synthesizing norecurse and so we can only save the singular SCCs as SCCs |
| 1142 | // with multiple functions in them will clearly be recursive. |
| 1143 | auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
| 1144 | SmallVector<Function *, 16> Worklist; |
| 1145 | for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { |
| 1146 | if (I->size() != 1) |
| 1147 | continue; |
| 1148 | |
| 1149 | Function *F = I->front()->getFunction(); |
| 1150 | if (F && !F->isDeclaration() && !F->doesNotRecurse() && |
| 1151 | F->hasInternalLinkage()) |
| 1152 | Worklist.push_back(F); |
| 1153 | } |
| 1154 | |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1155 | bool Changed = false; |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1156 | for (auto *F : reverse(Worklist)) |
| 1157 | Changed |= addNoRecurseAttrsTopDown(*F); |
| 1158 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 1159 | return Changed; |
| 1160 | } |