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