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