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