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 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// This file implements interprocedural passes which walk the |
| 12 | /// call-graph deducing and/or propagating function attributes. |
| 13 | /// |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/IPO.h" |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SCCIterator.h" |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SetVector.h" |
Duncan Sands | b193a37 | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
James Molloy | 0ecdbe7 | 2015-11-19 08:49:57 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 839a98e | 2013-01-07 15:26:48 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CallGraphSCCPass.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 32 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/LLVMContext.h" |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Hans Wennborg | 043bf5b | 2015-08-31 21:19:18 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "functionattrs" |
| 41 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 42 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 43 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 44 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 45 | STATISTIC(NumReturned, "Number of arguments marked returned"); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 46 | STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); |
| 47 | STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 48 | STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 49 | STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 50 | STATISTIC(NumNoRecurse, "Number of functions marked as norecurse"); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 51 | |
Sanjay Patel | 4f74216 | 2017-02-13 23:10:51 +0000 | [diff] [blame] | 52 | // FIXME: This is disabled by default to avoid exposing security vulnerabilities |
| 53 | // in C/C++ code compiled by clang: |
| 54 | // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html |
| 55 | static cl::opt<bool> EnableNonnullArgPropagation( |
| 56 | "enable-nonnull-arg-prop", cl::Hidden, |
| 57 | cl::desc("Try to propagate nonnull argument attributes from callsites to " |
| 58 | "caller functions.")); |
| 59 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 60 | namespace { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 61 | typedef SmallSetVector<Function *, 8> SCCNodeSet; |
| 62 | } |
| 63 | |
Peter Collingbourne | c45f7f3 | 2017-02-14 00:28:13 +0000 | [diff] [blame] | 64 | /// Returns the memory access attribute for function F using AAR for AA results, |
| 65 | /// where SCCNodes is the current SCC. |
| 66 | /// |
| 67 | /// If ThisBody is true, this function may examine the function body and will |
| 68 | /// return a result pertaining to this copy of the function. If it is false, the |
| 69 | /// result will be based only on AA results for the function declaration; it |
| 70 | /// will be assumed that some other (perhaps less optimized) version of the |
| 71 | /// function may be selected at link time. |
| 72 | static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody, |
| 73 | AAResults &AAR, |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 74 | const SCCNodeSet &SCCNodes) { |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 75 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); |
| 76 | if (MRB == FMRB_DoesNotAccessMemory) |
| 77 | // Already perfect! |
| 78 | return MAK_ReadNone; |
| 79 | |
Peter Collingbourne | c45f7f3 | 2017-02-14 00:28:13 +0000 | [diff] [blame] | 80 | if (!ThisBody) { |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 81 | if (AliasAnalysis::onlyReadsMemory(MRB)) |
| 82 | return MAK_ReadOnly; |
| 83 | |
| 84 | // Conservatively assume it writes to memory. |
| 85 | return MAK_MayWrite; |
| 86 | } |
| 87 | |
| 88 | // Scan the function body for instructions that may read or write memory. |
| 89 | bool ReadsMemory = false; |
| 90 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 91 | Instruction *I = &*II; |
| 92 | |
| 93 | // Some instructions can be ignored even if they read or write memory. |
| 94 | // Detect these now, skipping to the next instruction if one is found. |
| 95 | CallSite CS(cast<Value>(I)); |
| 96 | if (CS) { |
Sanjoy Das | 10c8a04 | 2016-02-09 18:40:40 +0000 | [diff] [blame] | 97 | // Ignore calls to functions in the same SCC, as long as the call sites |
| 98 | // don't have operand bundles. Calls with operand bundles are allowed to |
| 99 | // have memory effects not described by the memory effects of the call |
| 100 | // target. |
| 101 | if (!CS.hasOperandBundles() && CS.getCalledFunction() && |
| 102 | SCCNodes.count(CS.getCalledFunction())) |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 103 | continue; |
| 104 | FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS); |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 105 | |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 106 | // If the call doesn't access memory, we're done. |
| 107 | if (!(MRB & MRI_ModRef)) |
| 108 | continue; |
| 109 | |
| 110 | if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 111 | // The call could access any memory. If that includes writes, give up. |
| 112 | if (MRB & MRI_Mod) |
| 113 | return MAK_MayWrite; |
| 114 | // If it reads, note it. |
| 115 | if (MRB & MRI_Ref) |
| 116 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 119 | |
| 120 | // Check whether all pointer arguments point to local memory, and |
| 121 | // ignore calls that only access local memory. |
| 122 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 123 | CI != CE; ++CI) { |
| 124 | Value *Arg = *CI; |
Elena Demikhovsky | 3ec9e15 | 2015-11-17 19:30:51 +0000 | [diff] [blame] | 125 | if (!Arg->getType()->isPtrOrPtrVectorTy()) |
Chandler Carruth | 69798fb | 2015-10-27 01:41:43 +0000 | [diff] [blame] | 126 | continue; |
| 127 | |
| 128 | AAMDNodes AAInfo; |
| 129 | I->getAAMetadata(AAInfo); |
| 130 | MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); |
| 131 | |
| 132 | // Skip accesses to local or constant memory as they don't impact the |
| 133 | // externally visible mod/ref behavior. |
| 134 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 135 | continue; |
| 136 | |
| 137 | if (MRB & MRI_Mod) |
| 138 | // Writes non-local memory. Give up. |
| 139 | return MAK_MayWrite; |
| 140 | if (MRB & MRI_Ref) |
| 141 | // Ok, it reads non-local memory. |
| 142 | ReadsMemory = true; |
| 143 | } |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 144 | continue; |
| 145 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 146 | // Ignore non-volatile loads from local memory. (Atomic is okay here.) |
| 147 | if (!LI->isVolatile()) { |
| 148 | MemoryLocation Loc = MemoryLocation::get(LI); |
| 149 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 150 | continue; |
| 151 | } |
| 152 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 153 | // Ignore non-volatile stores to local memory. (Atomic is okay here.) |
| 154 | if (!SI->isVolatile()) { |
| 155 | MemoryLocation Loc = MemoryLocation::get(SI); |
| 156 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 157 | continue; |
| 158 | } |
| 159 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { |
| 160 | // Ignore vaargs on local memory. |
| 161 | MemoryLocation Loc = MemoryLocation::get(VI); |
| 162 | if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | // Any remaining instructions need to be taken seriously! Check if they |
| 167 | // read or write memory. |
| 168 | if (I->mayWriteToMemory()) |
| 169 | // Writes memory. Just give up. |
| 170 | return MAK_MayWrite; |
| 171 | |
| 172 | // If this instruction may read memory, remember that. |
| 173 | ReadsMemory |= I->mayReadFromMemory(); |
| 174 | } |
| 175 | |
| 176 | return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; |
| 177 | } |
| 178 | |
Peter Collingbourne | c45f7f3 | 2017-02-14 00:28:13 +0000 | [diff] [blame] | 179 | MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F, |
| 180 | AAResults &AAR) { |
| 181 | return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {}); |
| 182 | } |
| 183 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 184 | /// Deduce readonly/readnone attributes for the SCC. |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 185 | template <typename AARGetterT> |
Peter Collingbourne | cea1e4e | 2017-02-09 23:11:52 +0000 | [diff] [blame] | 186 | static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 187 | // Check if any of the functions in the SCC read or write memory. If they |
| 188 | // write memory then they can't be marked readnone or readonly. |
| 189 | bool ReadsMemory = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 190 | for (Function *F : SCCNodes) { |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 191 | // Call the callable parameter to look up AA results for this function. |
| 192 | AAResults &AAR = AARGetter(*F); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 193 | |
Peter Collingbourne | c45f7f3 | 2017-02-14 00:28:13 +0000 | [diff] [blame] | 194 | // Non-exact function definitions may not be selected at link time, and an |
| 195 | // alternative version that writes to memory may be selected. See the |
| 196 | // comment on GlobalValue::isDefinitionExact for more details. |
| 197 | switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(), |
| 198 | AAR, SCCNodes)) { |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 199 | case MAK_MayWrite: |
| 200 | return false; |
| 201 | case MAK_ReadOnly: |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 202 | ReadsMemory = true; |
Chandler Carruth | 7542d37 | 2015-09-21 17:39:41 +0000 | [diff] [blame] | 203 | break; |
| 204 | case MAK_ReadNone: |
| 205 | // Nothing to do! |
| 206 | break; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 211 | // Give them the appropriate attribute. |
| 212 | bool MadeChange = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 213 | for (Function *F : SCCNodes) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 214 | if (F->doesNotAccessMemory()) |
| 215 | // Already perfect! |
| 216 | continue; |
| 217 | |
| 218 | if (F->onlyReadsMemory() && ReadsMemory) |
| 219 | // No change. |
| 220 | continue; |
| 221 | |
| 222 | MadeChange = true; |
| 223 | |
| 224 | // Clear out any existing attributes. |
Bill Wendling | 50d2784 | 2012-10-15 20:35:56 +0000 | [diff] [blame] | 225 | AttrBuilder B; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 226 | B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
| 227 | F->removeAttributes( |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 228 | AttributeList::FunctionIndex, |
| 229 | AttributeList::get(F->getContext(), AttributeList::FunctionIndex, B)); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 230 | |
| 231 | // Add in the new attribute. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 232 | F->addAttribute(AttributeList::FunctionIndex, |
Bill Wendling | c0e2a1f | 2013-01-23 00:20:53 +0000 | [diff] [blame] | 233 | ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 234 | |
| 235 | if (ReadsMemory) |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 236 | ++NumReadOnly; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 237 | else |
Duncan Sands | cefc860 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 238 | ++NumReadNone; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return MadeChange; |
| 242 | } |
| 243 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 244 | namespace { |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 245 | /// For a given pointer Argument, this retains a list of Arguments of functions |
| 246 | /// in the same SCC that the pointer data flows into. We use this to build an |
| 247 | /// SCC of the arguments. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 248 | struct ArgumentGraphNode { |
| 249 | Argument *Definition; |
| 250 | SmallVector<ArgumentGraphNode *, 4> Uses; |
| 251 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 252 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 253 | class ArgumentGraph { |
| 254 | // We store pointers to ArgumentGraphNode objects, so it's important that |
| 255 | // that they not move around upon insert. |
| 256 | typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 257 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 258 | ArgumentMapTy ArgumentMap; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 259 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 260 | // There is no root node for the argument graph, in fact: |
| 261 | // void f(int *x, int *y) { if (...) f(x, y); } |
| 262 | // is an example where the graph is disconnected. The SCCIterator requires a |
| 263 | // single entry point, so we maintain a fake ("synthetic") root node that |
| 264 | // uses every node. Because the graph is directed and nothing points into |
| 265 | // the root, it will not participate in any SCCs (except for its own). |
| 266 | ArgumentGraphNode SyntheticRoot; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 267 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 268 | public: |
| 269 | ArgumentGraph() { SyntheticRoot.Definition = nullptr; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 270 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 271 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 272 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 273 | iterator begin() { return SyntheticRoot.Uses.begin(); } |
| 274 | iterator end() { return SyntheticRoot.Uses.end(); } |
| 275 | ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 276 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 277 | ArgumentGraphNode *operator[](Argument *A) { |
| 278 | ArgumentGraphNode &Node = ArgumentMap[A]; |
| 279 | Node.Definition = A; |
| 280 | SyntheticRoot.Uses.push_back(&Node); |
| 281 | return &Node; |
| 282 | } |
| 283 | }; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 284 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 285 | /// This tracker checks whether callees are in the SCC, and if so it does not |
| 286 | /// consider that a capture, instead adding it to the "Uses" list and |
| 287 | /// continuing with the analysis. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 288 | struct ArgumentUsesTracker : public CaptureTracker { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 289 | ArgumentUsesTracker(const SCCNodeSet &SCCNodes) |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 290 | : Captured(false), SCCNodes(SCCNodes) {} |
| 291 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 292 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 293 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 294 | bool captured(const Use *U) override { |
| 295 | CallSite CS(U->getUser()); |
| 296 | if (!CS.getInstruction()) { |
| 297 | Captured = true; |
| 298 | return true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 301 | Function *F = CS.getCalledFunction(); |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 302 | if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 303 | Captured = true; |
| 304 | return true; |
| 305 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 306 | |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 307 | // Note: the callee and the two successor blocks *follow* the argument |
| 308 | // operands. This means there is no need to adjust UseIndex to account for |
| 309 | // these. |
| 310 | |
| 311 | unsigned UseIndex = |
| 312 | std::distance(const_cast<const Use *>(CS.arg_begin()), U); |
| 313 | |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 314 | assert(UseIndex < CS.data_operands_size() && |
| 315 | "Indirect function calls should have been filtered above!"); |
| 316 | |
| 317 | if (UseIndex >= CS.getNumArgOperands()) { |
| 318 | // Data operand, but not a argument operand -- must be a bundle operand |
| 319 | assert(CS.hasOperandBundles() && "Must be!"); |
| 320 | |
| 321 | // CaptureTracking told us that we're being captured by an operand bundle |
| 322 | // use. In this case it does not matter if the callee is within our SCC |
| 323 | // or not -- we've been captured in some unknown way, and we have to be |
| 324 | // conservative. |
| 325 | Captured = true; |
| 326 | return true; |
| 327 | } |
| 328 | |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 329 | if (UseIndex >= F->arg_size()) { |
| 330 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 331 | Captured = true; |
| 332 | return true; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 333 | } |
Sanjoy Das | 98bfe26 | 2015-11-05 03:04:40 +0000 | [diff] [blame] | 334 | |
Duncan P. N. Exon Smith | 83c4b68 | 2015-11-07 00:01:16 +0000 | [diff] [blame] | 335 | Uses.push_back(&*std::next(F->arg_begin(), UseIndex)); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | |
| 339 | bool Captured; // True only if certainly captured (used outside our SCC). |
| 340 | SmallVector<Argument *, 4> Uses; // Uses within our SCC. |
| 341 | |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 342 | const SCCNodeSet &SCCNodes; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 343 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 344 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 345 | |
| 346 | namespace llvm { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 347 | template <> struct GraphTraits<ArgumentGraphNode *> { |
Tim Shen | b44909e | 2016-08-01 22:32:20 +0000 | [diff] [blame] | 348 | typedef ArgumentGraphNode *NodeRef; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 349 | typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 350 | |
Tim Shen | 48f814e | 2016-08-31 16:48:13 +0000 | [diff] [blame] | 351 | static NodeRef getEntryNode(NodeRef A) { return A; } |
| 352 | static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); } |
| 353 | static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 354 | }; |
| 355 | template <> |
| 356 | struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { |
Tim Shen | f2187ed | 2016-08-22 21:09:30 +0000 | [diff] [blame] | 357 | static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 358 | static ChildIteratorType nodes_begin(ArgumentGraph *AG) { |
| 359 | return AG->begin(); |
| 360 | } |
| 361 | static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } |
| 362 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 363 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 364 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 365 | /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 366 | static Attribute::AttrKind |
| 367 | determinePointerReadAttrs(Argument *A, |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 368 | const SmallPtrSet<Argument *, 8> &SCCNodes) { |
| 369 | |
| 370 | SmallVector<Use *, 32> Worklist; |
| 371 | SmallSet<Use *, 32> Visited; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 372 | |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 373 | // inalloca arguments are always clobbered by the call. |
| 374 | if (A->hasInAllocaAttr()) |
| 375 | return Attribute::None; |
| 376 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 377 | bool IsRead = false; |
| 378 | // We don't need to track IsWritten. If A is written to, return immediately. |
| 379 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 380 | for (Use &U : A->uses()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 381 | Visited.insert(&U); |
| 382 | Worklist.push_back(&U); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | while (!Worklist.empty()) { |
| 386 | Use *U = Worklist.pop_back_val(); |
| 387 | Instruction *I = cast<Instruction>(U->getUser()); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 388 | |
| 389 | switch (I->getOpcode()) { |
| 390 | case Instruction::BitCast: |
| 391 | case Instruction::GetElementPtr: |
| 392 | case Instruction::PHI: |
| 393 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 394 | case Instruction::AddrSpaceCast: |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 395 | // 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] | 396 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 397 | if (Visited.insert(&UU).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 398 | Worklist.push_back(&UU); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | case Instruction::Call: |
| 402 | case Instruction::Invoke: { |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 403 | bool Captures = true; |
| 404 | |
| 405 | if (I->getType()->isVoidTy()) |
| 406 | Captures = false; |
| 407 | |
| 408 | auto AddUsersToWorklistIfCapturing = [&] { |
| 409 | if (Captures) |
| 410 | for (Use &UU : I->uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 411 | if (Visited.insert(&UU).second) |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 412 | Worklist.push_back(&UU); |
| 413 | }; |
| 414 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 415 | CallSite CS(I); |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 416 | if (CS.doesNotAccessMemory()) { |
| 417 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 418 | continue; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 419 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 420 | |
| 421 | Function *F = CS.getCalledFunction(); |
| 422 | if (!F) { |
| 423 | if (CS.onlyReadsMemory()) { |
| 424 | IsRead = true; |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 425 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | return Attribute::None; |
| 429 | } |
| 430 | |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 431 | // Note: the callee and the two successor blocks *follow* the argument |
| 432 | // operands. This means there is no need to adjust UseIndex to account |
| 433 | // for these. |
| 434 | |
| 435 | unsigned UseIndex = std::distance(CS.arg_begin(), U); |
| 436 | |
Sanjoy Das | ea1df7f | 2015-11-07 01:56:07 +0000 | [diff] [blame] | 437 | // U cannot be the callee operand use: since we're exploring the |
| 438 | // transitive uses of an Argument, having such a use be a callee would |
| 439 | // imply the CallSite is an indirect call or invoke; and we'd take the |
| 440 | // early exit above. |
| 441 | assert(UseIndex < CS.data_operands_size() && |
| 442 | "Data operand use expected!"); |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 443 | |
| 444 | bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands(); |
| 445 | |
| 446 | if (UseIndex >= F->arg_size() && !IsOperandBundleUse) { |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 447 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 448 | return Attribute::None; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 449 | } |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 450 | |
Tilmann Scheller | 925b193 | 2015-11-20 19:17:10 +0000 | [diff] [blame] | 451 | Captures &= !CS.doesNotCapture(UseIndex); |
| 452 | |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 453 | // Since the optimizer (by design) cannot see the data flow corresponding |
| 454 | // to a operand bundle use, these cannot participate in the optimistic SCC |
| 455 | // analysis. Instead, we model the operand bundle uses as arguments in |
| 456 | // call to a function external to the SCC. |
Duncan P. N. Exon Smith | 9e3edad | 2016-08-17 01:23:58 +0000 | [diff] [blame] | 457 | if (IsOperandBundleUse || |
| 458 | !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) { |
Sanjoy Das | 71fe81f | 2015-11-07 01:56:00 +0000 | [diff] [blame] | 459 | |
| 460 | // The accessors used on CallSite here do the right thing for calls and |
| 461 | // invokes with operand bundles. |
| 462 | |
Sanjoy Das | 436e239 | 2015-11-07 01:55:53 +0000 | [diff] [blame] | 463 | if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex)) |
| 464 | return Attribute::None; |
| 465 | if (!CS.doesNotAccessMemory(UseIndex)) |
| 466 | IsRead = true; |
| 467 | } |
| 468 | |
Nick Lewycky | 59633cb | 2014-05-30 02:31:27 +0000 | [diff] [blame] | 469 | AddUsersToWorklistIfCapturing(); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | |
| 473 | case Instruction::Load: |
David Majnemer | 124bdb7 | 2016-05-25 05:53:04 +0000 | [diff] [blame] | 474 | // A volatile load has side effects beyond what readonly can be relied |
| 475 | // upon. |
| 476 | if (cast<LoadInst>(I)->isVolatile()) |
| 477 | return Attribute::None; |
| 478 | |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 479 | IsRead = true; |
| 480 | break; |
| 481 | |
| 482 | case Instruction::ICmp: |
| 483 | case Instruction::Ret: |
| 484 | break; |
| 485 | |
| 486 | default: |
| 487 | return Attribute::None; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; |
| 492 | } |
| 493 | |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 494 | /// Deduce returned attributes for the SCC. |
| 495 | static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) { |
| 496 | bool Changed = false; |
| 497 | |
| 498 | AttrBuilder B; |
| 499 | B.addAttribute(Attribute::Returned); |
| 500 | |
| 501 | // Check each function in turn, determining if an argument is always returned. |
| 502 | for (Function *F : SCCNodes) { |
| 503 | // We can infer and propagate function attributes only when we know that the |
| 504 | // definition we'll get at link time is *exactly* the definition we see now. |
| 505 | // For more details, see GlobalValue::mayBeDerefined. |
| 506 | if (!F->hasExactDefinition()) |
| 507 | continue; |
| 508 | |
| 509 | if (F->getReturnType()->isVoidTy()) |
| 510 | continue; |
| 511 | |
David Majnemer | c83044d | 2016-09-12 16:04:59 +0000 | [diff] [blame] | 512 | // There is nothing to do if an argument is already marked as 'returned'. |
| 513 | if (any_of(F->args(), |
| 514 | [](const Argument &Arg) { return Arg.hasReturnedAttr(); })) |
| 515 | continue; |
| 516 | |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 517 | auto FindRetArg = [&]() -> Value * { |
| 518 | Value *RetArg = nullptr; |
| 519 | for (BasicBlock &BB : *F) |
| 520 | if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) { |
| 521 | // Note that stripPointerCasts should look through functions with |
| 522 | // returned arguments. |
| 523 | Value *RetVal = Ret->getReturnValue()->stripPointerCasts(); |
| 524 | if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType()) |
| 525 | return nullptr; |
| 526 | |
| 527 | if (!RetArg) |
| 528 | RetArg = RetVal; |
| 529 | else if (RetArg != RetVal) |
| 530 | return nullptr; |
| 531 | } |
| 532 | |
| 533 | return RetArg; |
| 534 | }; |
| 535 | |
| 536 | if (Value *RetArg = FindRetArg()) { |
| 537 | auto *A = cast<Argument>(RetArg); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 538 | A->addAttr(AttributeList::get(F->getContext(), A->getArgNo() + 1, B)); |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 539 | ++NumReturned; |
| 540 | Changed = true; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return Changed; |
| 545 | } |
| 546 | |
Sanjay Patel | 4f74216 | 2017-02-13 23:10:51 +0000 | [diff] [blame] | 547 | /// If a callsite has arguments that are also arguments to the parent function, |
| 548 | /// try to propagate attributes from the callsite's arguments to the parent's |
| 549 | /// arguments. This may be important because inlining can cause information loss |
| 550 | /// when attribute knowledge disappears with the inlined call. |
| 551 | static bool addArgumentAttrsFromCallsites(Function &F) { |
| 552 | if (!EnableNonnullArgPropagation) |
| 553 | return false; |
| 554 | |
| 555 | bool Changed = false; |
| 556 | |
| 557 | // For an argument attribute to transfer from a callsite to the parent, the |
| 558 | // call must be guaranteed to execute every time the parent is called. |
| 559 | // Conservatively, just check for calls in the entry block that are guaranteed |
| 560 | // to execute. |
| 561 | // TODO: This could be enhanced by testing if the callsite post-dominates the |
| 562 | // entry block or by doing simple forward walks or backward walks to the |
| 563 | // callsite. |
| 564 | BasicBlock &Entry = F.getEntryBlock(); |
| 565 | for (Instruction &I : Entry) { |
| 566 | if (auto CS = CallSite(&I)) { |
| 567 | if (auto *CalledFunc = CS.getCalledFunction()) { |
| 568 | for (auto &CSArg : CalledFunc->args()) { |
| 569 | if (!CSArg.hasNonNullAttr()) |
| 570 | continue; |
| 571 | |
| 572 | // If the non-null callsite argument operand is an argument to 'F' |
| 573 | // (the caller) and the call is guaranteed to execute, then the value |
| 574 | // must be non-null throughout 'F'. |
| 575 | auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo())); |
| 576 | if (FArg && !FArg->hasNonNullAttr()) { |
| 577 | FArg->addAttr(Attribute::NonNull); |
| 578 | Changed = true; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | return Changed; |
| 588 | } |
| 589 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 590 | /// Deduce nocapture attributes for the SCC. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 591 | static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) { |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 592 | bool Changed = false; |
| 593 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 594 | ArgumentGraph AG; |
| 595 | |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 596 | AttrBuilder B; |
| 597 | B.addAttribute(Attribute::NoCapture); |
| 598 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 599 | // Check each function in turn, determining which pointer arguments are not |
| 600 | // captured. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 601 | for (Function *F : SCCNodes) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 602 | // We can infer and propagate function attributes only when we know that the |
| 603 | // definition we'll get at link time is *exactly* the definition we see now. |
| 604 | // For more details, see GlobalValue::mayBeDerefined. |
| 605 | if (!F->hasExactDefinition()) |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 606 | continue; |
| 607 | |
Sanjay Patel | 4f74216 | 2017-02-13 23:10:51 +0000 | [diff] [blame] | 608 | Changed |= addArgumentAttrsFromCallsites(*F); |
| 609 | |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 610 | // Functions that are readonly (or readnone) and nounwind and don't return |
| 611 | // a value can't capture arguments. Don't analyze them. |
| 612 | if (F->onlyReadsMemory() && F->doesNotThrow() && |
| 613 | F->getReturnType()->isVoidTy()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 614 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 615 | ++A) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 616 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 617 | A->addAttr(AttributeList::get(F->getContext(), A->getArgNo() + 1, B)); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 618 | ++NumNoCapture; |
| 619 | Changed = true; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 620 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 621 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 622 | continue; |
Benjamin Kramer | 76b7bd0 | 2013-06-22 15:51:19 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 625 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
| 626 | ++A) { |
| 627 | if (!A->getType()->isPointerTy()) |
| 628 | continue; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 629 | bool HasNonLocalUses = false; |
| 630 | if (!A->hasNoCaptureAttr()) { |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 631 | ArgumentUsesTracker Tracker(SCCNodes); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 632 | PointerMayBeCaptured(&*A, &Tracker); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 633 | if (!Tracker.Captured) { |
| 634 | if (Tracker.Uses.empty()) { |
| 635 | // If it's trivially not captured, mark it nocapture now. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 636 | A->addAttr( |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 637 | AttributeList::get(F->getContext(), A->getArgNo() + 1, B)); |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 638 | ++NumNoCapture; |
| 639 | Changed = true; |
| 640 | } else { |
| 641 | // If it's not trivially captured and not trivially not captured, |
| 642 | // then it must be calling into another function in our SCC. Save |
| 643 | // its particulars for Argument-SCC analysis later. |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 644 | ArgumentGraphNode *Node = AG[&*A]; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 645 | for (Argument *Use : Tracker.Uses) { |
| 646 | Node->Uses.push_back(AG[Use]); |
| 647 | if (Use != &*A) |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 648 | HasNonLocalUses = true; |
| 649 | } |
Benjamin Kramer | 40d7f35 | 2013-06-22 16:56:32 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | // Otherwise, it's captured. Don't bother doing SCC analysis on it. |
| 653 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 654 | if (!HasNonLocalUses && !A->onlyReadsMemory()) { |
| 655 | // Can we determine that it's readonly/readnone without doing an SCC? |
| 656 | // Note that we don't allow any calls at all here, or else our result |
| 657 | // will be dependent on the iteration order through the functions in the |
| 658 | // SCC. |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 659 | SmallPtrSet<Argument *, 8> Self; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 660 | Self.insert(&*A); |
| 661 | Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 662 | if (R != Attribute::None) { |
| 663 | AttrBuilder B; |
| 664 | B.addAttribute(R); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 665 | A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 666 | Changed = true; |
| 667 | R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 668 | } |
| 669 | } |
| 670 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | // The graph we've collected is partial because we stopped scanning for |
| 674 | // argument uses once we solved the argument trivially. These partial nodes |
| 675 | // show up as ArgumentGraphNode objects with an empty Uses list, and for |
| 676 | // these nodes the final decision about whether they capture has already been |
| 677 | // made. If the definition doesn't have a 'nocapture' attribute by now, it |
| 678 | // captures. |
| 679 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 680 | 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] | 681 | const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 682 | if (ArgumentSCC.size() == 1) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 683 | if (!ArgumentSCC[0]->Definition) |
| 684 | continue; // synthetic root node |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 685 | |
| 686 | // eg. "void f(int* x) { if (...) f(x); }" |
| 687 | if (ArgumentSCC[0]->Uses.size() == 1 && |
| 688 | ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 689 | Argument *A = ArgumentSCC[0]->Definition; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 690 | A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 7e82055 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 691 | ++NumNoCapture; |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 692 | Changed = true; |
| 693 | } |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 694 | continue; |
| 695 | } |
| 696 | |
| 697 | bool SCCCaptured = false; |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 698 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 699 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 700 | ArgumentGraphNode *Node = *I; |
| 701 | if (Node->Uses.empty()) { |
| 702 | if (!Node->Definition->hasNoCaptureAttr()) |
| 703 | SCCCaptured = true; |
| 704 | } |
| 705 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 706 | if (SCCCaptured) |
| 707 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 708 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 709 | SmallPtrSet<Argument *, 8> ArgumentSCCNodes; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 710 | // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for |
| 711 | // quickly looking up whether a given Argument is in this ArgumentSCC. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 712 | for (ArgumentGraphNode *I : ArgumentSCC) { |
| 713 | ArgumentSCCNodes.insert(I->Definition); |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Duncan P. N. Exon Smith | d2b2fac | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 716 | for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
| 717 | I != E && !SCCCaptured; ++I) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 718 | ArgumentGraphNode *N = *I; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 719 | for (ArgumentGraphNode *Use : N->Uses) { |
| 720 | Argument *A = Use->Definition; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 721 | if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) |
| 722 | continue; |
| 723 | SCCCaptured = true; |
| 724 | break; |
| 725 | } |
| 726 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 727 | if (SCCCaptured) |
| 728 | continue; |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 729 | |
Nick Lewycky | f740db3 | 2012-01-05 22:21:45 +0000 | [diff] [blame] | 730 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 731 | Argument *A = ArgumentSCC[i]->Definition; |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 732 | A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 733 | ++NumNoCapture; |
| 734 | Changed = true; |
| 735 | } |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 736 | |
| 737 | // We also want to compute readonly/readnone. With a small number of false |
| 738 | // negatives, we can assume that any pointer which is captured isn't going |
| 739 | // to be provably readonly or readnone, since by definition we can't |
| 740 | // analyze all uses of a captured pointer. |
| 741 | // |
| 742 | // The false negatives happen when the pointer is captured by a function |
| 743 | // that promises readonly/readnone behaviour on the pointer, then the |
| 744 | // pointer's lifetime ends before anything that writes to arbitrary memory. |
| 745 | // Also, a readonly/readnone pointer may be returned, but returning a |
| 746 | // pointer is capturing it. |
| 747 | |
| 748 | Attribute::AttrKind ReadAttr = Attribute::ReadNone; |
| 749 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 750 | Argument *A = ArgumentSCC[i]->Definition; |
| 751 | Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); |
| 752 | if (K == Attribute::ReadNone) |
| 753 | continue; |
| 754 | if (K == Attribute::ReadOnly) { |
| 755 | ReadAttr = Attribute::ReadOnly; |
| 756 | continue; |
| 757 | } |
| 758 | ReadAttr = K; |
| 759 | break; |
| 760 | } |
| 761 | |
| 762 | if (ReadAttr != Attribute::None) { |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 763 | AttrBuilder B, R; |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 764 | B.addAttribute(ReadAttr); |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 765 | R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 766 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
| 767 | Argument *A = ArgumentSCC[i]->Definition; |
Bjorn Steinbrink | 236446c | 2015-05-25 19:46:38 +0000 | [diff] [blame] | 768 | // Clear out existing readonly/readnone attributes |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 769 | A->removeAttr( |
| 770 | AttributeList::get(A->getContext(), A->getArgNo() + 1, R)); |
| 771 | A->addAttr(AttributeList::get(A->getContext(), A->getArgNo() + 1, B)); |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 772 | ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
| 773 | Changed = true; |
| 774 | } |
| 775 | } |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | return Changed; |
| 779 | } |
| 780 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 781 | /// Tests whether a function is "malloc-like". |
| 782 | /// |
| 783 | /// A function is "malloc-like" if it returns either null or a pointer that |
| 784 | /// doesn't alias any other pointer visible to the caller. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 785 | static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 786 | SmallSetVector<Value *, 8> FlowsToReturn; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 787 | for (BasicBlock &BB : *F) |
| 788 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 789 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 790 | |
| 791 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
Benjamin Kramer | 1559127 | 2012-10-31 13:45:49 +0000 | [diff] [blame] | 792 | Value *RetVal = FlowsToReturn[i]; |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 793 | |
| 794 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 795 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 796 | return false; |
| 797 | |
| 798 | continue; |
| 799 | } |
| 800 | |
| 801 | if (isa<Argument>(RetVal)) |
| 802 | return false; |
| 803 | |
| 804 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 805 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 806 | // Extend the analysis by looking upwards. |
| 807 | case Instruction::BitCast: |
| 808 | case Instruction::GetElementPtr: |
| 809 | case Instruction::AddrSpaceCast: |
| 810 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 811 | continue; |
| 812 | case Instruction::Select: { |
| 813 | SelectInst *SI = cast<SelectInst>(RVI); |
| 814 | FlowsToReturn.insert(SI->getTrueValue()); |
| 815 | FlowsToReturn.insert(SI->getFalseValue()); |
| 816 | continue; |
| 817 | } |
| 818 | case Instruction::PHI: { |
| 819 | PHINode *PN = cast<PHINode>(RVI); |
| 820 | for (Value *IncValue : PN->incoming_values()) |
| 821 | FlowsToReturn.insert(IncValue); |
| 822 | continue; |
| 823 | } |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 824 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 825 | // Check whether the pointer came from an allocation. |
| 826 | case Instruction::Alloca: |
| 827 | break; |
| 828 | case Instruction::Call: |
| 829 | case Instruction::Invoke: { |
| 830 | CallSite CS(RVI); |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame^] | 831 | if (CS.hasRetAttr(Attribute::NoAlias)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 832 | break; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 833 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
| 834 | break; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 835 | LLVM_FALLTHROUGH; |
| 836 | } |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 837 | default: |
| 838 | return false; // Did not come from an allocation. |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 841 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
| 845 | return true; |
| 846 | } |
| 847 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 848 | /// Deduce noalias attributes for the SCC. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 849 | static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) { |
Nick Lewycky | 9ec96d1 | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 850 | // Check each function in turn, determining which functions return noalias |
| 851 | // pointers. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 852 | for (Function *F : SCCNodes) { |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 853 | // Already noalias. |
| 854 | if (F->doesNotAlias(0)) |
| 855 | continue; |
| 856 | |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 857 | // We can infer and propagate function attributes only when we know that the |
| 858 | // definition we'll get at link time is *exactly* the definition we see now. |
| 859 | // For more details, see GlobalValue::mayBeDerefined. |
| 860 | if (!F->hasExactDefinition()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 861 | return false; |
| 862 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 863 | // We annotate noalias return values, which are only applicable to |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 864 | // pointer types. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 865 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 866 | continue; |
| 867 | |
Chandler Carruth | 3824f85 | 2015-09-13 08:23:27 +0000 | [diff] [blame] | 868 | if (!isFunctionMallocLike(F, SCCNodes)) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 869 | return false; |
| 870 | } |
| 871 | |
| 872 | bool MadeChange = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 873 | for (Function *F : SCCNodes) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 874 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | fbed86a | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 875 | continue; |
| 876 | |
| 877 | F->setDoesNotAlias(0); |
| 878 | ++NumNoAlias; |
| 879 | MadeChange = true; |
| 880 | } |
| 881 | |
| 882 | return MadeChange; |
| 883 | } |
| 884 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 885 | /// Tests whether this function is known to not return null. |
Chandler Carruth | 8874b78 | 2015-09-13 08:17:14 +0000 | [diff] [blame] | 886 | /// |
| 887 | /// Requires that the function returns a pointer. |
| 888 | /// |
| 889 | /// Returns true if it believes the function will not return a null, and sets |
| 890 | /// \p Speculative based on whether the returned conclusion is a speculative |
| 891 | /// conclusion due to SCC calls. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 892 | static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 893 | bool &Speculative) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 894 | assert(F->getReturnType()->isPointerTy() && |
| 895 | "nonnull only meaningful on pointer types"); |
| 896 | Speculative = false; |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 897 | |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 898 | SmallSetVector<Value *, 8> FlowsToReturn; |
| 899 | for (BasicBlock &BB : *F) |
| 900 | if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) |
| 901 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 902 | |
| 903 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 904 | Value *RetVal = FlowsToReturn[i]; |
| 905 | |
| 906 | // If this value is locally known to be non-null, we're good |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 907 | if (isKnownNonNull(RetVal)) |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 908 | continue; |
| 909 | |
| 910 | // 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] | 911 | // conclusions. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 912 | Instruction *RVI = dyn_cast<Instruction>(RetVal); |
| 913 | if (!RVI) |
| 914 | return false; |
| 915 | switch (RVI->getOpcode()) { |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 916 | // Extend the analysis by looking upwards. |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 917 | case Instruction::BitCast: |
| 918 | case Instruction::GetElementPtr: |
| 919 | case Instruction::AddrSpaceCast: |
| 920 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 921 | continue; |
| 922 | case Instruction::Select: { |
| 923 | SelectInst *SI = cast<SelectInst>(RVI); |
| 924 | FlowsToReturn.insert(SI->getTrueValue()); |
| 925 | FlowsToReturn.insert(SI->getFalseValue()); |
| 926 | continue; |
| 927 | } |
| 928 | case Instruction::PHI: { |
| 929 | PHINode *PN = cast<PHINode>(RVI); |
| 930 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 931 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
| 932 | continue; |
| 933 | } |
| 934 | case Instruction::Call: |
| 935 | case Instruction::Invoke: { |
| 936 | CallSite CS(RVI); |
| 937 | Function *Callee = CS.getCalledFunction(); |
| 938 | // A call to a node within the SCC is assumed to return null until |
| 939 | // proven otherwise |
| 940 | if (Callee && SCCNodes.count(Callee)) { |
| 941 | Speculative = true; |
| 942 | continue; |
| 943 | } |
| 944 | return false; |
| 945 | } |
| 946 | default: |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 947 | return false; // Unknown source, may be null |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 948 | }; |
| 949 | llvm_unreachable("should have either continued or returned"); |
| 950 | } |
| 951 | |
| 952 | return true; |
| 953 | } |
| 954 | |
Chandler Carruth | a632fb9 | 2015-09-13 06:57:25 +0000 | [diff] [blame] | 955 | /// Deduce nonnull attributes for the SCC. |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 956 | static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 957 | // Speculative that all functions in the SCC return only nonnull |
| 958 | // pointers. We may refute this as we analyze functions. |
| 959 | bool SCCReturnsNonNull = true; |
| 960 | |
| 961 | bool MadeChange = false; |
| 962 | |
| 963 | // Check each function in turn, determining which functions return nonnull |
| 964 | // pointers. |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 965 | for (Function *F : SCCNodes) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 966 | // Already nonnull. |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 967 | if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex, |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 968 | Attribute::NonNull)) |
| 969 | continue; |
| 970 | |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 971 | // We can infer and propagate function attributes only when we know that the |
| 972 | // definition we'll get at link time is *exactly* the definition we see now. |
| 973 | // For more details, see GlobalValue::mayBeDerefined. |
| 974 | if (!F->hasExactDefinition()) |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 975 | return false; |
| 976 | |
Chandler Carruth | 63559d7 | 2015-09-13 06:47:20 +0000 | [diff] [blame] | 977 | // We annotate nonnull return values, which are only applicable to |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 978 | // pointer types. |
| 979 | if (!F->getReturnType()->isPointerTy()) |
| 980 | continue; |
| 981 | |
| 982 | bool Speculative = false; |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 983 | if (isReturnNonNull(F, SCCNodes, Speculative)) { |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 984 | if (!Speculative) { |
| 985 | // Mark the function eagerly since we may discover a function |
| 986 | // which prevents us from speculating about the entire SCC |
| 987 | DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 988 | F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 989 | ++NumNonNullReturn; |
| 990 | MadeChange = true; |
| 991 | } |
| 992 | continue; |
| 993 | } |
| 994 | // At least one function returns something which could be null, can't |
| 995 | // speculate any more. |
| 996 | SCCReturnsNonNull = false; |
| 997 | } |
| 998 | |
| 999 | if (SCCReturnsNonNull) { |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1000 | for (Function *F : SCCNodes) { |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1001 | if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex, |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 1002 | Attribute::NonNull) || |
| 1003 | !F->getReturnType()->isPointerTy()) |
| 1004 | continue; |
| 1005 | |
| 1006 | DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); |
Reid Kleckner | b518054 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 1007 | F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull); |
Philip Reames | a88caea | 2015-08-31 19:44:38 +0000 | [diff] [blame] | 1008 | ++NumNonNullReturn; |
| 1009 | MadeChange = true; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | return MadeChange; |
| 1014 | } |
| 1015 | |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 1016 | /// Remove the convergent attribute from all functions in the SCC if every |
| 1017 | /// callsite within the SCC is not convergent (except for calls to functions |
| 1018 | /// within the SCC). Returns true if changes were made. |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 1019 | static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) { |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 1020 | // For every function in SCC, ensure that either |
| 1021 | // * it is not convergent, or |
| 1022 | // * we can remove its convergent attribute. |
| 1023 | bool HasConvergentFn = false; |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 1024 | for (Function *F : SCCNodes) { |
Justin Lebar | 9d94397 | 2016-03-14 20:18:54 +0000 | [diff] [blame] | 1025 | if (!F->isConvergent()) continue; |
| 1026 | HasConvergentFn = true; |
| 1027 | |
| 1028 | // Can't remove convergent from function declarations. |
| 1029 | if (F->isDeclaration()) return false; |
| 1030 | |
| 1031 | // Can't remove convergent if any of our functions has a convergent call to a |
| 1032 | // function not in the SCC. |
| 1033 | for (Instruction &I : instructions(*F)) { |
| 1034 | CallSite CS(&I); |
| 1035 | // Bail if CS is a convergent call to a function not in the SCC. |
| 1036 | if (CS && CS.isConvergent() && |
| 1037 | SCCNodes.count(CS.getCalledFunction()) == 0) |
| 1038 | return false; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | // If the SCC doesn't have any convergent functions, we have nothing to do. |
| 1043 | if (!HasConvergentFn) return false; |
| 1044 | |
| 1045 | // If we got here, all of the calls the SCC makes to functions not in the SCC |
| 1046 | // are non-convergent. Therefore all of the SCC's functions can also be made |
| 1047 | // non-convergent. We'll remove the attr from the callsites in |
| 1048 | // InstCombineCalls. |
| 1049 | for (Function *F : SCCNodes) { |
| 1050 | if (!F->isConvergent()) continue; |
| 1051 | |
| 1052 | DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName() |
| 1053 | << "\n"); |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 1054 | F->setNotConvergent(); |
| 1055 | } |
Justin Lebar | 260854b | 2016-02-09 23:03:22 +0000 | [diff] [blame] | 1056 | return true; |
| 1057 | } |
| 1058 | |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1059 | static bool setDoesNotRecurse(Function &F) { |
| 1060 | if (F.doesNotRecurse()) |
| 1061 | return false; |
| 1062 | F.setDoesNotRecurse(); |
| 1063 | ++NumNoRecurse; |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1067 | static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) { |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1068 | // Try and identify functions that do not recurse. |
| 1069 | |
| 1070 | // If the SCC contains multiple nodes we know for sure there is recursion. |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1071 | if (SCCNodes.size() != 1) |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1072 | return false; |
| 1073 | |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1074 | Function *F = *SCCNodes.begin(); |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1075 | if (!F || F->isDeclaration() || F->doesNotRecurse()) |
| 1076 | return false; |
| 1077 | |
| 1078 | // If all of the calls in F are identifiable and are to norecurse functions, F |
| 1079 | // is norecurse. This check also detects self-recursion as F is not currently |
| 1080 | // marked norecurse, so any called from F to F will not be marked norecurse. |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1081 | for (Instruction &I : instructions(*F)) |
| 1082 | if (auto CS = CallSite(&I)) { |
| 1083 | Function *Callee = CS.getCalledFunction(); |
| 1084 | if (!Callee || Callee == F || !Callee->doesNotRecurse()) |
| 1085 | // Function calls a potentially recursive function. |
| 1086 | return false; |
| 1087 | } |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1088 | |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1089 | // Every call was to a non-recursive function other than this function, and |
| 1090 | // we have no indirect recursion as the SCC size is one. This function cannot |
| 1091 | // recurse. |
| 1092 | return setDoesNotRecurse(*F); |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1095 | PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C, |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 1096 | CGSCCAnalysisManager &AM, |
| 1097 | LazyCallGraph &CG, |
| 1098 | CGSCCUpdateResult &) { |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1099 | FunctionAnalysisManager &FAM = |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 1100 | AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager(); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1101 | |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1102 | // We pass a lambda into functions to wire them up to the analysis manager |
| 1103 | // for getting function analyses. |
| 1104 | auto AARGetter = [&](Function &F) -> AAResults & { |
| 1105 | return FAM.getResult<AAManager>(F); |
| 1106 | }; |
| 1107 | |
| 1108 | // Fill SCCNodes with the elements of the SCC. Also track whether there are |
| 1109 | // any external or opt-none nodes that will prevent us from optimizing any |
| 1110 | // part of the SCC. |
| 1111 | SCCNodeSet SCCNodes; |
| 1112 | bool HasUnknownCall = false; |
| 1113 | for (LazyCallGraph::Node &N : C) { |
| 1114 | Function &F = N.getFunction(); |
| 1115 | if (F.hasFnAttribute(Attribute::OptimizeNone)) { |
| 1116 | // Treat any function we're trying not to optimize as if it were an |
| 1117 | // indirect call and omit it from the node set used below. |
| 1118 | HasUnknownCall = true; |
| 1119 | continue; |
| 1120 | } |
| 1121 | // Track whether any functions in this SCC have an unknown call edge. |
| 1122 | // Note: if this is ever a performance hit, we can common it with |
| 1123 | // subsequent routines which also do scans over the instructions of the |
| 1124 | // function. |
| 1125 | if (!HasUnknownCall) |
| 1126 | for (Instruction &I : instructions(F)) |
| 1127 | if (auto CS = CallSite(&I)) |
| 1128 | if (!CS.getCalledFunction()) { |
| 1129 | HasUnknownCall = true; |
| 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | SCCNodes.insert(&F); |
| 1134 | } |
| 1135 | |
| 1136 | bool Changed = false; |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 1137 | Changed |= addArgumentReturnedAttrs(SCCNodes); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1138 | Changed |= addReadAttrs(SCCNodes, AARGetter); |
| 1139 | Changed |= addArgumentAttrs(SCCNodes); |
| 1140 | |
| 1141 | // If we have no external nodes participating in the SCC, we can deduce some |
| 1142 | // more precise attributes as well. |
| 1143 | if (!HasUnknownCall) { |
| 1144 | Changed |= addNoAliasAttrs(SCCNodes); |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 1145 | Changed |= addNonNullAttrs(SCCNodes); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1146 | Changed |= removeConvergentAttrs(SCCNodes); |
| 1147 | Changed |= addNoRecurseAttrs(SCCNodes); |
| 1148 | } |
| 1149 | |
| 1150 | return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); |
| 1151 | } |
| 1152 | |
| 1153 | namespace { |
| 1154 | struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass { |
| 1155 | static char ID; // Pass identification, replacement for typeid |
| 1156 | PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) { |
Chad Rosier | 611b73b | 2016-11-07 16:28:04 +0000 | [diff] [blame] | 1157 | initializePostOrderFunctionAttrsLegacyPassPass( |
| 1158 | *PassRegistry::getPassRegistry()); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | bool runOnSCC(CallGraphSCC &SCC) override; |
| 1162 | |
| 1163 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1164 | AU.setPreservesCFG(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1165 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 12884f7 | 2016-03-02 15:56:53 +0000 | [diff] [blame] | 1166 | getAAResultsAnalysisUsage(AU); |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1167 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 1168 | } |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | } |
| 1171 | |
| 1172 | char PostOrderFunctionAttrsLegacyPass::ID = 0; |
| 1173 | INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs", |
| 1174 | "Deduce function attributes", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1175 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1176 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1177 | INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs", |
| 1178 | "Deduce function attributes", false, false) |
| 1179 | |
Chad Rosier | 611b73b | 2016-11-07 16:28:04 +0000 | [diff] [blame] | 1180 | Pass *llvm::createPostOrderFunctionAttrsLegacyPass() { |
| 1181 | return new PostOrderFunctionAttrsLegacyPass(); |
| 1182 | } |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 1183 | |
Sean Silva | 997cbea | 2016-07-03 03:35:03 +0000 | [diff] [blame] | 1184 | template <typename AARGetterT> |
| 1185 | static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) { |
Chandler Carruth | cada2d8 | 2015-10-31 00:28:37 +0000 | [diff] [blame] | 1186 | bool Changed = false; |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1187 | |
| 1188 | // Fill SCCNodes with the elements of the SCC. Used for quickly looking up |
| 1189 | // whether a given CallGraphNode is in this SCC. Also track whether there are |
| 1190 | // any external or opt-none nodes that will prevent us from optimizing any |
| 1191 | // part of the SCC. |
| 1192 | SCCNodeSet SCCNodes; |
| 1193 | bool ExternalNode = false; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1194 | for (CallGraphNode *I : SCC) { |
| 1195 | Function *F = I->getFunction(); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1196 | if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) { |
| 1197 | // External node or function we're trying not to optimize - we both avoid |
| 1198 | // transform them and avoid leveraging information they provide. |
| 1199 | ExternalNode = true; |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | SCCNodes.insert(F); |
| 1204 | } |
| 1205 | |
David Majnemer | 5246e0b | 2016-07-19 18:50:26 +0000 | [diff] [blame] | 1206 | Changed |= addArgumentReturnedAttrs(SCCNodes); |
Chandler Carruth | a812535 | 2015-10-30 16:48:08 +0000 | [diff] [blame] | 1207 | Changed |= addReadAttrs(SCCNodes, AARGetter); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1208 | Changed |= addArgumentAttrs(SCCNodes); |
| 1209 | |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 1210 | // If we have no external nodes participating in the SCC, we can deduce some |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1211 | // more precise attributes as well. |
| 1212 | if (!ExternalNode) { |
| 1213 | Changed |= addNoAliasAttrs(SCCNodes); |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 1214 | Changed |= addNonNullAttrs(SCCNodes); |
Chandler Carruth | 3937bc7 | 2016-02-12 09:47:49 +0000 | [diff] [blame] | 1215 | Changed |= removeConvergentAttrs(SCCNodes); |
Chandler Carruth | 632d208 | 2016-02-13 08:47:51 +0000 | [diff] [blame] | 1216 | Changed |= addNoRecurseAttrs(SCCNodes); |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1217 | } |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1218 | |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1219 | return Changed; |
| 1220 | } |
Chandler Carruth | c518ebd | 2015-10-29 18:29:15 +0000 | [diff] [blame] | 1221 | |
Sean Silva | 997cbea | 2016-07-03 03:35:03 +0000 | [diff] [blame] | 1222 | bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) { |
| 1223 | if (skipSCC(SCC)) |
| 1224 | return false; |
Peter Collingbourne | cea1e4e | 2017-02-09 23:11:52 +0000 | [diff] [blame] | 1225 | return runImpl(SCC, LegacyAARGetter(*this)); |
Sean Silva | 997cbea | 2016-07-03 03:35:03 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1228 | namespace { |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1229 | struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass { |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1230 | static char ID; // Pass identification, replacement for typeid |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1231 | ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) { |
Chad Rosier | 611b73b | 2016-11-07 16:28:04 +0000 | [diff] [blame] | 1232 | initializeReversePostOrderFunctionAttrsLegacyPassPass( |
| 1233 | *PassRegistry::getPassRegistry()); |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | bool runOnModule(Module &M) override; |
| 1237 | |
| 1238 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1239 | AU.setPreservesCFG(); |
| 1240 | AU.addRequired<CallGraphWrapperPass>(); |
Mehdi Amini | 0ddf404 | 2016-05-02 18:03:33 +0000 | [diff] [blame] | 1241 | AU.addPreserved<CallGraphWrapperPass>(); |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1242 | } |
| 1243 | }; |
| 1244 | } |
| 1245 | |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1246 | char ReversePostOrderFunctionAttrsLegacyPass::ID = 0; |
| 1247 | INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1248 | "Deduce function attributes in RPO", false, false) |
| 1249 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1250 | INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1251 | "Deduce function attributes in RPO", false, false) |
| 1252 | |
| 1253 | Pass *llvm::createReversePostOrderFunctionAttrsPass() { |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1254 | return new ReversePostOrderFunctionAttrsLegacyPass(); |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | static bool addNoRecurseAttrsTopDown(Function &F) { |
| 1258 | // We check the preconditions for the function prior to calling this to avoid |
| 1259 | // the cost of building up a reversible post-order list. We assert them here |
| 1260 | // to make sure none of the invariants this relies on were violated. |
| 1261 | assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!"); |
| 1262 | assert(!F.doesNotRecurse() && |
| 1263 | "This function has already been deduced as norecurs!"); |
| 1264 | assert(F.hasInternalLinkage() && |
| 1265 | "Can only do top-down deduction for internal linkage functions!"); |
| 1266 | |
| 1267 | // If F is internal and all of its uses are calls from a non-recursive |
| 1268 | // functions, then none of its calls could in fact recurse without going |
| 1269 | // through a function marked norecurse, and so we can mark this function too |
| 1270 | // as norecurse. Note that the uses must actually be calls -- otherwise |
| 1271 | // a pointer to this function could be returned from a norecurse function but |
| 1272 | // this function could be recursively (indirectly) called. Note that this |
| 1273 | // also detects if F is directly recursive as F is not yet marked as |
| 1274 | // a norecurse function. |
| 1275 | for (auto *U : F.users()) { |
| 1276 | auto *I = dyn_cast<Instruction>(U); |
| 1277 | if (!I) |
| 1278 | return false; |
| 1279 | CallSite CS(I); |
| 1280 | if (!CS || !CS.getParent()->getParent()->doesNotRecurse()) |
| 1281 | return false; |
| 1282 | } |
| 1283 | return setDoesNotRecurse(F); |
| 1284 | } |
| 1285 | |
Sean Silva | adc7939 | 2016-06-12 05:44:51 +0000 | [diff] [blame] | 1286 | static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) { |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1287 | // We only have a post-order SCC traversal (because SCCs are inherently |
| 1288 | // discovered in post-order), so we accumulate them in a vector and then walk |
| 1289 | // it in reverse. This is simpler than using the RPO iterator infrastructure |
| 1290 | // because we need to combine SCC detection and the PO walk of the call |
| 1291 | // graph. We can also cheat egregiously because we're primarily interested in |
| 1292 | // synthesizing norecurse and so we can only save the singular SCCs as SCCs |
| 1293 | // with multiple functions in them will clearly be recursive. |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1294 | SmallVector<Function *, 16> Worklist; |
| 1295 | for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { |
| 1296 | if (I->size() != 1) |
| 1297 | continue; |
| 1298 | |
| 1299 | Function *F = I->front()->getFunction(); |
| 1300 | if (F && !F->isDeclaration() && !F->doesNotRecurse() && |
| 1301 | F->hasInternalLinkage()) |
| 1302 | Worklist.push_back(F); |
| 1303 | } |
| 1304 | |
James Molloy | 7e9bdd5 | 2015-11-12 10:55:20 +0000 | [diff] [blame] | 1305 | bool Changed = false; |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 1306 | for (auto *F : reverse(Worklist)) |
| 1307 | Changed |= addNoRecurseAttrsTopDown(*F); |
| 1308 | |
Duncan Sands | 44c8cd9 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 1309 | return Changed; |
| 1310 | } |
Sean Silva | adc7939 | 2016-06-12 05:44:51 +0000 | [diff] [blame] | 1311 | |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1312 | bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) { |
Sean Silva | adc7939 | 2016-06-12 05:44:51 +0000 | [diff] [blame] | 1313 | if (skipModule(M)) |
| 1314 | return false; |
| 1315 | |
| 1316 | auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
| 1317 | |
| 1318 | return deduceFunctionAttributeInRPO(M, CG); |
| 1319 | } |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1320 | |
| 1321 | PreservedAnalyses |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 1322 | ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) { |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1323 | auto &CG = AM.getResult<CallGraphAnalysis>(M); |
| 1324 | |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 1325 | if (!deduceFunctionAttributeInRPO(M, CG)) |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1326 | return PreservedAnalyses::all(); |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 1327 | |
Sean Silva | f508019 | 2016-06-12 07:48:51 +0000 | [diff] [blame] | 1328 | PreservedAnalyses PA; |
| 1329 | PA.preserve<CallGraphAnalysis>(); |
| 1330 | return PA; |
| 1331 | } |