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