comparison lib/Transforms/IPO/FunctionAttrs.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children 3a76565eade5
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 //
10 /// \file 10 /// \file
11 /// This file implements interprocedural passes which walk the 11 /// This file implements interprocedural passes which walk the
12 /// call-graph deducing and/or propagating function attributes. 12 /// call-graph deducing and/or propagating function attributes.
13 /// 13 //
14 //===----------------------------------------------------------------------===// 14 //===----------------------------------------------------------------------===//
15 15
16 #include "llvm/Transforms/IPO/FunctionAttrs.h" 16 #include "llvm/Transforms/IPO/FunctionAttrs.h"
17 #include "llvm/Transforms/IPO.h"
18 #include "llvm/ADT/SCCIterator.h" 17 #include "llvm/ADT/SCCIterator.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h" 23 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Analysis/AliasAnalysis.h" 24 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/AssumptionCache.h" 25 #include "llvm/Analysis/AssumptionCache.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h" 26 #include "llvm/Analysis/BasicAliasAnalysis.h"
27 #include "llvm/Analysis/CGSCCPassManager.h"
26 #include "llvm/Analysis/CallGraph.h" 28 #include "llvm/Analysis/CallGraph.h"
27 #include "llvm/Analysis/CallGraphSCCPass.h" 29 #include "llvm/Analysis/CallGraphSCCPass.h"
28 #include "llvm/Analysis/CaptureTracking.h" 30 #include "llvm/Analysis/CaptureTracking.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h" 31 #include "llvm/Analysis/LazyCallGraph.h"
32 #include "llvm/Analysis/MemoryLocation.h"
30 #include "llvm/Analysis/ValueTracking.h" 33 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/GlobalVariable.h" 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"
32 #include "llvm/IR/InstIterator.h" 41 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h" 42 #include "llvm/IR/InstrTypes.h"
34 #include "llvm/IR/LLVMContext.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"
35 #include "llvm/Support/Debug.h" 55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h" 57 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h" 58 #include "llvm/Transforms/IPO.h"
59 #include <cassert>
60 #include <iterator>
61 #include <map>
62 #include <vector>
63
38 using namespace llvm; 64 using namespace llvm;
39 65
40 #define DEBUG_TYPE "functionattrs" 66 #define DEBUG_TYPE "functionattrs"
41 67
42 STATISTIC(NumReadNone, "Number of functions marked readnone"); 68 STATISTIC(NumReadNone, "Number of functions marked readnone");
47 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); 73 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
48 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); 74 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
49 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); 75 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
50 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse"); 76 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
51 77
78 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
79 // in C/C++ code compiled by clang:
80 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
81 static cl::opt<bool> EnableNonnullArgPropagation(
82 "enable-nonnull-arg-prop", cl::Hidden,
83 cl::desc("Try to propagate nonnull argument attributes from callsites to "
84 "caller functions."));
85
52 namespace { 86 namespace {
53 typedef SmallSetVector<Function *, 8> SCCNodeSet; 87
54 } 88 using SCCNodeSet = SmallSetVector<Function *, 8>;
55 89
56 namespace { 90 } // end anonymous namespace
57 /// The three kinds of memory access relevant to 'readonly' and 91
58 /// 'readnone' attributes. 92 /// Returns the memory access attribute for function F using AAR for AA results,
59 enum MemoryAccessKind { 93 /// where SCCNodes is the current SCC.
60 MAK_ReadNone = 0, 94 ///
61 MAK_ReadOnly = 1, 95 /// If ThisBody is true, this function may examine the function body and will
62 MAK_MayWrite = 2 96 /// return a result pertaining to this copy of the function. If it is false, the
63 }; 97 /// result will be based only on AA results for the function declaration; it
64 } 98 /// will be assumed that some other (perhaps less optimized) version of the
65 99 /// function may be selected at link time.
66 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR, 100 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
101 AAResults &AAR,
67 const SCCNodeSet &SCCNodes) { 102 const SCCNodeSet &SCCNodes) {
68 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); 103 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
69 if (MRB == FMRB_DoesNotAccessMemory) 104 if (MRB == FMRB_DoesNotAccessMemory)
70 // Already perfect! 105 // Already perfect!
71 return MAK_ReadNone; 106 return MAK_ReadNone;
72 107
73 // Non-exact function definitions may not be selected at link time, and an 108 if (!ThisBody) {
74 // alternative version that writes to memory may be selected. See the comment
75 // on GlobalValue::isDefinitionExact for more details.
76 if (!F.hasExactDefinition()) {
77 if (AliasAnalysis::onlyReadsMemory(MRB)) 109 if (AliasAnalysis::onlyReadsMemory(MRB))
78 return MAK_ReadOnly; 110 return MAK_ReadOnly;
79 111
80 // Conservatively assume it writes to memory. 112 // Conservatively assume it writes to memory.
81 return MAK_MayWrite; 113 return MAK_MayWrite;
170 } 202 }
171 203
172 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; 204 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
173 } 205 }
174 206
207 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
208 AAResults &AAR) {
209 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
210 }
211
175 /// Deduce readonly/readnone attributes for the SCC. 212 /// Deduce readonly/readnone attributes for the SCC.
176 template <typename AARGetterT> 213 template <typename AARGetterT>
177 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) { 214 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
178 // Check if any of the functions in the SCC read or write memory. If they 215 // 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. 216 // write memory then they can't be marked readnone or readonly.
180 bool ReadsMemory = false; 217 bool ReadsMemory = false;
181 for (Function *F : SCCNodes) { 218 for (Function *F : SCCNodes) {
182 // Call the callable parameter to look up AA results for this function. 219 // Call the callable parameter to look up AA results for this function.
183 AAResults &AAR = AARGetter(*F); 220 AAResults &AAR = AARGetter(*F);
184 221
185 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) { 222 // Non-exact function definitions may not be selected at link time, and an
223 // alternative version that writes to memory may be selected. See the
224 // comment on GlobalValue::isDefinitionExact for more details.
225 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
226 AAR, SCCNodes)) {
186 case MAK_MayWrite: 227 case MAK_MayWrite:
187 return false; 228 return false;
188 case MAK_ReadOnly: 229 case MAK_ReadOnly:
189 ReadsMemory = true; 230 ReadsMemory = true;
190 break; 231 break;
207 continue; 248 continue;
208 249
209 MadeChange = true; 250 MadeChange = true;
210 251
211 // Clear out any existing attributes. 252 // Clear out any existing attributes.
212 AttrBuilder B; 253 F->removeFnAttr(Attribute::ReadOnly);
213 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); 254 F->removeFnAttr(Attribute::ReadNone);
214 F->removeAttributes(
215 AttributeSet::FunctionIndex,
216 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
217 255
218 // Add in the new attribute. 256 // Add in the new attribute.
219 F->addAttribute(AttributeSet::FunctionIndex, 257 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
220 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
221 258
222 if (ReadsMemory) 259 if (ReadsMemory)
223 ++NumReadOnly; 260 ++NumReadOnly;
224 else 261 else
225 ++NumReadNone; 262 ++NumReadNone;
227 264
228 return MadeChange; 265 return MadeChange;
229 } 266 }
230 267
231 namespace { 268 namespace {
269
232 /// For a given pointer Argument, this retains a list of Arguments of functions 270 /// 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 271 /// in the same SCC that the pointer data flows into. We use this to build an
234 /// SCC of the arguments. 272 /// SCC of the arguments.
235 struct ArgumentGraphNode { 273 struct ArgumentGraphNode {
236 Argument *Definition; 274 Argument *Definition;
238 }; 276 };
239 277
240 class ArgumentGraph { 278 class ArgumentGraph {
241 // We store pointers to ArgumentGraphNode objects, so it's important that 279 // We store pointers to ArgumentGraphNode objects, so it's important that
242 // that they not move around upon insert. 280 // that they not move around upon insert.
243 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; 281 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
244 282
245 ArgumentMapTy ArgumentMap; 283 ArgumentMapTy ArgumentMap;
246 284
247 // There is no root node for the argument graph, in fact: 285 // There is no root node for the argument graph, in fact:
248 // void f(int *x, int *y) { if (...) f(x, y); } 286 // void f(int *x, int *y) { if (...) f(x, y); }
253 ArgumentGraphNode SyntheticRoot; 291 ArgumentGraphNode SyntheticRoot;
254 292
255 public: 293 public:
256 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } 294 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
257 295
258 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; 296 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
259 297
260 iterator begin() { return SyntheticRoot.Uses.begin(); } 298 iterator begin() { return SyntheticRoot.Uses.begin(); }
261 iterator end() { return SyntheticRoot.Uses.end(); } 299 iterator end() { return SyntheticRoot.Uses.end(); }
262 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } 300 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
263 301
271 309
272 /// This tracker checks whether callees are in the SCC, and if so it does not 310 /// 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 311 /// consider that a capture, instead adding it to the "Uses" list and
274 /// continuing with the analysis. 312 /// continuing with the analysis.
275 struct ArgumentUsesTracker : public CaptureTracker { 313 struct ArgumentUsesTracker : public CaptureTracker {
276 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) 314 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
277 : Captured(false), SCCNodes(SCCNodes) {}
278 315
279 void tooManyUses() override { Captured = true; } 316 void tooManyUses() override { Captured = true; }
280 317
281 bool captured(const Use *U) override { 318 bool captured(const Use *U) override {
282 CallSite CS(U->getUser()); 319 CallSite CS(U->getUser());
321 358
322 Uses.push_back(&*std::next(F->arg_begin(), UseIndex)); 359 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
323 return false; 360 return false;
324 } 361 }
325 362
326 bool Captured; // True only if certainly captured (used outside our SCC). 363 // True only if certainly captured (used outside our SCC).
327 SmallVector<Argument *, 4> Uses; // Uses within our SCC. 364 bool Captured = false;
365
366 // Uses within our SCC.
367 SmallVector<Argument *, 4> Uses;
328 368
329 const SCCNodeSet &SCCNodes; 369 const SCCNodeSet &SCCNodes;
330 }; 370 };
331 } 371
372 } // end anonymous namespace
332 373
333 namespace llvm { 374 namespace llvm {
375
334 template <> struct GraphTraits<ArgumentGraphNode *> { 376 template <> struct GraphTraits<ArgumentGraphNode *> {
335 typedef ArgumentGraphNode *NodeRef; 377 using NodeRef = ArgumentGraphNode *;
336 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; 378 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
337 379
338 static NodeRef getEntryNode(NodeRef A) { return A; } 380 static NodeRef getEntryNode(NodeRef A) { return A; }
339 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); } 381 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
340 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); } 382 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
341 }; 383 };
384
342 template <> 385 template <>
343 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { 386 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
344 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); } 387 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
388
345 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { 389 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
346 return AG->begin(); 390 return AG->begin();
347 } 391 }
392
348 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } 393 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
349 }; 394 };
350 } 395
396 } // end namespace llvm
351 397
352 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. 398 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
353 static Attribute::AttrKind 399 static Attribute::AttrKind
354 determinePointerReadAttrs(Argument *A, 400 determinePointerReadAttrs(Argument *A,
355 const SmallPtrSet<Argument *, 8> &SCCNodes) { 401 const SmallPtrSet<Argument *, 8> &SCCNodes) {
356
357 SmallVector<Use *, 32> Worklist; 402 SmallVector<Use *, 32> Worklist;
358 SmallSet<Use *, 32> Visited; 403 SmallSet<Use *, 32> Visited;
359 404
360 // inalloca arguments are always clobbered by the call. 405 // inalloca arguments are always clobbered by the call.
361 if (A->hasInAllocaAttr()) 406 if (A->hasInAllocaAttr())
480 525
481 /// Deduce returned attributes for the SCC. 526 /// Deduce returned attributes for the SCC.
482 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) { 527 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
483 bool Changed = false; 528 bool Changed = false;
484 529
485 AttrBuilder B;
486 B.addAttribute(Attribute::Returned);
487
488 // Check each function in turn, determining if an argument is always returned. 530 // Check each function in turn, determining if an argument is always returned.
489 for (Function *F : SCCNodes) { 531 for (Function *F : SCCNodes) {
490 // We can infer and propagate function attributes only when we know that the 532 // 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. 533 // definition we'll get at link time is *exactly* the definition we see now.
492 // For more details, see GlobalValue::mayBeDerefined. 534 // For more details, see GlobalValue::mayBeDerefined.
495 537
496 if (F->getReturnType()->isVoidTy()) 538 if (F->getReturnType()->isVoidTy())
497 continue; 539 continue;
498 540
499 // There is nothing to do if an argument is already marked as 'returned'. 541 // There is nothing to do if an argument is already marked as 'returned'.
500 if (any_of(F->args(), 542 if (llvm::any_of(F->args(),
501 [](const Argument &Arg) { return Arg.hasReturnedAttr(); })) 543 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
502 continue; 544 continue;
503 545
504 auto FindRetArg = [&]() -> Value * { 546 auto FindRetArg = [&]() -> Value * {
505 Value *RetArg = nullptr; 547 Value *RetArg = nullptr;
506 for (BasicBlock &BB : *F) 548 for (BasicBlock &BB : *F)
520 return RetArg; 562 return RetArg;
521 }; 563 };
522 564
523 if (Value *RetArg = FindRetArg()) { 565 if (Value *RetArg = FindRetArg()) {
524 auto *A = cast<Argument>(RetArg); 566 auto *A = cast<Argument>(RetArg);
525 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); 567 A->addAttr(Attribute::Returned);
526 ++NumReturned; 568 ++NumReturned;
527 Changed = true; 569 Changed = true;
528 } 570 }
529 } 571 }
530 572
573 return Changed;
574 }
575
576 /// If a callsite has arguments that are also arguments to the parent function,
577 /// try to propagate attributes from the callsite's arguments to the parent's
578 /// arguments. This may be important because inlining can cause information loss
579 /// when attribute knowledge disappears with the inlined call.
580 static bool addArgumentAttrsFromCallsites(Function &F) {
581 if (!EnableNonnullArgPropagation)
582 return false;
583
584 bool Changed = false;
585
586 // For an argument attribute to transfer from a callsite to the parent, the
587 // call must be guaranteed to execute every time the parent is called.
588 // Conservatively, just check for calls in the entry block that are guaranteed
589 // to execute.
590 // TODO: This could be enhanced by testing if the callsite post-dominates the
591 // entry block or by doing simple forward walks or backward walks to the
592 // callsite.
593 BasicBlock &Entry = F.getEntryBlock();
594 for (Instruction &I : Entry) {
595 if (auto CS = CallSite(&I)) {
596 if (auto *CalledFunc = CS.getCalledFunction()) {
597 for (auto &CSArg : CalledFunc->args()) {
598 if (!CSArg.hasNonNullAttr())
599 continue;
600
601 // If the non-null callsite argument operand is an argument to 'F'
602 // (the caller) and the call is guaranteed to execute, then the value
603 // must be non-null throughout 'F'.
604 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
605 if (FArg && !FArg->hasNonNullAttr()) {
606 FArg->addAttr(Attribute::NonNull);
607 Changed = true;
608 }
609 }
610 }
611 }
612 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
613 break;
614 }
615
531 return Changed; 616 return Changed;
532 } 617 }
533 618
534 /// Deduce nocapture attributes for the SCC. 619 /// Deduce nocapture attributes for the SCC.
535 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) { 620 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
536 bool Changed = false; 621 bool Changed = false;
537 622
538 ArgumentGraph AG; 623 ArgumentGraph AG;
539
540 AttrBuilder B;
541 B.addAttribute(Attribute::NoCapture);
542 624
543 // Check each function in turn, determining which pointer arguments are not 625 // Check each function in turn, determining which pointer arguments are not
544 // captured. 626 // captured.
545 for (Function *F : SCCNodes) { 627 for (Function *F : SCCNodes) {
546 // We can infer and propagate function attributes only when we know that the 628 // 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. 629 // definition we'll get at link time is *exactly* the definition we see now.
548 // For more details, see GlobalValue::mayBeDerefined. 630 // For more details, see GlobalValue::mayBeDerefined.
549 if (!F->hasExactDefinition()) 631 if (!F->hasExactDefinition())
550 continue; 632 continue;
551 633
634 Changed |= addArgumentAttrsFromCallsites(*F);
635
552 // Functions that are readonly (or readnone) and nounwind and don't return 636 // Functions that are readonly (or readnone) and nounwind and don't return
553 // a value can't capture arguments. Don't analyze them. 637 // a value can't capture arguments. Don't analyze them.
554 if (F->onlyReadsMemory() && F->doesNotThrow() && 638 if (F->onlyReadsMemory() && F->doesNotThrow() &&
555 F->getReturnType()->isVoidTy()) { 639 F->getReturnType()->isVoidTy()) {
556 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; 640 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
557 ++A) { 641 ++A) {
558 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 642 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
559 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); 643 A->addAttr(Attribute::NoCapture);
560 ++NumNoCapture; 644 ++NumNoCapture;
561 Changed = true; 645 Changed = true;
562 } 646 }
563 } 647 }
564 continue; 648 continue;
573 ArgumentUsesTracker Tracker(SCCNodes); 657 ArgumentUsesTracker Tracker(SCCNodes);
574 PointerMayBeCaptured(&*A, &Tracker); 658 PointerMayBeCaptured(&*A, &Tracker);
575 if (!Tracker.Captured) { 659 if (!Tracker.Captured) {
576 if (Tracker.Uses.empty()) { 660 if (Tracker.Uses.empty()) {
577 // If it's trivially not captured, mark it nocapture now. 661 // If it's trivially not captured, mark it nocapture now.
578 A->addAttr( 662 A->addAttr(Attribute::NoCapture);
579 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
580 ++NumNoCapture; 663 ++NumNoCapture;
581 Changed = true; 664 Changed = true;
582 } else { 665 } else {
583 // If it's not trivially captured and not trivially not captured, 666 // If it's not trivially captured and not trivially not captured,
584 // then it must be calling into another function in our SCC. Save 667 // then it must be calling into another function in our SCC. Save
600 // SCC. 683 // SCC.
601 SmallPtrSet<Argument *, 8> Self; 684 SmallPtrSet<Argument *, 8> Self;
602 Self.insert(&*A); 685 Self.insert(&*A);
603 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); 686 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
604 if (R != Attribute::None) { 687 if (R != Attribute::None) {
605 AttrBuilder B; 688 A->addAttr(R);
606 B.addAttribute(R);
607 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
608 Changed = true; 689 Changed = true;
609 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 690 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
610 } 691 }
611 } 692 }
612 } 693 }
627 708
628 // eg. "void f(int* x) { if (...) f(x); }" 709 // eg. "void f(int* x) { if (...) f(x); }"
629 if (ArgumentSCC[0]->Uses.size() == 1 && 710 if (ArgumentSCC[0]->Uses.size() == 1 &&
630 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { 711 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
631 Argument *A = ArgumentSCC[0]->Definition; 712 Argument *A = ArgumentSCC[0]->Definition;
632 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 713 A->addAttr(Attribute::NoCapture);
633 ++NumNoCapture; 714 ++NumNoCapture;
634 Changed = true; 715 Changed = true;
635 } 716 }
636 continue; 717 continue;
637 } 718 }
669 if (SCCCaptured) 750 if (SCCCaptured)
670 continue; 751 continue;
671 752
672 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 753 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
673 Argument *A = ArgumentSCC[i]->Definition; 754 Argument *A = ArgumentSCC[i]->Definition;
674 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 755 A->addAttr(Attribute::NoCapture);
675 ++NumNoCapture; 756 ++NumNoCapture;
676 Changed = true; 757 Changed = true;
677 } 758 }
678 759
679 // We also want to compute readonly/readnone. With a small number of false 760 // We also want to compute readonly/readnone. With a small number of false
700 ReadAttr = K; 781 ReadAttr = K;
701 break; 782 break;
702 } 783 }
703 784
704 if (ReadAttr != Attribute::None) { 785 if (ReadAttr != Attribute::None) {
705 AttrBuilder B, R;
706 B.addAttribute(ReadAttr);
707 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
708 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 786 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
709 Argument *A = ArgumentSCC[i]->Definition; 787 Argument *A = ArgumentSCC[i]->Definition;
710 // Clear out existing readonly/readnone attributes 788 // Clear out existing readonly/readnone attributes
711 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R)); 789 A->removeAttr(Attribute::ReadOnly);
712 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 790 A->removeAttr(Attribute::ReadNone);
791 A->addAttr(ReadAttr);
713 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 792 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
714 Changed = true; 793 Changed = true;
715 } 794 }
716 } 795 }
717 } 796 }
767 case Instruction::Alloca: 846 case Instruction::Alloca:
768 break; 847 break;
769 case Instruction::Call: 848 case Instruction::Call:
770 case Instruction::Invoke: { 849 case Instruction::Invoke: {
771 CallSite CS(RVI); 850 CallSite CS(RVI);
772 if (CS.paramHasAttr(0, Attribute::NoAlias)) 851 if (CS.hasRetAttr(Attribute::NoAlias))
773 break; 852 break;
774 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) 853 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
775 break; 854 break;
776 LLVM_FALLTHROUGH; 855 LLVM_FALLTHROUGH;
777 } 856 }
790 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) { 869 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
791 // Check each function in turn, determining which functions return noalias 870 // Check each function in turn, determining which functions return noalias
792 // pointers. 871 // pointers.
793 for (Function *F : SCCNodes) { 872 for (Function *F : SCCNodes) {
794 // Already noalias. 873 // Already noalias.
795 if (F->doesNotAlias(0)) 874 if (F->returnDoesNotAlias())
796 continue; 875 continue;
797 876
798 // We can infer and propagate function attributes only when we know that the 877 // 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. 878 // definition we'll get at link time is *exactly* the definition we see now.
800 // For more details, see GlobalValue::mayBeDerefined. 879 // For more details, see GlobalValue::mayBeDerefined.
810 return false; 889 return false;
811 } 890 }
812 891
813 bool MadeChange = false; 892 bool MadeChange = false;
814 for (Function *F : SCCNodes) { 893 for (Function *F : SCCNodes) {
815 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) 894 if (F->returnDoesNotAlias() ||
816 continue; 895 !F->getReturnType()->isPointerTy())
817 896 continue;
818 F->setDoesNotAlias(0); 897
898 F->setReturnDoesNotAlias();
819 ++NumNoAlias; 899 ++NumNoAlias;
820 MadeChange = true; 900 MadeChange = true;
821 } 901 }
822 902
823 return MadeChange; 903 return MadeChange;
839 SmallSetVector<Value *, 8> FlowsToReturn; 919 SmallSetVector<Value *, 8> FlowsToReturn;
840 for (BasicBlock &BB : *F) 920 for (BasicBlock &BB : *F)
841 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) 921 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
842 FlowsToReturn.insert(Ret->getReturnValue()); 922 FlowsToReturn.insert(Ret->getReturnValue());
843 923
924 auto &DL = F->getParent()->getDataLayout();
925
844 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 926 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
845 Value *RetVal = FlowsToReturn[i]; 927 Value *RetVal = FlowsToReturn[i];
846 928
847 // If this value is locally known to be non-null, we're good 929 // If this value is locally known to be non-null, we're good
848 if (isKnownNonNull(RetVal)) 930 if (isKnownNonZero(RetVal, DL))
849 continue; 931 continue;
850 932
851 // Otherwise, we need to look upwards since we can't make any local 933 // Otherwise, we need to look upwards since we can't make any local
852 // conclusions. 934 // conclusions.
853 Instruction *RVI = dyn_cast<Instruction>(RetVal); 935 Instruction *RVI = dyn_cast<Instruction>(RetVal);
903 985
904 // Check each function in turn, determining which functions return nonnull 986 // Check each function in turn, determining which functions return nonnull
905 // pointers. 987 // pointers.
906 for (Function *F : SCCNodes) { 988 for (Function *F : SCCNodes) {
907 // Already nonnull. 989 // Already nonnull.
908 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, 990 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
909 Attribute::NonNull)) 991 Attribute::NonNull))
910 continue; 992 continue;
911 993
912 // We can infer and propagate function attributes only when we know that the 994 // 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. 995 // definition we'll get at link time is *exactly* the definition we see now.
924 if (isReturnNonNull(F, SCCNodes, Speculative)) { 1006 if (isReturnNonNull(F, SCCNodes, Speculative)) {
925 if (!Speculative) { 1007 if (!Speculative) {
926 // Mark the function eagerly since we may discover a function 1008 // Mark the function eagerly since we may discover a function
927 // which prevents us from speculating about the entire SCC 1009 // which prevents us from speculating about the entire SCC
928 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); 1010 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
929 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); 1011 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
930 ++NumNonNullReturn; 1012 ++NumNonNullReturn;
931 MadeChange = true; 1013 MadeChange = true;
932 } 1014 }
933 continue; 1015 continue;
934 } 1016 }
937 SCCReturnsNonNull = false; 1019 SCCReturnsNonNull = false;
938 } 1020 }
939 1021
940 if (SCCReturnsNonNull) { 1022 if (SCCReturnsNonNull) {
941 for (Function *F : SCCNodes) { 1023 for (Function *F : SCCNodes) {
942 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, 1024 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
943 Attribute::NonNull) || 1025 Attribute::NonNull) ||
944 !F->getReturnType()->isPointerTy()) 1026 !F->getReturnType()->isPointerTy())
945 continue; 1027 continue;
946 1028
947 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); 1029 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
948 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); 1030 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
949 ++NumNonNullReturn; 1031 ++NumNonNullReturn;
950 MadeChange = true; 1032 MadeChange = true;
951 } 1033 }
952 } 1034 }
953 1035
1090 1172
1091 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all(); 1173 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1092 } 1174 }
1093 1175
1094 namespace { 1176 namespace {
1177
1095 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass { 1178 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1096 static char ID; // Pass identification, replacement for typeid 1179 // Pass identification, replacement for typeid
1180 static char ID;
1181
1097 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) { 1182 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1098 initializePostOrderFunctionAttrsLegacyPassPass( 1183 initializePostOrderFunctionAttrsLegacyPassPass(
1099 *PassRegistry::getPassRegistry()); 1184 *PassRegistry::getPassRegistry());
1100 } 1185 }
1101 1186
1106 AU.addRequired<AssumptionCacheTracker>(); 1191 AU.addRequired<AssumptionCacheTracker>();
1107 getAAResultsAnalysisUsage(AU); 1192 getAAResultsAnalysisUsage(AU);
1108 CallGraphSCCPass::getAnalysisUsage(AU); 1193 CallGraphSCCPass::getAnalysisUsage(AU);
1109 } 1194 }
1110 }; 1195 };
1111 } 1196
1197 } // end anonymous namespace
1112 1198
1113 char PostOrderFunctionAttrsLegacyPass::ID = 0; 1199 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1114 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs", 1200 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1115 "Deduce function attributes", false, false) 1201 "Deduce function attributes", false, false)
1116 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 1202 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1142 } 1228 }
1143 1229
1144 SCCNodes.insert(F); 1230 SCCNodes.insert(F);
1145 } 1231 }
1146 1232
1233 // Skip it if the SCC only contains optnone functions.
1234 if (SCCNodes.empty())
1235 return Changed;
1236
1147 Changed |= addArgumentReturnedAttrs(SCCNodes); 1237 Changed |= addArgumentReturnedAttrs(SCCNodes);
1148 Changed |= addReadAttrs(SCCNodes, AARGetter); 1238 Changed |= addReadAttrs(SCCNodes, AARGetter);
1149 Changed |= addArgumentAttrs(SCCNodes); 1239 Changed |= addArgumentAttrs(SCCNodes);
1150 1240
1151 // If we have no external nodes participating in the SCC, we can deduce some 1241 // If we have no external nodes participating in the SCC, we can deduce some
1161 } 1251 }
1162 1252
1163 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) { 1253 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1164 if (skipSCC(SCC)) 1254 if (skipSCC(SCC))
1165 return false; 1255 return false;
1166 1256 return runImpl(SCC, LegacyAARGetter(*this));
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 } 1257 }
1180 1258
1181 namespace { 1259 namespace {
1260
1182 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass { 1261 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
1183 static char ID; // Pass identification, replacement for typeid 1262 // Pass identification, replacement for typeid
1263 static char ID;
1264
1184 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) { 1265 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1185 initializeReversePostOrderFunctionAttrsLegacyPassPass( 1266 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1186 *PassRegistry::getPassRegistry()); 1267 *PassRegistry::getPassRegistry());
1187 } 1268 }
1188 1269
1192 AU.setPreservesCFG(); 1273 AU.setPreservesCFG();
1193 AU.addRequired<CallGraphWrapperPass>(); 1274 AU.addRequired<CallGraphWrapperPass>();
1194 AU.addPreserved<CallGraphWrapperPass>(); 1275 AU.addPreserved<CallGraphWrapperPass>();
1195 } 1276 }
1196 }; 1277 };
1197 } 1278
1279 } // end anonymous namespace
1198 1280
1199 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0; 1281 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1282
1200 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", 1283 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1201 "Deduce function attributes in RPO", false, false) 1284 "Deduce function attributes in RPO", false, false)
1202 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 1285 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1203 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs", 1286 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
1204 "Deduce function attributes in RPO", false, false) 1287 "Deduce function attributes in RPO", false, false)
1254 F->hasInternalLinkage()) 1337 F->hasInternalLinkage())
1255 Worklist.push_back(F); 1338 Worklist.push_back(F);
1256 } 1339 }
1257 1340
1258 bool Changed = false; 1341 bool Changed = false;
1259 for (auto *F : reverse(Worklist)) 1342 for (auto *F : llvm::reverse(Worklist))
1260 Changed |= addNoRecurseAttrsTopDown(*F); 1343 Changed |= addNoRecurseAttrsTopDown(*F);
1261 1344
1262 return Changed; 1345 return Changed;
1263 } 1346 }
1264 1347
1273 1356
1274 PreservedAnalyses 1357 PreservedAnalyses
1275 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) { 1358 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1276 auto &CG = AM.getResult<CallGraphAnalysis>(M); 1359 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1277 1360
1278 bool Changed = deduceFunctionAttributeInRPO(M, CG); 1361 if (!deduceFunctionAttributeInRPO(M, CG))
1279
1280 // CallGraphAnalysis holds AssertingVH and must be invalidated eagerly so
1281 // that other passes don't delete stuff from under it.
1282 // FIXME: We need to invalidate this to avoid PR28400. Is there a better
1283 // solution?
1284 AM.invalidate<CallGraphAnalysis>(M);
1285
1286 if (!Changed)
1287 return PreservedAnalyses::all(); 1362 return PreservedAnalyses::all();
1363
1288 PreservedAnalyses PA; 1364 PreservedAnalyses PA;
1289 PA.preserve<CallGraphAnalysis>(); 1365 PA.preserve<CallGraphAnalysis>();
1290 return PA; 1366 return PA;
1291 } 1367 }