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