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