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