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