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