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