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