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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a simple interprocedural pass which walks the |
| 11 | // call-graph, looking for functions which do not access or only read |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 12 | // non-local memory, and marking them readnone/readonly. It does the |
| 13 | // same with function arguments independently, marking them readonly/ |
| 14 | // readnone/nocapture. Finally, well-known library call declarations |
| 15 | // are marked with all attributes that are consistent with the |
| 16 | // function's standard definition. This pass is implemented as a |
| 17 | // bottom-up traversal of the call-graph. |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/IPO.h" |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SCCIterator.h" |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SetVector.h" |
Duncan Sands | b193a37 | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AssumptionCache.h" |
| 28 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 35 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IntrinsicInst.h" |
| 37 | #include "llvm/IR/LLVMContext.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Hans Wennborg | 043bf5b | 2015-08-31 21:19:18 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "functionattrs" |
| 44 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 45 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 46 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 47 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 48 | STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); |
| 49 | STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 50 | STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 51 | STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 52 | STATISTIC(NumAnnotated, "Number of attributes added to library functions"); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 55 | struct FunctionAttrs : public CallGraphSCCPass { |
| 56 | static char ID; // Pass identification, replacement for typeid |
| 57 | FunctionAttrs() : CallGraphSCCPass(ID) { |
| 58 | initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
| 59 | } |
| 60 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 61 | bool runOnSCC(CallGraphSCC &SCC) override; |
| 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>(); |
| 67 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 68 | } |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 69 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 70 | private: |
| 71 | TargetLibraryInfo *TLI; |
Chandler Carruth | d024520 | 2015-09-13 07:50:43 +0000 | [diff] [blame] | 72 | |
| 73 | bool AddReadAttrs(const CallGraphSCC &SCC); |
| 74 | bool AddArgumentAttrs(const CallGraphSCC &SCC); |
Chandler Carruth | d024520 | 2015-09-13 07:50:43 +0000 | [diff] [blame] | 75 | bool AddNoAliasAttrs(const CallGraphSCC &SCC); |
Chandler Carruth | d024520 | 2015-09-13 07:50:43 +0000 | [diff] [blame] | 76 | bool AddNonNullAttrs(const CallGraphSCC &SCC); |
Chandler Carruth | d024520 | 2015-09-13 07:50:43 +0000 | [diff] [blame] | 77 | bool annotateLibraryCalls(const CallGraphSCC &SCC); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 78 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 79 | } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 80 | |
| 81 | char FunctionAttrs::ID = 0; |
Owen Anderson | 071cee0 | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 82 | INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 83 | "Deduce function attributes", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 84 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 85 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 86 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Owen Anderson | 071cee0 | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 87 | INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 88 | "Deduce function attributes", false, false) |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 89 | |
| 90 | Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
| 91 | |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 92 | namespace { |
| 93 | /// The three kinds of memory access relevant to 'readonly' and |
| 94 | /// 'readnone' attributes. |
| 95 | enum MemoryAccessKind { |
| 96 | MAK_ReadNone = 0, |
| 97 | MAK_ReadOnly = 1, |
| 98 | MAK_MayWrite = 2 |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | static MemoryAccessKind |
| 103 | checkFunctionMemoryAccess(Function &F, AAResults &AAR, |
| 104 | const SmallPtrSetImpl<Function *> &SCCNodes) { |
| 105 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); |
| 106 | if (MRB == FMRB_DoesNotAccessMemory) |
| 107 | // Already perfect! |
| 108 | return MAK_ReadNone; |
| 109 | |
| 110 | // Definitions with weak linkage may be overridden at linktime with |
| 111 | // something that writes memory, so treat them like declarations. |
| 112 | if (F.isDeclaration() || F.mayBeOverridden()) { |
| 113 | if (AliasAnalysis::onlyReadsMemory(MRB)) |
| 114 | return MAK_ReadOnly; |
| 115 | |
| 116 | // Conservatively assume it writes to memory. |
| 117 | return MAK_MayWrite; |
| 118 | } |
| 119 | |
| 120 | // Scan the function body for instructions that may read or write memory. |
| 121 | bool ReadsMemory = false; |
| 122 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 123 | Instruction *I = &*II; |
| 124 | |
| 125 | // Some instructions can be ignored even if they read or write memory. |
| 126 | // Detect these now, skipping to the next instruction if one is found. |
| 127 | CallSite CS(cast<Value>(I)); |
| 128 | if (CS) { |
| 129 | // Ignore calls to functions in the same SCC. |
| 130 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
| 131 | continue; |
| 132 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS); |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 133 | |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame^] | 134 | // If the call doesn't access memory, we're done. |
| 135 | if (!(MRB & MRI_ModRef)) |
| 136 | continue; |
| 137 | |
| 138 | if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 139 | // The call could access any memory. If that includes writes, give up. |
| 140 | if (MRB & MRI_Mod) |
| 141 | return MAK_MayWrite; |
| 142 | // If it reads, note it. |
| 143 | if (MRB & MRI_Ref) |
| 144 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 145 | continue; |
| 146 | } |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame^] | 147 | |
| 148 | // Check whether all pointer arguments point to local memory, and |
| 149 | // ignore calls that only access local memory. |
| 150 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 151 | CI != CE; ++CI) { |
| 152 | Value *Arg = *CI; |
| 153 | if (!Arg->getType()->isPointerTy()) |
| 154 | continue; |
| 155 | |
| 156 | AAMDNodes AAInfo; |
| 157 | I->getAAMetadata(AAInfo); |
| 158 | MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); |
| 159 | |
| 160 | // Skip accesses to local or constant memory as they don't impact the |
| 161 | // externally visible mod/ref behavior. |
| 162 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 163 | continue; |
| 164 | |
| 165 | if (MRB & MRI_Mod) |
| 166 | // Writes non-local memory. Give up. |
| 167 | return MAK_MayWrite; |
| 168 | if (MRB & MRI_Ref) |
| 169 | // Ok, it reads non-local memory. |
| 170 | ReadsMemory = true; |
| 171 | } |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 172 | continue; |
| 173 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 174 | // Ignore non-volatile loads from local memory. (Atomic is okay here.) |
| 175 | if (!LI->isVolatile()) { |
| 176 | MemoryLocation Loc = MemoryLocation::get(LI); |
| 177 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 178 | continue; |
| 179 | } |
| 180 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 181 | // Ignore non-volatile stores to local memory. (Atomic is okay here.) |
| 182 | if (!SI->isVolatile()) { |
| 183 | MemoryLocation Loc = MemoryLocation::get(SI); |
| 184 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 185 | continue; |
| 186 | } |
| 187 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { |
| 188 | // Ignore vaargs on local memory. |
| 189 | MemoryLocation Loc = MemoryLocation::get(VI); |
| 190 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | // Any remaining instructions need to be taken seriously! Check if they |
| 195 | // read or write memory. |
| 196 | if (I->mayWriteToMemory()) |
| 197 | // Writes memory. Just give up. |
| 198 | return MAK_MayWrite; |
| 199 | |
| 200 | // If this instruction may read memory, remember that. |
| 201 | ReadsMemory |= I->mayReadFromMemory(); |
| 202 | } |
| 203 | |
| 204 | return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; |
| 205 | } |
| 206 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 207 | /// Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 208 | bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 209 | SmallPtrSet<Function *, 8> SCCNodes; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 210 | |
| 211 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 212 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 213 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 214 | SCCNodes.insert((*I)->getFunction()); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 215 | |
| 216 | // Check if any of the functions in the SCC read or write memory. If they |
| 217 | // write memory then they can't be marked readnone or readonly. |
| 218 | bool ReadsMemory = false; |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 219 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 220 | Function *F = (*I)->getFunction(); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 221 | |
Chandler Carruth | 0fb9981 | 2014-08-13 10:49:33 +0000 | [diff] [blame] | 222 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) |
| 223 | // External node or node we don't want to optimize - assume it may write |
| 224 | // memory and give up. |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 225 | return false; |
| 226 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 227 | // We need to manually construct BasicAA directly in order to disable its |
| 228 | // use of other function analyses. |
| 229 | BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F)); |
| 230 | |
| 231 | // Construct our own AA results for this function. We do this manually to |
| 232 | // work around the limitations of the legacy pass manager. |
| 233 | AAResults AAR(createLegacyPMAAResults(*this, *F, BAR)); |
| 234 | |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 235 | switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) { |
| 236 | case MAK_MayWrite: |
| 237 | return false; |
| 238 | case MAK_ReadOnly: |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 239 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 240 | break; |
| 241 | case MAK_ReadNone: |
| 242 | // Nothing to do! |
| 243 | break; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 248 | // Give them the appropriate attribute. |
| 249 | bool MadeChange = false; |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 250 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 251 | Function *F = (*I)->getFunction(); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 252 | |
| 253 | if (F->doesNotAccessMemory()) |
| 254 | // Already perfect! |
| 255 | continue; |
| 256 | |
| 257 | if (F->onlyReadsMemory() && ReadsMemory) |
| 258 | // No change. |
| 259 | continue; |
| 260 | |
| 261 | MadeChange = true; |
| 262 | |
| 263 | // Clear out any existing attributes. |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 264 | AttrBuilder B; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 265 | B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
| 266 | F->removeAttributes( |
| 267 | AttributeSet::FunctionIndex, |
| 268 | AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B)); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 269 | |
| 270 | // Add in the new attribute. |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 271 | F->addAttribute(AttributeSet::FunctionIndex, |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 272 | ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 273 | |
| 274 | if (ReadsMemory) |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 275 | ++NumReadOnly; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 276 | else |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 277 | ++NumReadNone; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return MadeChange; |
| 281 | } |
| 282 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 283 | namespace { |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 284 | /// For a given pointer Argument, this retains a list of Arguments of functions |
| 285 | /// in the same SCC that the pointer data flows into. We use this to build an |
| 286 | /// SCC of the arguments. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 287 | struct ArgumentGraphNode { |
| 288 | Argument *Definition; |
| 289 | SmallVector<ArgumentGraphNode *, 4> Uses; |
| 290 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 291 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 292 | class ArgumentGraph { |
| 293 | // We store pointers to ArgumentGraphNode objects, so it's important that |
| 294 | // that they not move around upon insert. |
| 295 | typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 296 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 297 | ArgumentMapTy ArgumentMap; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 298 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 299 | // There is no root node for the argument graph, in fact: |
| 300 | // void f(int *x, int *y) { if (...) f(x, y); } |
| 301 | // is an example where the graph is disconnected. The SCCIterator requires a |
| 302 | // single entry point, so we maintain a fake ("synthetic") root node that |
| 303 | // uses every node. Because the graph is directed and nothing points into |
| 304 | // the root, it will not participate in any SCCs (except for its own). |
| 305 | ArgumentGraphNode SyntheticRoot; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 306 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 307 | public: |
| 308 | ArgumentGraph() { SyntheticRoot.Definition = nullptr; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 309 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 310 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 311 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 312 | iterator begin() { return SyntheticRoot.Uses.begin(); } |
| 313 | iterator end() { return SyntheticRoot.Uses.end(); } |
| 314 | ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 315 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 316 | ArgumentGraphNode *operator[](Argument *A) { |
| 317 | ArgumentGraphNode &Node = ArgumentMap[A]; |
| 318 | Node.Definition = A; |
| 319 | SyntheticRoot.Uses.push_back(&Node); |
| 320 | return &Node; |
| 321 | } |
| 322 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 323 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 324 | /// This tracker checks whether callees are in the SCC, and if so it does not |
| 325 | /// consider that a capture, instead adding it to the "Uses" list and |
| 326 | /// continuing with the analysis. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 327 | struct ArgumentUsesTracker : public CaptureTracker { |
| 328 | ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes) |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 329 | : Captured(false), SCCNodes(SCCNodes) {} |
| 330 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 331 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 332 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 333 | bool captured(const Use *U) override { |
| 334 | CallSite CS(U->getUser()); |
| 335 | if (!CS.getInstruction()) { |
| 336 | Captured = true; |
| 337 | return true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 340 | Function *F = CS.getCalledFunction(); |
| 341 | if (!F || !SCCNodes.count(F)) { |
| 342 | Captured = true; |
| 343 | return true; |
| 344 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 345 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 346 | bool Found = false; |
| 347 | Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 348 | for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); |
| 349 | PI != PE; ++PI, ++AI) { |
| 350 | if (AI == AE) { |
| 351 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 352 | Captured = true; |
| 353 | return true; |
| 354 | } |
| 355 | if (PI == U) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 356 | Uses.push_back(&*AI); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 357 | Found = true; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | assert(Found && "Capturing call-site captured nothing?"); |
| 362 | (void)Found; |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | bool Captured; // True only if certainly captured (used outside our SCC). |
| 367 | SmallVector<Argument *, 4> Uses; // Uses within our SCC. |
| 368 | |
| 369 | const SmallPtrSet<Function *, 8> &SCCNodes; |
| 370 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 371 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 372 | |
| 373 | namespace llvm { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 374 | template <> struct GraphTraits<ArgumentGraphNode *> { |
| 375 | typedef ArgumentGraphNode NodeType; |
| 376 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 377 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 378 | static inline NodeType *getEntryNode(NodeType *A) { return A; } |
| 379 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 380 | return N->Uses.begin(); |
| 381 | } |
| 382 | static inline ChildIteratorType child_end(NodeType *N) { |
| 383 | return N->Uses.end(); |
| 384 | } |
| 385 | }; |
| 386 | template <> |
| 387 | struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { |
| 388 | static NodeType *getEntryNode(ArgumentGraph *AG) { |
| 389 | return AG->getEntryNode(); |
| 390 | } |
| 391 | static ChildIteratorType nodes_begin(ArgumentGraph *AG) { |
| 392 | return AG->begin(); |
| 393 | } |
| 394 | static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } |
| 395 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 396 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 397 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 398 | /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 399 | static Attribute::AttrKind |
| 400 | determinePointerReadAttrs(Argument *A, |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 401 | const SmallPtrSet<Argument *, 8> &SCCNodes) { |
| 402 | |
| 403 | SmallVector<Use *, 32> Worklist; |
| 404 | SmallSet<Use *, 32> Visited; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 405 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 406 | // inalloca arguments are always clobbered by the call. |
| 407 | if (A->hasInAllocaAttr()) |
| 408 | return Attribute::None; |
| 409 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 410 | bool IsRead = false; |
| 411 | // We don't need to track IsWritten. If A is written to, return immediately. |
| 412 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 413 | for (Use &U : A->uses()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 414 | Visited.insert(&U); |
| 415 | Worklist.push_back(&U); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | while (!Worklist.empty()) { |
| 419 | Use *U = Worklist.pop_back_val(); |
| 420 | Instruction *I = cast<Instruction>(U->getUser()); |
| 421 | Value *V = U->get(); |
| 422 | |
| 423 | switch (I->getOpcode()) { |
| 424 | case Instruction::BitCast: |
| 425 | case Instruction::GetElementPtr: |
| 426 | case Instruction::PHI: |
| 427 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 428 | case Instruction::AddrSpaceCast: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 429 | // 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] | 430 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 431 | if (Visited.insert(&UU).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 432 | Worklist.push_back(&UU); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 433 | break; |
| 434 | |
| 435 | case Instruction::Call: |
| 436 | case Instruction::Invoke: { |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 437 | bool Captures = true; |
| 438 | |
| 439 | if (I->getType()->isVoidTy()) |
| 440 | Captures = false; |
| 441 | |
| 442 | auto AddUsersToWorklistIfCapturing = [&] { |
| 443 | if (Captures) |
| 444 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 445 | if (Visited.insert(&UU).second) |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 446 | Worklist.push_back(&UU); |
| 447 | }; |
| 448 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 449 | CallSite CS(I); |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 450 | if (CS.doesNotAccessMemory()) { |
| 451 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 452 | continue; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 453 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 454 | |
| 455 | Function *F = CS.getCalledFunction(); |
| 456 | if (!F) { |
| 457 | if (CS.onlyReadsMemory()) { |
| 458 | IsRead = true; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 459 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 460 | continue; |
| 461 | } |
| 462 | return Attribute::None; |
| 463 | } |
| 464 | |
| 465 | Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 466 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 467 | for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) { |
| 468 | if (A->get() == V) { |
| 469 | if (AI == AE) { |
| 470 | assert(F->isVarArg() && |
| 471 | "More params than args in non-varargs call."); |
| 472 | return Attribute::None; |
| 473 | } |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 474 | Captures &= !CS.doesNotCapture(A - B); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 475 | if (SCCNodes.count(&*AI)) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 476 | continue; |
| 477 | if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B)) |
| 478 | return Attribute::None; |
| 479 | if (!CS.doesNotAccessMemory(A - B)) |
| 480 | IsRead = true; |
| 481 | } |
| 482 | } |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 483 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 484 | break; |
| 485 | } |
| 486 | |
| 487 | case Instruction::Load: |
| 488 | IsRead = true; |
| 489 | break; |
| 490 | |
| 491 | case Instruction::ICmp: |
| 492 | case Instruction::Ret: |
| 493 | break; |
| 494 | |
| 495 | default: |
| 496 | return Attribute::None; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; |
| 501 | } |
| 502 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 503 | /// Deduce nocapture attributes for the SCC. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 504 | bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 505 | bool Changed = false; |
| 506 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 507 | SmallPtrSet<Function *, 8> SCCNodes; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 508 | |
| 509 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 510 | // looking up whether a given CallGraphNode is in this SCC. |
| 511 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 512 | Function *F = (*I)->getFunction(); |
Chandler Carruth | 0fb9981 | 2014-08-13 10:49:33 +0000 | [diff] [blame] | 513 | if (F && !F->isDeclaration() && !F->mayBeOverridden() && |
| 514 | !F->hasFnAttribute(Attribute::OptimizeNone)) |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 515 | SCCNodes.insert(F); |
| 516 | } |
| 517 | |
| 518 | ArgumentGraph AG; |
| 519 | |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 520 | AttrBuilder B; |
| 521 | B.addAttribute(Attribute::NoCapture); |
| 522 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 523 | // Check each function in turn, determining which pointer arguments are not |
| 524 | // captured. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 525 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 526 | Function *F = (*I)->getFunction(); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 527 | |
Chandler Carruth | 0fb9981 | 2014-08-13 10:49:33 +0000 | [diff] [blame] | 528 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) |
| 529 | // External node or function we're trying not to optimize - only a problem |
| 530 | // for arguments that we pass to it. |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 531 | continue; |
| 532 | |
| 533 | // Definitions with weak linkage may be overridden at linktime with |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 534 | // something that captures pointers, so treat them like declarations. |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 535 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 536 | continue; |
| 537 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 538 | // Functions that are readonly (or readnone) and nounwind and don't return |
| 539 | // a value can't capture arguments. Don't analyze them. |
| 540 | if (F->onlyReadsMemory() && F->doesNotThrow() && |
| 541 | F->getReturnType()->isVoidTy()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 542 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 543 | ++A) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 544 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
| 545 | A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); |
| 546 | ++NumNoCapture; |
| 547 | Changed = true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 548 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 549 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 550 | continue; |
Benjamin Kramer | 76b7bd0 | 2013-06-22 15:51:19 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 553 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 554 | ++A) { |
| 555 | if (!A->getType()->isPointerTy()) |
| 556 | continue; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 557 | bool HasNonLocalUses = false; |
| 558 | if (!A->hasNoCaptureAttr()) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 559 | ArgumentUsesTracker Tracker(SCCNodes); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 560 | PointerMayBeCaptured(&*A, &Tracker); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 561 | if (!Tracker.Captured) { |
| 562 | if (Tracker.Uses.empty()) { |
| 563 | // If it's trivially not captured, mark it nocapture now. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 564 | A->addAttr( |
| 565 | AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 566 | ++NumNoCapture; |
| 567 | Changed = true; |
| 568 | } else { |
| 569 | // If it's not trivially captured and not trivially not captured, |
| 570 | // then it must be calling into another function in our SCC. Save |
| 571 | // its particulars for Argument-SCC analysis later. |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 572 | ArgumentGraphNode *Node = AG[&*A]; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 573 | for (SmallVectorImpl<Argument *>::iterator |
| 574 | UI = Tracker.Uses.begin(), |
| 575 | UE = Tracker.Uses.end(); |
| 576 | UI != UE; ++UI) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 577 | Node->Uses.push_back(AG[*UI]); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 578 | if (*UI != A) |
| 579 | HasNonLocalUses = true; |
| 580 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | // Otherwise, it's captured. Don't bother doing SCC analysis on it. |
| 584 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 585 | if (!HasNonLocalUses && !A->onlyReadsMemory()) { |
| 586 | // Can we determine that it's readonly/readnone without doing an SCC? |
| 587 | // Note that we don't allow any calls at all here, or else our result |
| 588 | // will be dependent on the iteration order through the functions in the |
| 589 | // SCC. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 590 | SmallPtrSet<Argument *, 8> Self; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 591 | Self.insert(&*A); |
| 592 | Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 593 | if (R != Attribute::None) { |
| 594 | AttrBuilder B; |
| 595 | B.addAttribute(R); |
| 596 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
| 597 | Changed = true; |
| 598 | R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 599 | } |
| 600 | } |
| 601 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | // The graph we've collected is partial because we stopped scanning for |
| 605 | // argument uses once we solved the argument trivially. These partial nodes |
| 606 | // show up as ArgumentGraphNode objects with an empty Uses list, and for |
| 607 | // these nodes the final decision about whether they capture has already been |
| 608 | // made. If the definition doesn't have a 'nocapture' attribute by now, it |
| 609 | // captures. |
| 610 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 611 | 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] | 612 | const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 613 | if (ArgumentSCC.size() == 1) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 614 | if (!ArgumentSCC[0]->Definition) |
| 615 | continue; // synthetic root node |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 616 | |
| 617 | // eg. "void f(int* x) { if (...) f(x); }" |
| 618 | if (ArgumentSCC[0]->Uses.size() == 1 && |
| 619 | ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 620 | Argument *A = ArgumentSCC[0]->Definition; |
| 621 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 7e82055 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 622 | ++NumNoCapture; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 623 | Changed = true; |
| 624 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 625 | continue; |
| 626 | } |
| 627 | |
| 628 | bool SCCCaptured = false; |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 629 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 630 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 631 | ArgumentGraphNode *Node = *I; |
| 632 | if (Node->Uses.empty()) { |
| 633 | if (!Node->Definition->hasNoCaptureAttr()) |
| 634 | SCCCaptured = true; |
| 635 | } |
| 636 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 637 | if (SCCCaptured) |
| 638 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 639 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 640 | SmallPtrSet<Argument *, 8> ArgumentSCCNodes; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 641 | // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for |
| 642 | // 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] | 643 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 644 | ArgumentSCCNodes.insert((*I)->Definition); |
| 645 | } |
| 646 | |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 647 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 648 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 649 | ArgumentGraphNode *N = *I; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 650 | for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(), |
| 651 | UE = N->Uses.end(); |
| 652 | UI != UE; ++UI) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 653 | Argument *A = (*UI)->Definition; |
| 654 | if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) |
| 655 | continue; |
| 656 | SCCCaptured = true; |
| 657 | break; |
| 658 | } |
| 659 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 660 | if (SCCCaptured) |
| 661 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 662 | |
Nick Lewycky | f740db3 | 2012-01-05 22:21:45 +0000 | [diff] [blame] | 663 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 664 | Argument *A = ArgumentSCC[i]->Definition; |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 665 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 666 | ++NumNoCapture; |
| 667 | Changed = true; |
| 668 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 669 | |
| 670 | // We also want to compute readonly/readnone. With a small number of false |
| 671 | // negatives, we can assume that any pointer which is captured isn't going |
| 672 | // to be provably readonly or readnone, since by definition we can't |
| 673 | // analyze all uses of a captured pointer. |
| 674 | // |
| 675 | // The false negatives happen when the pointer is captured by a function |
| 676 | // that promises readonly/readnone behaviour on the pointer, then the |
| 677 | // pointer's lifetime ends before anything that writes to arbitrary memory. |
| 678 | // Also, a readonly/readnone pointer may be returned, but returning a |
| 679 | // pointer is capturing it. |
| 680 | |
| 681 | Attribute::AttrKind ReadAttr = Attribute::ReadNone; |
| 682 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 683 | Argument *A = ArgumentSCC[i]->Definition; |
| 684 | Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); |
| 685 | if (K == Attribute::ReadNone) |
| 686 | continue; |
| 687 | if (K == Attribute::ReadOnly) { |
| 688 | ReadAttr = Attribute::ReadOnly; |
| 689 | continue; |
| 690 | } |
| 691 | ReadAttr = K; |
| 692 | break; |
| 693 | } |
| 694 | |
| 695 | if (ReadAttr != Attribute::None) { |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 696 | AttrBuilder B, R; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 697 | B.addAttribute(ReadAttr); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 698 | R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 699 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 700 | Argument *A = ArgumentSCC[i]->Definition; |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 701 | // Clear out existing readonly/readnone attributes |
| 702 | A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R)); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 703 | A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
| 704 | ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 705 | Changed = true; |
| 706 | } |
| 707 | } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return Changed; |
| 711 | } |
| 712 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 713 | /// Tests whether a function is "malloc-like". |
| 714 | /// |
| 715 | /// A function is "malloc-like" if it returns either null or a pointer that |
| 716 | /// doesn't alias any other pointer visible to the caller. |
Chandler Carruth | 3824f85 | 2015-09-13 08:23:27 +0000 | [diff] [blame] | 717 | static bool isFunctionMallocLike(Function *F, |
| 718 | SmallPtrSet<Function *, 8> &SCCNodes) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 719 | SmallSetVector<Value *, 8> FlowsToReturn; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 720 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 721 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 722 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 723 | |
| 724 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 725 | Value *RetVal = FlowsToReturn[i]; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 726 | |
| 727 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 728 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 729 | return false; |
| 730 | |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | if (isa<Argument>(RetVal)) |
| 735 | return false; |
| 736 | |
| 737 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 738 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 739 | // Extend the analysis by looking upwards. |
| 740 | case Instruction::BitCast: |
| 741 | case Instruction::GetElementPtr: |
| 742 | case Instruction::AddrSpaceCast: |
| 743 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 744 | continue; |
| 745 | case Instruction::Select: { |
| 746 | SelectInst *SI = cast<SelectInst>(RVI); |
| 747 | FlowsToReturn.insert(SI->getTrueValue()); |
| 748 | FlowsToReturn.insert(SI->getFalseValue()); |
| 749 | continue; |
| 750 | } |
| 751 | case Instruction::PHI: { |
| 752 | PHINode *PN = cast<PHINode>(RVI); |
| 753 | for (Value *IncValue : PN->incoming_values()) |
| 754 | FlowsToReturn.insert(IncValue); |
| 755 | continue; |
| 756 | } |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 757 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 758 | // Check whether the pointer came from an allocation. |
| 759 | case Instruction::Alloca: |
| 760 | break; |
| 761 | case Instruction::Call: |
| 762 | case Instruction::Invoke: { |
| 763 | CallSite CS(RVI); |
| 764 | if (CS.paramHasAttr(0, Attribute::NoAlias)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 765 | break; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 766 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
| 767 | break; |
| 768 | } // fall-through |
| 769 | default: |
| 770 | return false; // Did not come from an allocation. |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 773 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 774 | return false; |
| 775 | } |
| 776 | |
| 777 | return true; |
| 778 | } |
| 779 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 780 | /// Deduce noalias attributes for the SCC. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 781 | bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 782 | SmallPtrSet<Function *, 8> SCCNodes; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 783 | |
| 784 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 785 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 786 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 787 | SCCNodes.insert((*I)->getFunction()); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 788 | |
Nick Lewycky | 9ec96d1 | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 789 | // Check each function in turn, determining which functions return noalias |
| 790 | // pointers. |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 791 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 792 | Function *F = (*I)->getFunction(); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 793 | |
Chandler Carruth | 0fb9981 | 2014-08-13 10:49:33 +0000 | [diff] [blame] | 794 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) |
| 795 | // External node or node we don't want to optimize - skip it; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 796 | return false; |
| 797 | |
| 798 | // Already noalias. |
| 799 | if (F->doesNotAlias(0)) |
| 800 | continue; |
| 801 | |
| 802 | // Definitions with weak linkage may be overridden at linktime, so |
| 803 | // treat them like declarations. |
| 804 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 805 | return false; |
| 806 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 807 | // We annotate noalias return values, which are only applicable to |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 808 | // pointer types. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 809 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 810 | continue; |
| 811 | |
Chandler Carruth | 3824f85 | 2015-09-13 08:23:27 +0000 | [diff] [blame] | 812 | if (!isFunctionMallocLike(F, SCCNodes)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 813 | return false; |
| 814 | } |
| 815 | |
| 816 | bool MadeChange = false; |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 817 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 818 | Function *F = (*I)->getFunction(); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 819 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 820 | continue; |
| 821 | |
| 822 | F->setDoesNotAlias(0); |
| 823 | ++NumNoAlias; |
| 824 | MadeChange = true; |
| 825 | } |
| 826 | |
| 827 | return MadeChange; |
| 828 | } |
| 829 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 830 | /// Tests whether this function is known to not return null. |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 831 | /// |
| 832 | /// Requires that the function returns a pointer. |
| 833 | /// |
| 834 | /// Returns true if it believes the function will not return a null, and sets |
| 835 | /// \p Speculative based on whether the returned conclusion is a speculative |
| 836 | /// conclusion due to SCC calls. |
| 837 | static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes, |
| 838 | const TargetLibraryInfo &TLI, bool &Speculative) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 839 | assert(F->getReturnType()->isPointerTy() && |
| 840 | "nonnull only meaningful on pointer types"); |
| 841 | Speculative = false; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 842 | |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 843 | SmallSetVector<Value *, 8> FlowsToReturn; |
| 844 | for (BasicBlock &BB : *F) |
| 845 | if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) |
| 846 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 847 | |
| 848 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 849 | Value *RetVal = FlowsToReturn[i]; |
| 850 | |
| 851 | // 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] | 852 | if (isKnownNonNull(RetVal, &TLI)) |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 853 | continue; |
| 854 | |
| 855 | // 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] | 856 | // conclusions. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 857 | Instruction *RVI = dyn_cast<Instruction>(RetVal); |
| 858 | if (!RVI) |
| 859 | return false; |
| 860 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 861 | // Extend the analysis by looking upwards. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 862 | case Instruction::BitCast: |
| 863 | case Instruction::GetElementPtr: |
| 864 | case Instruction::AddrSpaceCast: |
| 865 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 866 | continue; |
| 867 | case Instruction::Select: { |
| 868 | SelectInst *SI = cast<SelectInst>(RVI); |
| 869 | FlowsToReturn.insert(SI->getTrueValue()); |
| 870 | FlowsToReturn.insert(SI->getFalseValue()); |
| 871 | continue; |
| 872 | } |
| 873 | case Instruction::PHI: { |
| 874 | PHINode *PN = cast<PHINode>(RVI); |
| 875 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 876 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
| 877 | continue; |
| 878 | } |
| 879 | case Instruction::Call: |
| 880 | case Instruction::Invoke: { |
| 881 | CallSite CS(RVI); |
| 882 | Function *Callee = CS.getCalledFunction(); |
| 883 | // A call to a node within the SCC is assumed to return null until |
| 884 | // proven otherwise |
| 885 | if (Callee && SCCNodes.count(Callee)) { |
| 886 | Speculative = true; |
| 887 | continue; |
| 888 | } |
| 889 | return false; |
| 890 | } |
| 891 | default: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 892 | return false; // Unknown source, may be null |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 893 | }; |
| 894 | llvm_unreachable("should have either continued or returned"); |
| 895 | } |
| 896 | |
| 897 | return true; |
| 898 | } |
| 899 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 900 | /// Deduce nonnull attributes for the SCC. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 901 | bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 902 | SmallPtrSet<Function *, 8> SCCNodes; |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 903 | |
| 904 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 905 | // looking up whether a given CallGraphNode is in this SCC. |
| 906 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 907 | SCCNodes.insert((*I)->getFunction()); |
| 908 | |
| 909 | // Speculative that all functions in the SCC return only nonnull |
| 910 | // pointers. We may refute this as we analyze functions. |
| 911 | bool SCCReturnsNonNull = true; |
| 912 | |
| 913 | bool MadeChange = false; |
| 914 | |
| 915 | // Check each function in turn, determining which functions return nonnull |
| 916 | // pointers. |
| 917 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 918 | Function *F = (*I)->getFunction(); |
| 919 | |
| 920 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) |
| 921 | // External node or node we don't want to optimize - skip it; |
| 922 | return false; |
| 923 | |
| 924 | // Already nonnull. |
| 925 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 926 | Attribute::NonNull)) |
| 927 | continue; |
| 928 | |
| 929 | // Definitions with weak linkage may be overridden at linktime, so |
| 930 | // treat them like declarations. |
| 931 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 932 | return false; |
| 933 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 934 | // We annotate nonnull return values, which are only applicable to |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 935 | // pointer types. |
| 936 | if (!F->getReturnType()->isPointerTy()) |
| 937 | continue; |
| 938 | |
| 939 | bool Speculative = false; |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 940 | if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 941 | if (!Speculative) { |
| 942 | // Mark the function eagerly since we may discover a function |
| 943 | // which prevents us from speculating about the entire SCC |
| 944 | DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); |
| 945 | F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
| 946 | ++NumNonNullReturn; |
| 947 | MadeChange = true; |
| 948 | } |
| 949 | continue; |
| 950 | } |
| 951 | // At least one function returns something which could be null, can't |
| 952 | // speculate any more. |
| 953 | SCCReturnsNonNull = false; |
| 954 | } |
| 955 | |
| 956 | if (SCCReturnsNonNull) { |
| 957 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 958 | Function *F = (*I)->getFunction(); |
| 959 | if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, |
| 960 | Attribute::NonNull) || |
| 961 | !F->getReturnType()->isPointerTy()) |
| 962 | continue; |
| 963 | |
| 964 | DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); |
| 965 | F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); |
| 966 | ++NumNonNullReturn; |
| 967 | MadeChange = true; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return MadeChange; |
| 972 | } |
| 973 | |
Chandler Carruth | d024520 | 2015-09-13 07:50:43 +0000 | [diff] [blame] | 974 | static void setDoesNotAccessMemory(Function &F) { |
| 975 | if (!F.doesNotAccessMemory()) { |
| 976 | F.setDoesNotAccessMemory(); |
| 977 | ++NumAnnotated; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | static void setOnlyReadsMemory(Function &F) { |
| 982 | if (!F.onlyReadsMemory()) { |
| 983 | F.setOnlyReadsMemory(); |
| 984 | ++NumAnnotated; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | static void setDoesNotThrow(Function &F) { |
| 989 | if (!F.doesNotThrow()) { |
| 990 | F.setDoesNotThrow(); |
| 991 | ++NumAnnotated; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | static void setDoesNotCapture(Function &F, unsigned n) { |
| 996 | if (!F.doesNotCapture(n)) { |
| 997 | F.setDoesNotCapture(n); |
| 998 | ++NumAnnotated; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | static void setOnlyReadsMemory(Function &F, unsigned n) { |
| 1003 | if (!F.onlyReadsMemory(n)) { |
| 1004 | F.setOnlyReadsMemory(n); |
| 1005 | ++NumAnnotated; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | static void setDoesNotAlias(Function &F, unsigned n) { |
| 1010 | if (!F.doesNotAlias(n)) { |
| 1011 | F.setDoesNotAlias(n); |
| 1012 | ++NumAnnotated; |
| 1013 | } |
| 1014 | } |
| 1015 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 1016 | /// Analyze the name and prototype of the given function and set any applicable |
| 1017 | /// attributes. |
| 1018 | /// |
| 1019 | /// Returns true if any attributes were set and false otherwise. |
Chandler Carruth | 444d005 | 2015-09-13 08:03:23 +0000 | [diff] [blame] | 1020 | static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) { |
Chandler Carruth | 0fb9981 | 2014-08-13 10:49:33 +0000 | [diff] [blame] | 1021 | if (F.hasFnAttribute(Attribute::OptimizeNone)) |
| 1022 | return false; |
| 1023 | |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1024 | FunctionType *FTy = F.getFunctionType(); |
| 1025 | LibFunc::Func TheLibFunc; |
Chandler Carruth | 444d005 | 2015-09-13 08:03:23 +0000 | [diff] [blame] | 1026 | if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc))) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1027 | return false; |
| 1028 | |
| 1029 | switch (TheLibFunc) { |
| 1030 | case LibFunc::strlen: |
| 1031 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1032 | return false; |
| 1033 | setOnlyReadsMemory(F); |
| 1034 | setDoesNotThrow(F); |
| 1035 | setDoesNotCapture(F, 1); |
| 1036 | break; |
| 1037 | case LibFunc::strchr: |
| 1038 | case LibFunc::strrchr: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1039 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1040 | !FTy->getParamType(1)->isIntegerTy()) |
| 1041 | return false; |
| 1042 | setOnlyReadsMemory(F); |
| 1043 | setDoesNotThrow(F); |
| 1044 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1045 | case LibFunc::strtol: |
| 1046 | case LibFunc::strtod: |
| 1047 | case LibFunc::strtof: |
| 1048 | case LibFunc::strtoul: |
| 1049 | case LibFunc::strtoll: |
| 1050 | case LibFunc::strtold: |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1051 | case LibFunc::strtoull: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1052 | if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1053 | return false; |
| 1054 | setDoesNotThrow(F); |
| 1055 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1056 | setOnlyReadsMemory(F, 1); |
| 1057 | break; |
| 1058 | case LibFunc::strcpy: |
| 1059 | case LibFunc::stpcpy: |
| 1060 | case LibFunc::strcat: |
| 1061 | case LibFunc::strncat: |
| 1062 | case LibFunc::strncpy: |
| 1063 | case LibFunc::stpncpy: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1064 | if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | setDoesNotThrow(F); |
| 1067 | setDoesNotCapture(F, 2); |
| 1068 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | case LibFunc::strxfrm: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1071 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1072 | !FTy->getParamType(1)->isPointerTy()) |
| 1073 | return false; |
| 1074 | setDoesNotThrow(F); |
| 1075 | setDoesNotCapture(F, 1); |
| 1076 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1077 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1078 | break; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1079 | case LibFunc::strcmp: // 0,1 |
| 1080 | case LibFunc::strspn: // 0,1 |
| 1081 | case LibFunc::strncmp: // 0,1 |
| 1082 | case LibFunc::strcspn: // 0,1 |
| 1083 | case LibFunc::strcoll: // 0,1 |
| 1084 | case LibFunc::strcasecmp: // 0,1 |
| 1085 | case LibFunc::strncasecmp: // |
| 1086 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1087 | !FTy->getParamType(1)->isPointerTy()) |
| 1088 | return false; |
| 1089 | setOnlyReadsMemory(F); |
| 1090 | setDoesNotThrow(F); |
| 1091 | setDoesNotCapture(F, 1); |
| 1092 | setDoesNotCapture(F, 2); |
| 1093 | break; |
| 1094 | case LibFunc::strstr: |
| 1095 | case LibFunc::strpbrk: |
| 1096 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1097 | return false; |
| 1098 | setOnlyReadsMemory(F); |
| 1099 | setDoesNotThrow(F); |
| 1100 | setDoesNotCapture(F, 2); |
| 1101 | break; |
| 1102 | case LibFunc::strtok: |
| 1103 | case LibFunc::strtok_r: |
| 1104 | if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1105 | return false; |
| 1106 | setDoesNotThrow(F); |
| 1107 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1108 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | case LibFunc::scanf: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1111 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1112 | return false; |
| 1113 | setDoesNotThrow(F); |
| 1114 | setDoesNotCapture(F, 1); |
| 1115 | setOnlyReadsMemory(F, 1); |
| 1116 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1117 | case LibFunc::setbuf: |
| 1118 | case LibFunc::setvbuf: |
| 1119 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1120 | return false; |
| 1121 | setDoesNotThrow(F); |
| 1122 | setDoesNotCapture(F, 1); |
| 1123 | break; |
| 1124 | case LibFunc::strdup: |
| 1125 | case LibFunc::strndup: |
| 1126 | if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || |
| 1127 | !FTy->getParamType(0)->isPointerTy()) |
| 1128 | return false; |
| 1129 | setDoesNotThrow(F); |
| 1130 | setDoesNotAlias(F, 0); |
| 1131 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1132 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | case LibFunc::stat: |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1135 | case LibFunc::statvfs: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1136 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1137 | !FTy->getParamType(1)->isPointerTy()) |
| 1138 | return false; |
| 1139 | setDoesNotThrow(F); |
| 1140 | setDoesNotCapture(F, 1); |
| 1141 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1142 | setOnlyReadsMemory(F, 1); |
| 1143 | break; |
| 1144 | case LibFunc::sscanf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1145 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1146 | !FTy->getParamType(1)->isPointerTy()) |
| 1147 | return false; |
| 1148 | setDoesNotThrow(F); |
| 1149 | setDoesNotCapture(F, 1); |
| 1150 | setDoesNotCapture(F, 2); |
| 1151 | setOnlyReadsMemory(F, 1); |
| 1152 | setOnlyReadsMemory(F, 2); |
| 1153 | break; |
| 1154 | case LibFunc::sprintf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1155 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1156 | !FTy->getParamType(1)->isPointerTy()) |
| 1157 | return false; |
| 1158 | setDoesNotThrow(F); |
| 1159 | setDoesNotCapture(F, 1); |
| 1160 | setDoesNotCapture(F, 2); |
| 1161 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | case LibFunc::snprintf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1164 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1165 | !FTy->getParamType(2)->isPointerTy()) |
| 1166 | return false; |
| 1167 | setDoesNotThrow(F); |
| 1168 | setDoesNotCapture(F, 1); |
| 1169 | setDoesNotCapture(F, 3); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1170 | setOnlyReadsMemory(F, 3); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1171 | break; |
| 1172 | case LibFunc::setitimer: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1173 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1174 | !FTy->getParamType(2)->isPointerTy()) |
| 1175 | return false; |
| 1176 | setDoesNotThrow(F); |
| 1177 | setDoesNotCapture(F, 2); |
| 1178 | setDoesNotCapture(F, 3); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1179 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1180 | break; |
| 1181 | case LibFunc::system: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1182 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1183 | return false; |
| 1184 | // May throw; "system" is a valid pthread cancellation point. |
| 1185 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1186 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | case LibFunc::malloc: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1189 | if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | setDoesNotThrow(F); |
| 1192 | setDoesNotAlias(F, 0); |
| 1193 | break; |
| 1194 | case LibFunc::memcmp: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1195 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1196 | !FTy->getParamType(1)->isPointerTy()) |
| 1197 | return false; |
| 1198 | setOnlyReadsMemory(F); |
| 1199 | setDoesNotThrow(F); |
| 1200 | setDoesNotCapture(F, 1); |
| 1201 | setDoesNotCapture(F, 2); |
| 1202 | break; |
| 1203 | case LibFunc::memchr: |
| 1204 | case LibFunc::memrchr: |
| 1205 | if (FTy->getNumParams() != 3) |
| 1206 | return false; |
| 1207 | setOnlyReadsMemory(F); |
| 1208 | setDoesNotThrow(F); |
| 1209 | break; |
| 1210 | case LibFunc::modf: |
| 1211 | case LibFunc::modff: |
| 1212 | case LibFunc::modfl: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1213 | if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1214 | return false; |
| 1215 | setDoesNotThrow(F); |
| 1216 | setDoesNotCapture(F, 2); |
| 1217 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1218 | case LibFunc::memcpy: |
| 1219 | case LibFunc::memccpy: |
| 1220 | case LibFunc::memmove: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1221 | if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1222 | return false; |
| 1223 | setDoesNotThrow(F); |
| 1224 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1225 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | case LibFunc::memalign: |
| 1228 | if (!FTy->getReturnType()->isPointerTy()) |
| 1229 | return false; |
| 1230 | setDoesNotAlias(F, 0); |
| 1231 | break; |
| 1232 | case LibFunc::mkdir: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1233 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1234 | return false; |
| 1235 | setDoesNotThrow(F); |
| 1236 | setDoesNotCapture(F, 1); |
| 1237 | setOnlyReadsMemory(F, 1); |
| 1238 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1239 | case LibFunc::mktime: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1240 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1241 | return false; |
| 1242 | setDoesNotThrow(F); |
| 1243 | setDoesNotCapture(F, 1); |
| 1244 | break; |
| 1245 | case LibFunc::realloc: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1246 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1247 | !FTy->getReturnType()->isPointerTy()) |
| 1248 | return false; |
| 1249 | setDoesNotThrow(F); |
| 1250 | setDoesNotAlias(F, 0); |
| 1251 | setDoesNotCapture(F, 1); |
| 1252 | break; |
| 1253 | case LibFunc::read: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1254 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1255 | return false; |
| 1256 | // May throw; "read" is a valid pthread cancellation point. |
| 1257 | setDoesNotCapture(F, 2); |
| 1258 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1259 | case LibFunc::rewind: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1260 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1261 | return false; |
| 1262 | setDoesNotThrow(F); |
| 1263 | setDoesNotCapture(F, 1); |
| 1264 | break; |
| 1265 | case LibFunc::rmdir: |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1266 | case LibFunc::remove: |
| 1267 | case LibFunc::realpath: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1268 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1269 | return false; |
| 1270 | setDoesNotThrow(F); |
| 1271 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1272 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1273 | break; |
| 1274 | case LibFunc::rename: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1275 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1276 | !FTy->getParamType(1)->isPointerTy()) |
| 1277 | return false; |
| 1278 | setDoesNotThrow(F); |
| 1279 | setDoesNotCapture(F, 1); |
| 1280 | setDoesNotCapture(F, 2); |
| 1281 | setOnlyReadsMemory(F, 1); |
| 1282 | setOnlyReadsMemory(F, 2); |
| 1283 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1284 | case LibFunc::readlink: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1285 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1286 | !FTy->getParamType(1)->isPointerTy()) |
| 1287 | return false; |
| 1288 | setDoesNotThrow(F); |
| 1289 | setDoesNotCapture(F, 1); |
| 1290 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1291 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | case LibFunc::write: |
| 1294 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) |
| 1295 | return false; |
| 1296 | // May throw; "write" is a valid pthread cancellation point. |
| 1297 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1298 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1299 | break; |
| 1300 | case LibFunc::bcopy: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1301 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1302 | !FTy->getParamType(1)->isPointerTy()) |
| 1303 | return false; |
| 1304 | setDoesNotThrow(F); |
| 1305 | setDoesNotCapture(F, 1); |
| 1306 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1307 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1308 | break; |
| 1309 | case LibFunc::bcmp: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1310 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1311 | !FTy->getParamType(1)->isPointerTy()) |
| 1312 | return false; |
| 1313 | setDoesNotThrow(F); |
| 1314 | setOnlyReadsMemory(F); |
| 1315 | setDoesNotCapture(F, 1); |
| 1316 | setDoesNotCapture(F, 2); |
| 1317 | break; |
| 1318 | case LibFunc::bzero: |
| 1319 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) |
| 1320 | return false; |
| 1321 | setDoesNotThrow(F); |
| 1322 | setDoesNotCapture(F, 1); |
| 1323 | break; |
| 1324 | case LibFunc::calloc: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1325 | if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1326 | return false; |
| 1327 | setDoesNotThrow(F); |
| 1328 | setDoesNotAlias(F, 0); |
| 1329 | break; |
| 1330 | case LibFunc::chmod: |
| 1331 | case LibFunc::chown: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1332 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
| 1333 | return false; |
| 1334 | setDoesNotThrow(F); |
| 1335 | setDoesNotCapture(F, 1); |
| 1336 | setOnlyReadsMemory(F, 1); |
| 1337 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1338 | case LibFunc::ctermid: |
| 1339 | case LibFunc::clearerr: |
| 1340 | case LibFunc::closedir: |
| 1341 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
| 1342 | return false; |
| 1343 | setDoesNotThrow(F); |
| 1344 | setDoesNotCapture(F, 1); |
| 1345 | break; |
| 1346 | case LibFunc::atoi: |
| 1347 | case LibFunc::atol: |
| 1348 | case LibFunc::atof: |
| 1349 | case LibFunc::atoll: |
| 1350 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1351 | return false; |
| 1352 | setDoesNotThrow(F); |
| 1353 | setOnlyReadsMemory(F); |
| 1354 | setDoesNotCapture(F, 1); |
| 1355 | break; |
| 1356 | case LibFunc::access: |
| 1357 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) |
| 1358 | return false; |
| 1359 | setDoesNotThrow(F); |
| 1360 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1361 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | case LibFunc::fopen: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1364 | if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1365 | !FTy->getParamType(0)->isPointerTy() || |
| 1366 | !FTy->getParamType(1)->isPointerTy()) |
| 1367 | return false; |
| 1368 | setDoesNotThrow(F); |
| 1369 | setDoesNotAlias(F, 0); |
| 1370 | setDoesNotCapture(F, 1); |
| 1371 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1372 | setOnlyReadsMemory(F, 1); |
| 1373 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1374 | break; |
| 1375 | case LibFunc::fdopen: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1376 | if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1377 | !FTy->getParamType(1)->isPointerTy()) |
| 1378 | return false; |
| 1379 | setDoesNotThrow(F); |
| 1380 | setDoesNotAlias(F, 0); |
| 1381 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1382 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1383 | break; |
| 1384 | case LibFunc::feof: |
| 1385 | case LibFunc::free: |
| 1386 | case LibFunc::fseek: |
| 1387 | case LibFunc::ftell: |
| 1388 | case LibFunc::fgetc: |
| 1389 | case LibFunc::fseeko: |
| 1390 | case LibFunc::ftello: |
| 1391 | case LibFunc::fileno: |
| 1392 | case LibFunc::fflush: |
| 1393 | case LibFunc::fclose: |
| 1394 | case LibFunc::fsetpos: |
| 1395 | case LibFunc::flockfile: |
| 1396 | case LibFunc::funlockfile: |
| 1397 | case LibFunc::ftrylockfile: |
| 1398 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
| 1399 | return false; |
| 1400 | setDoesNotThrow(F); |
| 1401 | setDoesNotCapture(F, 1); |
| 1402 | break; |
| 1403 | case LibFunc::ferror: |
| 1404 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1405 | return false; |
| 1406 | setDoesNotThrow(F); |
| 1407 | setDoesNotCapture(F, 1); |
| 1408 | setOnlyReadsMemory(F); |
| 1409 | break; |
| 1410 | case LibFunc::fputc: |
| 1411 | case LibFunc::fstat: |
| 1412 | case LibFunc::frexp: |
| 1413 | case LibFunc::frexpf: |
| 1414 | case LibFunc::frexpl: |
| 1415 | case LibFunc::fstatvfs: |
| 1416 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1417 | return false; |
| 1418 | setDoesNotThrow(F); |
| 1419 | setDoesNotCapture(F, 2); |
| 1420 | break; |
| 1421 | case LibFunc::fgets: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1422 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1423 | !FTy->getParamType(2)->isPointerTy()) |
| 1424 | return false; |
| 1425 | setDoesNotThrow(F); |
| 1426 | setDoesNotCapture(F, 3); |
Nick Lewycky | 26fcc51 | 2013-07-02 05:02:56 +0000 | [diff] [blame] | 1427 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1428 | case LibFunc::fread: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1429 | if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1430 | !FTy->getParamType(3)->isPointerTy()) |
| 1431 | return false; |
| 1432 | setDoesNotThrow(F); |
| 1433 | setDoesNotCapture(F, 1); |
| 1434 | setDoesNotCapture(F, 4); |
| 1435 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1436 | case LibFunc::fwrite: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1437 | if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1438 | !FTy->getParamType(3)->isPointerTy()) |
| 1439 | return false; |
| 1440 | setDoesNotThrow(F); |
| 1441 | setDoesNotCapture(F, 1); |
| 1442 | setDoesNotCapture(F, 4); |
Nick Lewycky | 26fcc51 | 2013-07-02 05:02:56 +0000 | [diff] [blame] | 1443 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1444 | case LibFunc::fputs: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1445 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1446 | !FTy->getParamType(1)->isPointerTy()) |
| 1447 | return false; |
| 1448 | setDoesNotThrow(F); |
| 1449 | setDoesNotCapture(F, 1); |
| 1450 | setDoesNotCapture(F, 2); |
| 1451 | setOnlyReadsMemory(F, 1); |
| 1452 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1453 | case LibFunc::fscanf: |
| 1454 | case LibFunc::fprintf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1455 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1456 | !FTy->getParamType(1)->isPointerTy()) |
| 1457 | return false; |
| 1458 | setDoesNotThrow(F); |
| 1459 | setDoesNotCapture(F, 1); |
| 1460 | setDoesNotCapture(F, 2); |
| 1461 | setOnlyReadsMemory(F, 2); |
| 1462 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1463 | case LibFunc::fgetpos: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1464 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1465 | !FTy->getParamType(1)->isPointerTy()) |
| 1466 | return false; |
| 1467 | setDoesNotThrow(F); |
| 1468 | setDoesNotCapture(F, 1); |
| 1469 | setDoesNotCapture(F, 2); |
| 1470 | break; |
| 1471 | case LibFunc::getc: |
| 1472 | case LibFunc::getlogin_r: |
| 1473 | case LibFunc::getc_unlocked: |
| 1474 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
| 1475 | return false; |
| 1476 | setDoesNotThrow(F); |
| 1477 | setDoesNotCapture(F, 1); |
| 1478 | break; |
| 1479 | case LibFunc::getenv: |
| 1480 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1481 | return false; |
| 1482 | setDoesNotThrow(F); |
| 1483 | setOnlyReadsMemory(F); |
| 1484 | setDoesNotCapture(F, 1); |
| 1485 | break; |
| 1486 | case LibFunc::gets: |
| 1487 | case LibFunc::getchar: |
| 1488 | setDoesNotThrow(F); |
| 1489 | break; |
| 1490 | case LibFunc::getitimer: |
| 1491 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1492 | return false; |
| 1493 | setDoesNotThrow(F); |
| 1494 | setDoesNotCapture(F, 2); |
| 1495 | break; |
| 1496 | case LibFunc::getpwnam: |
| 1497 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1498 | return false; |
| 1499 | setDoesNotThrow(F); |
| 1500 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1501 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | case LibFunc::ungetc: |
| 1504 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1505 | return false; |
| 1506 | setDoesNotThrow(F); |
| 1507 | setDoesNotCapture(F, 2); |
| 1508 | break; |
| 1509 | case LibFunc::uname: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1510 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1511 | return false; |
| 1512 | setDoesNotThrow(F); |
| 1513 | setDoesNotCapture(F, 1); |
| 1514 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1515 | case LibFunc::unlink: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1516 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1517 | return false; |
| 1518 | setDoesNotThrow(F); |
| 1519 | setDoesNotCapture(F, 1); |
Nick Lewycky | cff2cf8 | 2013-07-06 00:59:28 +0000 | [diff] [blame] | 1520 | setOnlyReadsMemory(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1521 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1522 | case LibFunc::unsetenv: |
| 1523 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1524 | return false; |
| 1525 | setDoesNotThrow(F); |
| 1526 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1527 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | case LibFunc::utime: |
| 1530 | case LibFunc::utimes: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1531 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1532 | !FTy->getParamType(1)->isPointerTy()) |
| 1533 | return false; |
| 1534 | setDoesNotThrow(F); |
| 1535 | setDoesNotCapture(F, 1); |
| 1536 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1537 | setOnlyReadsMemory(F, 1); |
| 1538 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | case LibFunc::putc: |
| 1541 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1542 | return false; |
| 1543 | setDoesNotThrow(F); |
| 1544 | setDoesNotCapture(F, 2); |
| 1545 | break; |
| 1546 | case LibFunc::puts: |
| 1547 | case LibFunc::printf: |
| 1548 | case LibFunc::perror: |
| 1549 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1550 | return false; |
| 1551 | setDoesNotThrow(F); |
| 1552 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1553 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1554 | break; |
| 1555 | case LibFunc::pread: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1556 | if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) |
| 1557 | return false; |
| 1558 | // May throw; "pread" is a valid pthread cancellation point. |
| 1559 | setDoesNotCapture(F, 2); |
| 1560 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1561 | case LibFunc::pwrite: |
| 1562 | if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) |
| 1563 | return false; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1564 | // May throw; "pwrite" is a valid pthread cancellation point. |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1565 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1566 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1567 | break; |
| 1568 | case LibFunc::putchar: |
| 1569 | setDoesNotThrow(F); |
| 1570 | break; |
| 1571 | case LibFunc::popen: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1572 | if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1573 | !FTy->getParamType(0)->isPointerTy() || |
| 1574 | !FTy->getParamType(1)->isPointerTy()) |
| 1575 | return false; |
| 1576 | setDoesNotThrow(F); |
| 1577 | setDoesNotAlias(F, 0); |
| 1578 | setDoesNotCapture(F, 1); |
| 1579 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1580 | setOnlyReadsMemory(F, 1); |
| 1581 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | case LibFunc::pclose: |
| 1584 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1585 | return false; |
| 1586 | setDoesNotThrow(F); |
| 1587 | setDoesNotCapture(F, 1); |
| 1588 | break; |
| 1589 | case LibFunc::vscanf: |
| 1590 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1591 | return false; |
| 1592 | setDoesNotThrow(F); |
| 1593 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1594 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1595 | break; |
| 1596 | case LibFunc::vsscanf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1597 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1598 | !FTy->getParamType(2)->isPointerTy()) |
| 1599 | return false; |
| 1600 | setDoesNotThrow(F); |
| 1601 | setDoesNotCapture(F, 1); |
| 1602 | setDoesNotCapture(F, 2); |
| 1603 | setOnlyReadsMemory(F, 1); |
| 1604 | setOnlyReadsMemory(F, 2); |
| 1605 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1606 | case LibFunc::vfscanf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1607 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1608 | !FTy->getParamType(2)->isPointerTy()) |
| 1609 | return false; |
| 1610 | setDoesNotThrow(F); |
| 1611 | setDoesNotCapture(F, 1); |
| 1612 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1613 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | case LibFunc::valloc: |
| 1616 | if (!FTy->getReturnType()->isPointerTy()) |
| 1617 | return false; |
| 1618 | setDoesNotThrow(F); |
| 1619 | setDoesNotAlias(F, 0); |
| 1620 | break; |
| 1621 | case LibFunc::vprintf: |
| 1622 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) |
| 1623 | return false; |
| 1624 | setDoesNotThrow(F); |
| 1625 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1626 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | case LibFunc::vfprintf: |
| 1629 | case LibFunc::vsprintf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1630 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1631 | !FTy->getParamType(1)->isPointerTy()) |
| 1632 | return false; |
| 1633 | setDoesNotThrow(F); |
| 1634 | setDoesNotCapture(F, 1); |
| 1635 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1636 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | case LibFunc::vsnprintf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1639 | if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1640 | !FTy->getParamType(2)->isPointerTy()) |
| 1641 | return false; |
| 1642 | setDoesNotThrow(F); |
| 1643 | setDoesNotCapture(F, 1); |
| 1644 | setDoesNotCapture(F, 3); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1645 | setOnlyReadsMemory(F, 3); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | case LibFunc::open: |
| 1648 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) |
| 1649 | return false; |
| 1650 | // May throw; "open" is a valid pthread cancellation point. |
| 1651 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1652 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1653 | break; |
| 1654 | case LibFunc::opendir: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1655 | if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1656 | !FTy->getParamType(0)->isPointerTy()) |
| 1657 | return false; |
| 1658 | setDoesNotThrow(F); |
| 1659 | setDoesNotAlias(F, 0); |
| 1660 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1661 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | case LibFunc::tmpfile: |
| 1664 | if (!FTy->getReturnType()->isPointerTy()) |
| 1665 | return false; |
| 1666 | setDoesNotThrow(F); |
| 1667 | setDoesNotAlias(F, 0); |
| 1668 | break; |
| 1669 | case LibFunc::times: |
| 1670 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1671 | return false; |
| 1672 | setDoesNotThrow(F); |
| 1673 | setDoesNotCapture(F, 1); |
| 1674 | break; |
| 1675 | case LibFunc::htonl: |
| 1676 | case LibFunc::htons: |
| 1677 | case LibFunc::ntohl: |
| 1678 | case LibFunc::ntohs: |
| 1679 | setDoesNotThrow(F); |
| 1680 | setDoesNotAccessMemory(F); |
| 1681 | break; |
| 1682 | case LibFunc::lstat: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1683 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1684 | !FTy->getParamType(1)->isPointerTy()) |
| 1685 | return false; |
| 1686 | setDoesNotThrow(F); |
| 1687 | setDoesNotCapture(F, 1); |
| 1688 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1689 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1690 | break; |
| 1691 | case LibFunc::lchown: |
| 1692 | if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy()) |
| 1693 | return false; |
| 1694 | setDoesNotThrow(F); |
| 1695 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1696 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | case LibFunc::qsort: |
| 1699 | if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy()) |
| 1700 | return false; |
| 1701 | // May throw; places call through function pointer. |
| 1702 | setDoesNotCapture(F, 4); |
| 1703 | break; |
| 1704 | case LibFunc::dunder_strdup: |
| 1705 | case LibFunc::dunder_strndup: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1706 | if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1707 | !FTy->getParamType(0)->isPointerTy()) |
| 1708 | return false; |
| 1709 | setDoesNotThrow(F); |
| 1710 | setDoesNotAlias(F, 0); |
| 1711 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1712 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | case LibFunc::dunder_strtok_r: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1715 | if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1716 | return false; |
| 1717 | setDoesNotThrow(F); |
| 1718 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1719 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1720 | break; |
| 1721 | case LibFunc::under_IO_getc: |
| 1722 | if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
| 1723 | return false; |
| 1724 | setDoesNotThrow(F); |
| 1725 | setDoesNotCapture(F, 1); |
| 1726 | break; |
| 1727 | case LibFunc::under_IO_putc: |
| 1728 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1729 | return false; |
| 1730 | setDoesNotThrow(F); |
| 1731 | setDoesNotCapture(F, 2); |
| 1732 | break; |
| 1733 | case LibFunc::dunder_isoc99_scanf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1734 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1735 | return false; |
| 1736 | setDoesNotThrow(F); |
| 1737 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1738 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1739 | break; |
| 1740 | case LibFunc::stat64: |
| 1741 | case LibFunc::lstat64: |
| 1742 | case LibFunc::statvfs64: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1743 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1744 | !FTy->getParamType(1)->isPointerTy()) |
| 1745 | return false; |
| 1746 | setDoesNotThrow(F); |
| 1747 | setDoesNotCapture(F, 1); |
| 1748 | setDoesNotCapture(F, 2); |
| 1749 | setOnlyReadsMemory(F, 1); |
| 1750 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1751 | case LibFunc::dunder_isoc99_sscanf: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1752 | if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1753 | !FTy->getParamType(1)->isPointerTy()) |
| 1754 | return false; |
| 1755 | setDoesNotThrow(F); |
| 1756 | setDoesNotCapture(F, 1); |
| 1757 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1758 | setOnlyReadsMemory(F, 1); |
| 1759 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | case LibFunc::fopen64: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 1762 | if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1763 | !FTy->getParamType(0)->isPointerTy() || |
| 1764 | !FTy->getParamType(1)->isPointerTy()) |
| 1765 | return false; |
| 1766 | setDoesNotThrow(F); |
| 1767 | setDoesNotAlias(F, 0); |
| 1768 | setDoesNotCapture(F, 1); |
| 1769 | setDoesNotCapture(F, 2); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1770 | setOnlyReadsMemory(F, 1); |
| 1771 | setOnlyReadsMemory(F, 2); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1772 | break; |
| 1773 | case LibFunc::fseeko64: |
| 1774 | case LibFunc::ftello64: |
| 1775 | if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
| 1776 | return false; |
| 1777 | setDoesNotThrow(F); |
| 1778 | setDoesNotCapture(F, 1); |
| 1779 | break; |
| 1780 | case LibFunc::tmpfile64: |
| 1781 | if (!FTy->getReturnType()->isPointerTy()) |
| 1782 | return false; |
| 1783 | setDoesNotThrow(F); |
| 1784 | setDoesNotAlias(F, 0); |
| 1785 | break; |
| 1786 | case LibFunc::fstat64: |
| 1787 | case LibFunc::fstatvfs64: |
| 1788 | if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) |
| 1789 | return false; |
| 1790 | setDoesNotThrow(F); |
| 1791 | setDoesNotCapture(F, 2); |
| 1792 | break; |
| 1793 | case LibFunc::open64: |
| 1794 | if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) |
| 1795 | return false; |
| 1796 | // May throw; "open" is a valid pthread cancellation point. |
| 1797 | setDoesNotCapture(F, 1); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1798 | setOnlyReadsMemory(F, 1); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1799 | break; |
Michael Gottesman | 2db1116 | 2013-07-03 04:00:54 +0000 | [diff] [blame] | 1800 | case LibFunc::gettimeofday: |
| 1801 | if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
| 1802 | !FTy->getParamType(1)->isPointerTy()) |
| 1803 | return false; |
| 1804 | // Currently some platforms have the restrict keyword on the arguments to |
| 1805 | // gettimeofday. To be conservative, do not add noalias to gettimeofday's |
| 1806 | // arguments. |
| 1807 | setDoesNotThrow(F); |
| 1808 | setDoesNotCapture(F, 1); |
| 1809 | setDoesNotCapture(F, 2); |
Rafael Espindola | 5e66a7e | 2014-03-30 03:26:17 +0000 | [diff] [blame] | 1810 | break; |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1811 | default: |
| 1812 | // Didn't mark any attributes. |
| 1813 | return false; |
| 1814 | } |
| 1815 | |
| 1816 | return true; |
| 1817 | } |
| 1818 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 1819 | /// Adds attributes to well-known standard library call declarations. |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1820 | bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) { |
| 1821 | bool MadeChange = false; |
| 1822 | |
| 1823 | // Check each function in turn annotating well-known library function |
| 1824 | // declarations with attributes. |
| 1825 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 1826 | Function *F = (*I)->getFunction(); |
| 1827 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1828 | if (F && F->isDeclaration()) |
Chandler Carruth | 444d005 | 2015-09-13 08:03:23 +0000 | [diff] [blame] | 1829 | MadeChange |= inferPrototypeAttributes(*F, *TLI); |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | return MadeChange; |
| 1833 | } |
| 1834 | |
Chris Lattner | 4422d31 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 1835 | bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1836 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Dan Gohman | 86449d7 | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 1837 | |
Meador Inge | 6b6a161 | 2013-03-21 00:55:59 +0000 | [diff] [blame] | 1838 | bool Changed = annotateLibraryCalls(SCC); |
| 1839 | Changed |= AddReadAttrs(SCC); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 1840 | Changed |= AddArgumentAttrs(SCC); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 1841 | Changed |= AddNoAliasAttrs(SCC); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 1842 | Changed |= AddNonNullAttrs(SCC); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 1843 | return Changed; |
| 1844 | } |