1 //===- Attributor.cpp - Module-wide attribute deduction -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements an inter procedural pass that deduces and/or propagating
10 // attributes. This is done in an abstract interpretation style fixpoint
11 // iteration. See the Attributor.h file comment and the class descriptions in
12 // that file for more information.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO/Attributor.h"
17 
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/CaptureTracking.h"
25 #include "llvm/Analysis/EHPersonalities.h"
26 #include "llvm/Analysis/GlobalsModRef.h"
27 #include "llvm/Analysis/Loads.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/InstIterator.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
38 #include "llvm/Transforms/Utils/Local.h"
39 
40 #include <cassert>
41 
42 using namespace llvm;
43 
44 #define DEBUG_TYPE "attributor"
45 
46 STATISTIC(NumFnWithExactDefinition,
47           "Number of function with exact definitions");
48 STATISTIC(NumFnWithoutExactDefinition,
49           "Number of function without exact definitions");
50 STATISTIC(NumAttributesTimedOut,
51           "Number of abstract attributes timed out before fixpoint");
52 STATISTIC(NumAttributesValidFixpoint,
53           "Number of abstract attributes in a valid fixpoint state");
54 STATISTIC(NumAttributesManifested,
55           "Number of abstract attributes manifested in IR");
56 STATISTIC(NumFnNoUnwind, "Number of functions marked nounwind");
57 
58 STATISTIC(NumFnUniqueReturned, "Number of function with unique return");
59 STATISTIC(NumFnKnownReturns, "Number of function with known return values");
60 STATISTIC(NumFnArgumentReturned,
61           "Number of function arguments marked returned");
62 STATISTIC(NumFnNoSync, "Number of functions marked nosync");
63 STATISTIC(NumFnNoFree, "Number of functions marked nofree");
64 STATISTIC(NumFnReturnedNonNull,
65           "Number of function return values marked nonnull");
66 STATISTIC(NumFnArgumentNonNull, "Number of function arguments marked nonnull");
67 STATISTIC(NumCSArgumentNonNull, "Number of call site arguments marked nonnull");
68 STATISTIC(NumFnWillReturn, "Number of functions marked willreturn");
69 STATISTIC(NumFnArgumentNoAlias, "Number of function arguments marked noalias");
70 STATISTIC(NumFnReturnedDereferenceable,
71           "Number of function return values marked dereferenceable");
72 STATISTIC(NumFnArgumentDereferenceable,
73           "Number of function arguments marked dereferenceable");
74 STATISTIC(NumCSArgumentDereferenceable,
75           "Number of call site arguments marked dereferenceable");
76 STATISTIC(NumFnReturnedAlign, "Number of function return values marked align");
77 STATISTIC(NumFnArgumentAlign, "Number of function arguments marked align");
78 STATISTIC(NumCSArgumentAlign, "Number of call site arguments marked align");
79 STATISTIC(NumFnNoReturn, "Number of functions marked noreturn");
80 
81 // TODO: Determine a good default value.
82 //
83 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
84 // (when run with the first 5 abstract attributes). The results also indicate
85 // that we never reach 32 iterations but always find a fixpoint sooner.
86 //
87 // This will become more evolved once we perform two interleaved fixpoint
88 // iterations: bottom-up and top-down.
89 static cl::opt<unsigned>
90     MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
91                           cl::desc("Maximal number of fixpoint iterations."),
92                           cl::init(32));
93 
94 static cl::opt<bool> DisableAttributor(
95     "attributor-disable", cl::Hidden,
96     cl::desc("Disable the attributor inter-procedural deduction pass."),
97     cl::init(true));
98 
99 static cl::opt<bool> VerifyAttributor(
100     "attributor-verify", cl::Hidden,
101     cl::desc("Verify the Attributor deduction and "
102              "manifestation of attributes -- may issue false-positive errors"),
103     cl::init(false));
104 
105 /// Logic operators for the change status enum class.
106 ///
107 ///{
108 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
109   return l == ChangeStatus::CHANGED ? l : r;
110 }
111 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
112   return l == ChangeStatus::UNCHANGED ? l : r;
113 }
114 ///}
115 
116 /// Helper to adjust the statistics.
117 static void bookkeeping(IRPosition::Kind PK, const Attribute &Attr) {
118   if (!AreStatisticsEnabled())
119     return;
120 
121   switch (Attr.getKindAsEnum()) {
122   case Attribute::Alignment:
123     switch (PK) {
124     case IRPosition::IRP_RETURNED:
125       NumFnReturnedAlign++;
126       break;
127     case IRPosition::IRP_ARGUMENT:
128       NumFnArgumentAlign++;
129       break;
130     case IRPosition::IRP_CALL_SITE_ARGUMENT:
131       NumCSArgumentAlign++;
132       break;
133     default:
134       break;
135     }
136     break;
137   case Attribute::Dereferenceable:
138     switch (PK) {
139     case IRPosition::IRP_RETURNED:
140       NumFnReturnedDereferenceable++;
141       break;
142     case IRPosition::IRP_ARGUMENT:
143       NumFnArgumentDereferenceable++;
144       break;
145     case IRPosition::IRP_CALL_SITE_ARGUMENT:
146       NumCSArgumentDereferenceable++;
147       break;
148     default:
149       break;
150     }
151     break;
152   case Attribute::NoUnwind:
153     NumFnNoUnwind++;
154     return;
155   case Attribute::Returned:
156     NumFnArgumentReturned++;
157     return;
158   case Attribute::NoSync:
159     NumFnNoSync++;
160     break;
161   case Attribute::NoFree:
162     NumFnNoFree++;
163     break;
164   case Attribute::NonNull:
165     switch (PK) {
166     case IRPosition::IRP_RETURNED:
167       NumFnReturnedNonNull++;
168       break;
169     case IRPosition::IRP_ARGUMENT:
170       NumFnArgumentNonNull++;
171       break;
172     case IRPosition::IRP_CALL_SITE_ARGUMENT:
173       NumCSArgumentNonNull++;
174       break;
175     default:
176       break;
177     }
178     break;
179   case Attribute::WillReturn:
180     NumFnWillReturn++;
181     break;
182   case Attribute::NoReturn:
183     NumFnNoReturn++;
184     return;
185   case Attribute::NoAlias:
186     NumFnArgumentNoAlias++;
187     return;
188   default:
189     return;
190   }
191 }
192 
193 template <typename StateTy>
194 using followValueCB_t = std::function<bool(Value *, StateTy &State)>;
195 template <typename StateTy>
196 using visitValueCB_t = std::function<void(Value *, StateTy &State)>;
197 
198 /// Recursively visit all values that might become \p InitV at some point. This
199 /// will be done by looking through cast instructions, selects, phis, and calls
200 /// with the "returned" attribute. The callback \p FollowValueCB is asked before
201 /// a potential origin value is looked at. If no \p FollowValueCB is passed, a
202 /// default one is used that will make sure we visit every value only once. Once
203 /// we cannot look through the value any further, the callback \p VisitValueCB
204 /// is invoked and passed the current value and the \p State. To limit how much
205 /// effort is invested, we will never visit more than \p MaxValues values.
206 template <typename StateTy>
207 static bool genericValueTraversal(
208     Value *InitV, StateTy &State, visitValueCB_t<StateTy> &VisitValueCB,
209     followValueCB_t<StateTy> *FollowValueCB = nullptr, int MaxValues = 8) {
210 
211   SmallPtrSet<Value *, 16> Visited;
212   followValueCB_t<bool> DefaultFollowValueCB = [&](Value *Val, bool &) {
213     return Visited.insert(Val).second;
214   };
215 
216   if (!FollowValueCB)
217     FollowValueCB = &DefaultFollowValueCB;
218 
219   SmallVector<Value *, 16> Worklist;
220   Worklist.push_back(InitV);
221 
222   int Iteration = 0;
223   do {
224     Value *V = Worklist.pop_back_val();
225 
226     // Check if we should process the current value. To prevent endless
227     // recursion keep a record of the values we followed!
228     if (!(*FollowValueCB)(V, State))
229       continue;
230 
231     // Make sure we limit the compile time for complex expressions.
232     if (Iteration++ >= MaxValues)
233       return false;
234 
235     // Explicitly look through calls with a "returned" attribute if we do
236     // not have a pointer as stripPointerCasts only works on them.
237     if (V->getType()->isPointerTy()) {
238       V = V->stripPointerCasts();
239     } else {
240       CallSite CS(V);
241       if (CS && CS.getCalledFunction()) {
242         Value *NewV = nullptr;
243         for (Argument &Arg : CS.getCalledFunction()->args())
244           if (Arg.hasReturnedAttr()) {
245             NewV = CS.getArgOperand(Arg.getArgNo());
246             break;
247           }
248         if (NewV) {
249           Worklist.push_back(NewV);
250           continue;
251         }
252       }
253     }
254 
255     // Look through select instructions, visit both potential values.
256     if (auto *SI = dyn_cast<SelectInst>(V)) {
257       Worklist.push_back(SI->getTrueValue());
258       Worklist.push_back(SI->getFalseValue());
259       continue;
260     }
261 
262     // Look through phi nodes, visit all operands.
263     if (auto *PHI = dyn_cast<PHINode>(V)) {
264       Worklist.append(PHI->op_begin(), PHI->op_end());
265       continue;
266     }
267 
268     // Once a leaf is reached we inform the user through the callback.
269     VisitValueCB(V, State);
270   } while (!Worklist.empty());
271 
272   // All values have been visited.
273   return true;
274 }
275 
276 /// Return true if \p New is equal or worse than \p Old.
277 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
278   if (!Old.isIntAttribute())
279     return true;
280 
281   return Old.getValueAsInt() >= New.getValueAsInt();
282 }
283 
284 /// Return true if the information provided by \p Attr was added to the
285 /// attribute list \p Attrs. This is only the case if it was not already present
286 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
287 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
288                              AttributeList &Attrs, int AttrIdx) {
289 
290   if (Attr.isEnumAttribute()) {
291     Attribute::AttrKind Kind = Attr.getKindAsEnum();
292     if (Attrs.hasAttribute(AttrIdx, Kind))
293       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
294         return false;
295     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
296     return true;
297   }
298   if (Attr.isStringAttribute()) {
299     StringRef Kind = Attr.getKindAsString();
300     if (Attrs.hasAttribute(AttrIdx, Kind))
301       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
302         return false;
303     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
304     return true;
305   }
306   if (Attr.isIntAttribute()) {
307     Attribute::AttrKind Kind = Attr.getKindAsEnum();
308     if (Attrs.hasAttribute(AttrIdx, Kind))
309       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
310         return false;
311     Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
312     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
313     return true;
314   }
315 
316   llvm_unreachable("Expected enum or string attribute!");
317 }
318 
319 ChangeStatus AbstractAttribute::update(Attributor &A,
320                                        InformationCache &InfoCache) {
321   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
322   if (getState().isAtFixpoint())
323     return HasChanged;
324 
325   LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
326 
327   HasChanged = updateImpl(A, InfoCache);
328 
329   LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
330                     << "\n");
331 
332   return HasChanged;
333 }
334 
335 template <Attribute::AttrKind AK, typename Base>
336 ChangeStatus IRAttribute<AK, Base>::manifest(Attributor &A) {
337   assert(this->getState().isValidState() &&
338          "Attempted to manifest an invalid state!");
339   assert(getIRPosition().getAssociatedValue() &&
340          "Attempted to manifest an attribute without associated value!");
341 
342   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
343 
344   Function &ScopeFn = getAnchorScope();
345   LLVMContext &Ctx = ScopeFn.getContext();
346   IRPosition::Kind PK = getPositionKind();
347 
348   SmallVector<Attribute, 4> DeducedAttrs;
349   getDeducedAttributes(Ctx, DeducedAttrs);
350 
351   // In the following some generic code that will manifest attributes in
352   // DeducedAttrs if they improve the current IR. Due to the different
353   // annotation positions we use the underlying AttributeList interface.
354 
355   AttributeList Attrs;
356   switch (PK) {
357   case IRPosition::IRP_ARGUMENT:
358   case IRPosition::IRP_FUNCTION:
359   case IRPosition::IRP_RETURNED:
360     Attrs = ScopeFn.getAttributes();
361     break;
362   case IRPosition::IRP_CALL_SITE_ARGUMENT:
363     Attrs = ImmutableCallSite(&getAnchorValue()).getAttributes();
364     break;
365   }
366 
367   for (const Attribute &Attr : DeducedAttrs) {
368     if (!addIfNotExistent(Ctx, Attr, Attrs, getAttrIdx()))
369       continue;
370 
371     HasChanged = ChangeStatus::CHANGED;
372     bookkeeping(PK, Attr);
373   }
374 
375   if (HasChanged == ChangeStatus::UNCHANGED)
376     return HasChanged;
377 
378   switch (PK) {
379   case IRPosition::IRP_ARGUMENT:
380   case IRPosition::IRP_FUNCTION:
381   case IRPosition::IRP_RETURNED:
382     ScopeFn.setAttributes(Attrs);
383     break;
384   case IRPosition::IRP_CALL_SITE_ARGUMENT:
385     CallSite(&getAnchorValue()).setAttributes(Attrs);
386   }
387 
388   return HasChanged;
389 }
390 
391 /// -----------------------NoUnwind Function Attribute--------------------------
392 
393 struct AANoUnwindImpl : AANoUnwind, BooleanState {
394   IRPositionConstructorForward(AANoUnwindImpl, AANoUnwind);
395 
396   /// See AbstractAttribute::getState()
397   /// {
398   AbstractState &getState() override { return *this; }
399   const AbstractState &getState() const override { return *this; }
400   /// }
401 
402   const std::string getAsStr() const override {
403     return getAssumed() ? "nounwind" : "may-unwind";
404   }
405 
406   /// See AbstractAttribute::updateImpl(...).
407   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
408 
409   /// See AANoUnwind::isAssumedNoUnwind().
410   bool isAssumedNoUnwind() const override { return getAssumed(); }
411 
412   /// See AANoUnwind::isKnownNoUnwind().
413   bool isKnownNoUnwind() const override { return getKnown(); }
414 };
415 
416 struct AANoUnwindFunction final : public AANoUnwindImpl {
417   AANoUnwindFunction(Function &F) : AANoUnwindImpl(F, IRP_FUNCTION) {}
418 };
419 
420 ChangeStatus AANoUnwindImpl::updateImpl(Attributor &A,
421                                         InformationCache &InfoCache) {
422   Function &F = getAnchorScope();
423 
424   // The map from instruction opcodes to those instructions in the function.
425   auto Opcodes = {
426       (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
427       (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
428       (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
429 
430   auto CheckForNoUnwind = [&](Instruction &I) {
431     if (!I.mayThrow())
432       return true;
433 
434     auto *NoUnwindAA = A.getAAFor<AANoUnwind>(*this, I);
435     return NoUnwindAA && NoUnwindAA->isAssumedNoUnwind();
436   };
437 
438   if (!A.checkForAllInstructions(F, CheckForNoUnwind, *this, InfoCache,
439                                  Opcodes))
440     return indicatePessimisticFixpoint();
441 
442   return ChangeStatus::UNCHANGED;
443 }
444 
445 /// --------------------- Function Return Values -------------------------------
446 
447 /// "Attribute" that collects all potential returned values and the return
448 /// instructions that they arise from.
449 ///
450 /// If there is a unique returned value R, the manifest method will:
451 ///   - mark R with the "returned" attribute, if R is an argument.
452 class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
453 
454   /// Mapping of values potentially returned by the associated function to the
455   /// return instructions that might return them.
456   DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> ReturnedValues;
457 
458   /// State flags
459   ///
460   ///{
461   bool IsFixed;
462   bool IsValidState;
463   bool HasOverdefinedReturnedCalls;
464   ///}
465 
466   /// Collect values that could become \p V in the set \p Values, each mapped to
467   /// \p ReturnInsts.
468   void collectValuesRecursively(
469       Attributor &A, Value *V, SmallPtrSetImpl<ReturnInst *> &ReturnInsts,
470       DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> &Values) {
471 
472     visitValueCB_t<bool> VisitValueCB = [&](Value *Val, bool &) {
473       assert(!isa<Instruction>(Val) ||
474              &getAnchorScope() == cast<Instruction>(Val)->getFunction());
475       Values[Val].insert(ReturnInsts.begin(), ReturnInsts.end());
476     };
477 
478     bool UnusedBool;
479     bool Success = genericValueTraversal(V, UnusedBool, VisitValueCB);
480 
481     // If we did abort the above traversal we haven't see all the values.
482     // Consequently, we cannot know if the information we would derive is
483     // accurate so we give up early.
484     if (!Success)
485       indicatePessimisticFixpoint();
486   }
487 
488 public:
489   IRPositionConstructorForward(AAReturnedValuesImpl, AAReturnedValues);
490 
491   /// See AbstractAttribute::initialize(...).
492   void initialize(Attributor &A, InformationCache &InfoCache) override {
493     // Reset the state.
494     AssociatedVal = nullptr;
495     IsFixed = false;
496     IsValidState = true;
497     HasOverdefinedReturnedCalls = false;
498     ReturnedValues.clear();
499 
500     Function &F = cast<Function>(getAnchorValue());
501 
502     // The map from instruction opcodes to those instructions in the function.
503     auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
504 
505     // Look through all arguments, if one is marked as returned we are done.
506     for (Argument &Arg : F.args()) {
507       if (Arg.hasReturnedAttr()) {
508 
509         auto &ReturnInstSet = ReturnedValues[&Arg];
510         for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
511           ReturnInstSet.insert(cast<ReturnInst>(RI));
512 
513         indicateOptimisticFixpoint();
514         return;
515       }
516     }
517 
518     // If no argument was marked as returned we look at all return instructions
519     // and collect potentially returned values.
520     for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) {
521       SmallPtrSet<ReturnInst *, 1> RISet({cast<ReturnInst>(RI)});
522       collectValuesRecursively(A, cast<ReturnInst>(RI)->getReturnValue(), RISet,
523                                ReturnedValues);
524     }
525   }
526 
527   /// See AbstractAttribute::manifest(...).
528   ChangeStatus manifest(Attributor &A) override;
529 
530   /// See AbstractAttribute::getState(...).
531   AbstractState &getState() override { return *this; }
532 
533   /// See AbstractAttribute::getState(...).
534   const AbstractState &getState() const override { return *this; }
535 
536   /// See AbstractAttribute::updateImpl(Attributor &A).
537   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
538 
539   /// Return the number of potential return values, -1 if unknown.
540   size_t getNumReturnValues() const {
541     return isValidState() ? ReturnedValues.size() : -1;
542   }
543 
544   /// Return an assumed unique return value if a single candidate is found. If
545   /// there cannot be one, return a nullptr. If it is not clear yet, return the
546   /// Optional::NoneType.
547   Optional<Value *>
548   getAssumedUniqueReturnValue(const AAIsDead *LivenessAA) const;
549 
550   /// See AbstractState::checkForallReturnedValues(...).
551   bool checkForallReturnedValues(
552       std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> &Pred)
553       const override;
554 
555   /// Pretty print the attribute similar to the IR representation.
556   const std::string getAsStr() const override;
557 
558   /// See AbstractState::isAtFixpoint().
559   bool isAtFixpoint() const override { return IsFixed; }
560 
561   /// See AbstractState::isValidState().
562   bool isValidState() const override { return IsValidState; }
563 
564   /// See AbstractState::indicateOptimisticFixpoint(...).
565   ChangeStatus indicateOptimisticFixpoint() override {
566     IsFixed = true;
567     IsValidState &= true;
568     return ChangeStatus::UNCHANGED;
569   }
570 
571   ChangeStatus indicatePessimisticFixpoint() override {
572     IsFixed = true;
573     IsValidState = false;
574     return ChangeStatus::CHANGED;
575   }
576 };
577 
578 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
579   AAReturnedValuesFunction(Function &F)
580       : AAReturnedValuesImpl(F, IRP_FUNCTION) {}
581 };
582 
583 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
584   ChangeStatus Changed = ChangeStatus::UNCHANGED;
585 
586   // Bookkeeping.
587   assert(isValidState());
588   NumFnKnownReturns++;
589 
590   auto *LivenessAA = A.getAAFor<AAIsDead>(*this, getAnchorScope());
591 
592   // Check if we have an assumed unique return value that we could manifest.
593   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(LivenessAA);
594 
595   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
596     return Changed;
597 
598   // Bookkeeping.
599   NumFnUniqueReturned++;
600 
601   // If the assumed unique return value is an argument, annotate it.
602   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
603     setAssociatedValue(UniqueRVArg);
604     setAttributeIdx(UniqueRVArg->getArgNo() + AttributeList::FirstArgIndex);
605     Changed = IRAttribute::manifest(A) | Changed;
606   }
607 
608   return Changed;
609 }
610 
611 const std::string AAReturnedValuesImpl::getAsStr() const {
612   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
613          (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
614          ")[OD: " + std::to_string(HasOverdefinedReturnedCalls) + "]";
615 }
616 
617 Optional<Value *> AAReturnedValuesImpl::getAssumedUniqueReturnValue(
618     const AAIsDead *LivenessAA) const {
619   // If checkForallReturnedValues provides a unique value, ignoring potential
620   // undef values that can also be present, it is assumed to be the actual
621   // return value and forwarded to the caller of this method. If there are
622   // multiple, a nullptr is returned indicating there cannot be a unique
623   // returned value.
624   Optional<Value *> UniqueRV;
625 
626   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
627       [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool {
628     // If all ReturnInsts are dead, then ReturnValue is dead as well
629     // and can be ignored.
630     if (LivenessAA &&
631         !LivenessAA->isLiveInstSet(RetInsts.begin(), RetInsts.end()))
632       return true;
633 
634     // If we found a second returned value and neither the current nor the saved
635     // one is an undef, there is no unique returned value. Undefs are special
636     // since we can pretend they have any value.
637     if (UniqueRV.hasValue() && UniqueRV != &RV &&
638         !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
639       UniqueRV = nullptr;
640       return false;
641     }
642 
643     // Do not overwrite a value with an undef.
644     if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
645       UniqueRV = &RV;
646 
647     return true;
648   };
649 
650   if (!checkForallReturnedValues(Pred))
651     UniqueRV = nullptr;
652 
653   return UniqueRV;
654 }
655 
656 bool AAReturnedValuesImpl::checkForallReturnedValues(
657     std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> &Pred)
658     const {
659   if (!isValidState())
660     return false;
661 
662   // Check all returned values but ignore call sites as long as we have not
663   // encountered an overdefined one during an update.
664   for (auto &It : ReturnedValues) {
665     Value *RV = It.first;
666     const SmallPtrSetImpl<ReturnInst *> &RetInsts = It.second;
667 
668     ImmutableCallSite ICS(RV);
669     if (ICS && !HasOverdefinedReturnedCalls)
670       continue;
671 
672     if (!Pred(*RV, RetInsts))
673       return false;
674   }
675 
676   return true;
677 }
678 
679 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A,
680                                               InformationCache &InfoCache) {
681 
682   // Check if we know of any values returned by the associated function,
683   // if not, we are done.
684   if (getNumReturnValues() == 0) {
685     indicateOptimisticFixpoint();
686     return ChangeStatus::UNCHANGED;
687   }
688 
689   // Check if any of the returned values is a call site we can refine.
690   decltype(ReturnedValues) AddRVs;
691   bool HasCallSite = false;
692 
693   // Keep track of any change to trigger updates on dependent attributes.
694   ChangeStatus Changed = ChangeStatus::UNCHANGED;
695 
696   auto *LivenessAA = A.getAAFor<AAIsDead>(*this, getAnchorScope());
697 
698   // Look at all returned call sites.
699   for (auto &It : ReturnedValues) {
700     SmallPtrSet<ReturnInst *, 2> &ReturnInsts = It.second;
701     Value *RV = It.first;
702 
703     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Potentially returned value " << *RV
704                       << "\n");
705 
706     // Only call sites can change during an update, ignore the rest.
707     CallSite RetCS(RV);
708     if (!RetCS)
709       continue;
710 
711     // For now, any call site we see will prevent us from directly fixing the
712     // state. However, if the information on the callees is fixed, the call
713     // sites will be removed and we will fix the information for this state.
714     HasCallSite = true;
715 
716     // Ignore dead ReturnValues.
717     if (LivenessAA &&
718         !LivenessAA->isLiveInstSet(ReturnInsts.begin(), ReturnInsts.end())) {
719       LLVM_DEBUG(dbgs() << "[AAReturnedValues] all returns are assumed dead, "
720                            "skip it for now\n");
721       continue;
722     }
723 
724     // Try to find a assumed unique return value for the called function.
725     auto *RetCSAA = A.getAAFor<AAReturnedValuesImpl>(*this, *RV);
726     if (!RetCSAA) {
727       if (!HasOverdefinedReturnedCalls)
728         Changed = ChangeStatus::CHANGED;
729       HasOverdefinedReturnedCalls = true;
730       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned call site (" << *RV
731                         << ") with " << (RetCSAA ? "invalid" : "no")
732                         << " associated state\n");
733       continue;
734     }
735 
736     auto *LivenessCSAA = A.getAAFor<AAIsDead>(*this, RetCSAA->getAnchorScope());
737 
738     // Try to find a assumed unique return value for the called function.
739     Optional<Value *> AssumedUniqueRV =
740         RetCSAA->getAssumedUniqueReturnValue(LivenessCSAA);
741 
742     // If no assumed unique return value was found due to the lack of
743     // candidates, we may need to resolve more calls (through more update
744     // iterations) or the called function will not return. Either way, we simply
745     // stick with the call sites as return values. Because there were not
746     // multiple possibilities, we do not treat it as overdefined.
747     if (!AssumedUniqueRV.hasValue())
748       continue;
749 
750     // If multiple, non-refinable values were found, there cannot be a unique
751     // return value for the called function. The returned call is overdefined!
752     if (!AssumedUniqueRV.getValue()) {
753       if (!HasOverdefinedReturnedCalls)
754         Changed = ChangeStatus::CHANGED;
755       HasOverdefinedReturnedCalls = true;
756       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned call site has multiple "
757                            "potentially returned values\n");
758       continue;
759     }
760 
761     LLVM_DEBUG({
762       bool UniqueRVIsKnown = RetCSAA->isAtFixpoint();
763       dbgs() << "[AAReturnedValues] Returned call site "
764              << (UniqueRVIsKnown ? "known" : "assumed")
765              << " unique return value: " << *AssumedUniqueRV << "\n";
766     });
767 
768     // The assumed unique return value.
769     Value *AssumedRetVal = AssumedUniqueRV.getValue();
770 
771     // If the assumed unique return value is an argument, lookup the matching
772     // call site operand and recursively collect new returned values.
773     // If it is not an argument, it is just put into the set of returned values
774     // as we would have already looked through casts, phis, and similar values.
775     if (Argument *AssumedRetArg = dyn_cast<Argument>(AssumedRetVal))
776       collectValuesRecursively(A,
777                                RetCS.getArgOperand(AssumedRetArg->getArgNo()),
778                                ReturnInsts, AddRVs);
779     else
780       AddRVs[AssumedRetVal].insert(ReturnInsts.begin(), ReturnInsts.end());
781   }
782 
783   for (auto &It : AddRVs) {
784     assert(!It.second.empty() && "Entry does not add anything.");
785     auto &ReturnInsts = ReturnedValues[It.first];
786     for (ReturnInst *RI : It.second)
787       if (ReturnInsts.insert(RI).second) {
788         LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
789                           << *It.first << " => " << *RI << "\n");
790         Changed = ChangeStatus::CHANGED;
791       }
792   }
793 
794   // If there is no call site in the returned values we are done.
795   if (!HasCallSite) {
796     indicateOptimisticFixpoint();
797     return ChangeStatus::CHANGED;
798   }
799 
800   return Changed;
801 }
802 
803 /// ------------------------ NoSync Function Attribute -------------------------
804 
805 struct AANoSyncImpl : AANoSync, BooleanState {
806   IRPositionConstructorForward(AANoSyncImpl, AANoSync);
807 
808   /// See AbstractAttribute::getState()
809   /// {
810   AbstractState &getState() override { return *this; }
811   const AbstractState &getState() const override { return *this; }
812   /// }
813 
814   const std::string getAsStr() const override {
815     return getAssumed() ? "nosync" : "may-sync";
816   }
817 
818   /// See AbstractAttribute::updateImpl(...).
819   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
820 
821   /// See AANoSync::isAssumedNoSync()
822   bool isAssumedNoSync() const override { return getAssumed(); }
823 
824   /// See AANoSync::isKnownNoSync()
825   bool isKnownNoSync() const override { return getKnown(); }
826 
827   /// Helper function used to determine whether an instruction is non-relaxed
828   /// atomic. In other words, if an atomic instruction does not have unordered
829   /// or monotonic ordering
830   static bool isNonRelaxedAtomic(Instruction *I);
831 
832   /// Helper function used to determine whether an instruction is volatile.
833   static bool isVolatile(Instruction *I);
834 
835   /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
836   /// memset).
837   static bool isNoSyncIntrinsic(Instruction *I);
838 };
839 
840 struct AANoSyncFunction final : public AANoSyncImpl {
841   AANoSyncFunction(Function &F) : AANoSyncImpl(F, IRP_FUNCTION) {}
842 };
843 
844 bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
845   if (!I->isAtomic())
846     return false;
847 
848   AtomicOrdering Ordering;
849   switch (I->getOpcode()) {
850   case Instruction::AtomicRMW:
851     Ordering = cast<AtomicRMWInst>(I)->getOrdering();
852     break;
853   case Instruction::Store:
854     Ordering = cast<StoreInst>(I)->getOrdering();
855     break;
856   case Instruction::Load:
857     Ordering = cast<LoadInst>(I)->getOrdering();
858     break;
859   case Instruction::Fence: {
860     auto *FI = cast<FenceInst>(I);
861     if (FI->getSyncScopeID() == SyncScope::SingleThread)
862       return false;
863     Ordering = FI->getOrdering();
864     break;
865   }
866   case Instruction::AtomicCmpXchg: {
867     AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
868     AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
869     // Only if both are relaxed, than it can be treated as relaxed.
870     // Otherwise it is non-relaxed.
871     if (Success != AtomicOrdering::Unordered &&
872         Success != AtomicOrdering::Monotonic)
873       return true;
874     if (Failure != AtomicOrdering::Unordered &&
875         Failure != AtomicOrdering::Monotonic)
876       return true;
877     return false;
878   }
879   default:
880     llvm_unreachable(
881         "New atomic operations need to be known in the attributor.");
882   }
883 
884   // Relaxed.
885   if (Ordering == AtomicOrdering::Unordered ||
886       Ordering == AtomicOrdering::Monotonic)
887     return false;
888   return true;
889 }
890 
891 /// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
892 /// FIXME: We should ipmrove the handling of intrinsics.
893 bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
894   if (auto *II = dyn_cast<IntrinsicInst>(I)) {
895     switch (II->getIntrinsicID()) {
896     /// Element wise atomic memory intrinsics are can only be unordered,
897     /// therefore nosync.
898     case Intrinsic::memset_element_unordered_atomic:
899     case Intrinsic::memmove_element_unordered_atomic:
900     case Intrinsic::memcpy_element_unordered_atomic:
901       return true;
902     case Intrinsic::memset:
903     case Intrinsic::memmove:
904     case Intrinsic::memcpy:
905       if (!cast<MemIntrinsic>(II)->isVolatile())
906         return true;
907       return false;
908     default:
909       return false;
910     }
911   }
912   return false;
913 }
914 
915 bool AANoSyncImpl::isVolatile(Instruction *I) {
916   assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
917          "Calls should not be checked here");
918 
919   switch (I->getOpcode()) {
920   case Instruction::AtomicRMW:
921     return cast<AtomicRMWInst>(I)->isVolatile();
922   case Instruction::Store:
923     return cast<StoreInst>(I)->isVolatile();
924   case Instruction::Load:
925     return cast<LoadInst>(I)->isVolatile();
926   case Instruction::AtomicCmpXchg:
927     return cast<AtomicCmpXchgInst>(I)->isVolatile();
928   default:
929     return false;
930   }
931 }
932 
933 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A,
934                                       InformationCache &InfoCache) {
935   Function &F = getAnchorScope();
936 
937   auto *LivenessAA = A.getAAFor<AAIsDead>(*this, F);
938 
939   /// We are looking for volatile instructions or Non-Relaxed atomics.
940   /// FIXME: We should ipmrove the handling of intrinsics.
941   for (Instruction *I : InfoCache.getReadOrWriteInstsForFunction(F)) {
942     // Skip assumed dead instructions.
943     if (LivenessAA && LivenessAA->isAssumedDead(I))
944       continue;
945 
946     ImmutableCallSite ICS(I);
947     auto *NoSyncAA = A.getAAFor<AANoSyncImpl>(*this, *I);
948 
949     if (isa<IntrinsicInst>(I) && isNoSyncIntrinsic(I))
950       continue;
951 
952     if (ICS && (!NoSyncAA || !NoSyncAA->isAssumedNoSync()) &&
953         !ICS.hasFnAttr(Attribute::NoSync))
954       return indicatePessimisticFixpoint();
955 
956     if (ICS)
957       continue;
958 
959     if (!isVolatile(I) && !isNonRelaxedAtomic(I))
960       continue;
961 
962     return indicatePessimisticFixpoint();
963   }
964 
965   auto CheckForNoSync = [&](Instruction &I) {
966     // At this point we handled all read/write effects and they are all
967     // nosync, so they can be skipped.
968     if (I.mayReadOrWriteMemory())
969       return true;
970 
971     // non-convergent and readnone imply nosync.
972     return !ImmutableCallSite(&I).isConvergent();
973   };
974 
975   if (!A.checkForAllCallLikeInstructions(F, CheckForNoSync, *this, InfoCache))
976     return indicatePessimisticFixpoint();
977   return ChangeStatus::UNCHANGED;
978 }
979 
980 /// ------------------------ No-Free Attributes ----------------------------
981 
982 struct AANoFreeImpl : public AANoFree, BooleanState {
983   IRPositionConstructorForward(AANoFreeImpl, AANoFree);
984 
985   /// See AbstractAttribute::getState()
986   ///{
987   AbstractState &getState() override { return *this; }
988   const AbstractState &getState() const override { return *this; }
989   ///}
990 
991   /// See AbstractAttribute::getAsStr().
992   const std::string getAsStr() const override {
993     return getAssumed() ? "nofree" : "may-free";
994   }
995 
996   /// See AbstractAttribute::updateImpl(...).
997   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
998 
999   /// Return true if "nofree" is assumed.
1000   bool isAssumedNoFree() const override { return getAssumed(); }
1001 
1002   /// Return true if "nofree" is known.
1003   bool isKnownNoFree() const override { return getKnown(); }
1004 };
1005 
1006 struct AANoFreeFunction final : public AANoFreeImpl {
1007   AANoFreeFunction(Function &F) : AANoFreeImpl(F, IRP_FUNCTION) {}
1008 };
1009 
1010 ChangeStatus AANoFreeImpl::updateImpl(Attributor &A,
1011                                       InformationCache &InfoCache) {
1012   Function &F = getAnchorScope();
1013 
1014   auto CheckForNoFree = [&](Instruction &I) {
1015     if (ImmutableCallSite(&I).hasFnAttr(Attribute::NoFree))
1016       return true;
1017 
1018     auto *NoFreeAA = A.getAAFor<AANoFreeImpl>(*this, I);
1019     return NoFreeAA && NoFreeAA->isAssumedNoFree();
1020   };
1021 
1022   if (!A.checkForAllCallLikeInstructions(F, CheckForNoFree, *this, InfoCache))
1023     return indicatePessimisticFixpoint();
1024   return ChangeStatus::UNCHANGED;
1025 }
1026 
1027 /// ------------------------ NonNull Argument Attribute ------------------------
1028 struct AANonNullImpl : AANonNull, BooleanState {
1029   IRPositionConstructorForward(AANonNullImpl, AANonNull);
1030 
1031   /// See AbstractAttribute::getState()
1032   /// {
1033   AbstractState &getState() override { return *this; }
1034   const AbstractState &getState() const override { return *this; }
1035   /// }
1036 
1037   /// See AbstractAttribute::getAsStr().
1038   const std::string getAsStr() const override {
1039     return getAssumed() ? "nonnull" : "may-null";
1040   }
1041 
1042   /// See AANonNull::isAssumedNonNull().
1043   bool isAssumedNonNull() const override { return getAssumed(); }
1044 
1045   /// See AANonNull::isKnownNonNull().
1046   bool isKnownNonNull() const override { return getKnown(); }
1047 
1048   /// Generate a predicate that checks if a given value is assumed nonnull.
1049   /// The generated function returns true if a value satisfies any of
1050   /// following conditions.
1051   /// (i) A value is known nonZero(=nonnull).
1052   /// (ii) A value is associated with AANonNull and its isAssumedNonNull() is
1053   /// true.
1054   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)>
1055   generatePredicate(Attributor &);
1056 };
1057 
1058 std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)>
1059 AANonNullImpl::generatePredicate(Attributor &A) {
1060   // FIXME: The `AAReturnedValues` should provide the predicate with the
1061   // `ReturnInst` vector as well such that we can use the control flow sensitive
1062   // version of `isKnownNonZero`. This should fix `test11` in
1063   // `test/Transforms/FunctionAttrs/nonnull.ll`
1064 
1065   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
1066       [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool {
1067     Function &F = getAnchorScope();
1068 
1069     if (isKnownNonZero(&RV, F.getParent()->getDataLayout()))
1070       return true;
1071 
1072     auto *NonNullAA = A.getAAFor<AANonNull>(*this, RV);
1073 
1074     ImmutableCallSite ICS(&RV);
1075 
1076     if ((!NonNullAA || !NonNullAA->isAssumedNonNull()) &&
1077         (!ICS || !ICS.hasRetAttr(Attribute::NonNull)))
1078       return false;
1079 
1080     return true;
1081   };
1082 
1083   return Pred;
1084 }
1085 
1086 /// NonNull attribute for function return value.
1087 struct AANonNullReturned : AANonNullImpl {
1088 
1089   AANonNullReturned(Function &F) : AANonNullImpl(F, IRP_RETURNED) {}
1090 
1091   /// See AbstractAttriubute::initialize(...).
1092   void initialize(Attributor &A, InformationCache &InfoCache) override {
1093     Function &F = getAnchorScope();
1094 
1095     // Already nonnull.
1096     if (F.getAttributes().hasAttribute(AttributeList::ReturnIndex,
1097                                        Attribute::NonNull) ||
1098         F.getAttributes().hasAttribute(AttributeList::ReturnIndex,
1099                                        Attribute::Dereferenceable))
1100       indicateOptimisticFixpoint();
1101   }
1102 
1103   /// See AbstractAttribute::updateImpl(...).
1104   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1105 };
1106 
1107 ChangeStatus AANonNullReturned::updateImpl(Attributor &A,
1108                                            InformationCache &InfoCache) {
1109   Function &F = getAnchorScope();
1110 
1111   auto *AARetVal = A.getAAFor<AAReturnedValues>(*this, F);
1112   if (!AARetVal)
1113     return indicatePessimisticFixpoint();
1114 
1115   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
1116       this->generatePredicate(A);
1117 
1118   if (!AARetVal->checkForallReturnedValues(Pred))
1119     return indicatePessimisticFixpoint();
1120   return ChangeStatus::UNCHANGED;
1121 }
1122 
1123 /// NonNull attribute for function argument.
1124 struct AANonNullArgument : AANonNullImpl {
1125 
1126   AANonNullArgument(Argument &A) : AANonNullImpl(A) {}
1127 
1128   /// See AbstractAttriubute::initialize(...).
1129   void initialize(Attributor &A, InformationCache &InfoCache) override {
1130     Argument *Arg = cast<Argument>(getAssociatedValue());
1131     if (Arg->hasNonNullAttr())
1132       indicateOptimisticFixpoint();
1133   }
1134 
1135   /// See AbstractAttribute::updateImpl(...).
1136   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1137 };
1138 
1139 /// NonNull attribute for a call site argument.
1140 struct AANonNullCallSiteArgument : AANonNullImpl {
1141 
1142   /// See AANonNullImpl::AANonNullImpl(...).
1143   AANonNullCallSiteArgument(Instruction &I, unsigned ArgNo)
1144       : AANonNullImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
1145 
1146   /// See AbstractAttribute::initialize(...).
1147   void initialize(Attributor &A, InformationCache &InfoCache) override {
1148     CallSite CS(&getAnchorValue());
1149     if (CS.paramHasAttr(getArgNo(), getAttrKind()) ||
1150         CS.paramHasAttr(getArgNo(), Attribute::Dereferenceable) ||
1151         isKnownNonZero(getAssociatedValue(),
1152                        getAnchorScope().getParent()->getDataLayout()))
1153       indicateOptimisticFixpoint();
1154   }
1155 
1156   /// See AbstractAttribute::updateImpl(Attributor &A).
1157   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1158 };
1159 
1160 ChangeStatus AANonNullArgument::updateImpl(Attributor &A,
1161                                            InformationCache &InfoCache) {
1162   Function &F = getAnchorScope();
1163   Argument &Arg = cast<Argument>(getAnchorValue());
1164 
1165   unsigned ArgNo = Arg.getArgNo();
1166 
1167   // Callback function
1168   std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) {
1169     assert(CS && "Sanity check: Call site was not initialized properly!");
1170 
1171     auto *NonNullAA = A.getAAFor<AANonNull>(*this, *CS.getInstruction(), ArgNo);
1172 
1173     // Check that NonNullAA is AANonNullCallSiteArgument.
1174     if (NonNullAA) {
1175       ImmutableCallSite ICS(&NonNullAA->getIRPosition().getAnchorValue());
1176       if (ICS && CS.getInstruction() == ICS.getInstruction())
1177         return NonNullAA->isAssumedNonNull();
1178       return false;
1179     }
1180 
1181     if (CS.paramHasAttr(ArgNo, Attribute::NonNull))
1182       return true;
1183 
1184     Value *V = CS.getArgOperand(ArgNo);
1185     if (isKnownNonZero(V, getAnchorScope().getParent()->getDataLayout()))
1186       return true;
1187 
1188     return false;
1189   };
1190   if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
1191     return indicatePessimisticFixpoint();
1192   return ChangeStatus::UNCHANGED;
1193 }
1194 
1195 ChangeStatus
1196 AANonNullCallSiteArgument::updateImpl(Attributor &A,
1197                                       InformationCache &InfoCache) {
1198   // NOTE: Never look at the argument of the callee in this method.
1199   //       If we do this, "nonnull" is always deduced because of the assumption.
1200 
1201   Value &V = *getAssociatedValue();
1202 
1203   auto *NonNullAA = A.getAAFor<AANonNull>(*this, V);
1204 
1205   if (!NonNullAA || !NonNullAA->isAssumedNonNull())
1206     return indicatePessimisticFixpoint();
1207 
1208   return ChangeStatus::UNCHANGED;
1209 }
1210 
1211 /// ------------------------ Will-Return Attributes ----------------------------
1212 
1213 struct AAWillReturnImpl : public AAWillReturn, BooleanState {
1214   IRPositionConstructorForward(AAWillReturnImpl, AAWillReturn);
1215 
1216   /// See AAWillReturn::isKnownWillReturn().
1217   bool isKnownWillReturn() const override { return getKnown(); }
1218 
1219   /// See AAWillReturn::isAssumedWillReturn().
1220   bool isAssumedWillReturn() const override { return getAssumed(); }
1221 
1222   /// See AbstractAttribute::getState(...).
1223   AbstractState &getState() override { return *this; }
1224 
1225   /// See AbstractAttribute::getState(...).
1226   const AbstractState &getState() const override { return *this; }
1227 
1228   /// See AbstractAttribute::getAsStr()
1229   const std::string getAsStr() const override {
1230     return getAssumed() ? "willreturn" : "may-noreturn";
1231   }
1232 };
1233 
1234 struct AAWillReturnFunction final : AAWillReturnImpl {
1235 
1236   /// See AbstractAttribute::AbstractAttribute(...).
1237   AAWillReturnFunction(Function &F) : AAWillReturnImpl(F, IRP_FUNCTION) {}
1238 
1239   /// See AbstractAttribute::initialize(...).
1240   void initialize(Attributor &A, InformationCache &InfoCache) override;
1241 
1242   /// See AbstractAttribute::updateImpl(...).
1243   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1244 };
1245 
1246 // Helper function that checks whether a function has any cycle.
1247 // TODO: Replace with more efficent code
1248 bool containsCycle(Function &F) {
1249   SmallPtrSet<BasicBlock *, 32> Visited;
1250 
1251   // Traverse BB by dfs and check whether successor is already visited.
1252   for (BasicBlock *BB : depth_first(&F)) {
1253     Visited.insert(BB);
1254     for (auto *SuccBB : successors(BB)) {
1255       if (Visited.count(SuccBB))
1256         return true;
1257     }
1258   }
1259   return false;
1260 }
1261 
1262 // Helper function that checks the function have a loop which might become an
1263 // endless loop
1264 // FIXME: Any cycle is regarded as endless loop for now.
1265 //        We have to allow some patterns.
1266 bool containsPossiblyEndlessLoop(Function &F) { return containsCycle(F); }
1267 
1268 void AAWillReturnFunction::initialize(Attributor &A,
1269                                       InformationCache &InfoCache) {
1270   Function &F = getAnchorScope();
1271 
1272   if (containsPossiblyEndlessLoop(F))
1273     indicatePessimisticFixpoint();
1274 }
1275 
1276 ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A,
1277                                               InformationCache &InfoCache) {
1278   const Function &F = getAnchorScope();
1279   // The map from instruction opcodes to those instructions in the function.
1280 
1281   auto CheckForWillReturn = [&](Instruction &I) {
1282     ImmutableCallSite ICS(&I);
1283     if (ICS.hasFnAttr(Attribute::WillReturn))
1284       return true;
1285 
1286     auto *WillReturnAA = A.getAAFor<AAWillReturn>(*this, I);
1287     if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn())
1288       return false;
1289 
1290     // FIXME: Prohibit any recursion for now.
1291     if (ICS.hasFnAttr(Attribute::NoRecurse))
1292       return true;
1293 
1294     auto *NoRecurseAA = A.getAAFor<AANoRecurse>(*this, I);
1295     return NoRecurseAA && NoRecurseAA->isAssumedNoRecurse();
1296   };
1297 
1298   if (!A.checkForAllCallLikeInstructions(F, CheckForWillReturn, *this,
1299                                          InfoCache))
1300     return indicatePessimisticFixpoint();
1301 
1302   return ChangeStatus::UNCHANGED;
1303 }
1304 
1305 /// ------------------------ NoAlias Argument Attribute ------------------------
1306 
1307 struct AANoAliasImpl : AANoAlias, BooleanState {
1308   IRPositionConstructorForward(AANoAliasImpl, AANoAlias);
1309 
1310   /// See AbstractAttribute::getState()
1311   /// {
1312   AbstractState &getState() override { return *this; }
1313   const AbstractState &getState() const override { return *this; }
1314   /// }
1315 
1316   const std::string getAsStr() const override {
1317     return getAssumed() ? "noalias" : "may-alias";
1318   }
1319 
1320   /// See AANoAlias::isAssumedNoAlias().
1321   bool isAssumedNoAlias() const override { return getAssumed(); }
1322 
1323   /// See AANoAlias::isKnowndNoAlias().
1324   bool isKnownNoAlias() const override { return getKnown(); }
1325 };
1326 
1327 /// NoAlias attribute for function return value.
1328 struct AANoAliasReturned : AANoAliasImpl {
1329 
1330   AANoAliasReturned(Function &F) : AANoAliasImpl(F, IRP_RETURNED) {}
1331 
1332   /// See AbstractAttriubute::initialize(...).
1333   void initialize(Attributor &A, InformationCache &InfoCache) override {
1334     Function &F = getAnchorScope();
1335 
1336     // Already noalias.
1337     if (F.returnDoesNotAlias()) {
1338       indicateOptimisticFixpoint();
1339       return;
1340     }
1341   }
1342 
1343   /// See AbstractAttribute::updateImpl(...).
1344   virtual ChangeStatus updateImpl(Attributor &A,
1345                                   InformationCache &InfoCache) override;
1346 };
1347 
1348 ChangeStatus AANoAliasReturned::updateImpl(Attributor &A,
1349                                            InformationCache &InfoCache) {
1350   Function &F = getAnchorScope();
1351 
1352   auto *AARetValImpl = A.getAAFor<AAReturnedValuesImpl>(*this, F);
1353   if (!AARetValImpl)
1354     return indicatePessimisticFixpoint();
1355 
1356   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
1357       [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool {
1358     if (Constant *C = dyn_cast<Constant>(&RV))
1359       if (C->isNullValue() || isa<UndefValue>(C))
1360         return true;
1361 
1362     /// For now, we can only deduce noalias if we have call sites.
1363     /// FIXME: add more support.
1364     ImmutableCallSite ICS(&RV);
1365     if (!ICS)
1366       return false;
1367 
1368     auto *NoAliasAA = A.getAAFor<AANoAlias>(*this, RV);
1369 
1370     if (!ICS.returnDoesNotAlias() &&
1371         (!NoAliasAA || !NoAliasAA->isAssumedNoAlias()))
1372       return false;
1373 
1374     /// FIXME: We can improve capture check in two ways:
1375     /// 1. Use the AANoCapture facilities.
1376     /// 2. Use the location of return insts for escape queries.
1377     if (PointerMayBeCaptured(&RV, /* ReturnCaptures */ false,
1378                              /* StoreCaptures */ true))
1379       return false;
1380 
1381     return true;
1382   };
1383 
1384   if (!AARetValImpl->checkForallReturnedValues(Pred))
1385     return indicatePessimisticFixpoint();
1386 
1387   return ChangeStatus::UNCHANGED;
1388 }
1389 
1390 /// -------------------AAIsDead Function Attribute-----------------------
1391 
1392 struct AAIsDeadImpl : public AAIsDead, BooleanState {
1393   IRPositionConstructorForward(AAIsDeadImpl, AAIsDead);
1394 
1395   void initialize(Attributor &A, InformationCache &InfoCache) override {
1396     Function &F = getAnchorScope();
1397 
1398     ToBeExploredPaths.insert(&(F.getEntryBlock().front()));
1399     AssumedLiveBlocks.insert(&(F.getEntryBlock()));
1400     for (size_t i = 0; i < ToBeExploredPaths.size(); ++i)
1401       if (const Instruction *NextNoReturnI =
1402               findNextNoReturn(A, ToBeExploredPaths[i]))
1403         NoReturnCalls.insert(NextNoReturnI);
1404   }
1405 
1406   /// Find the next assumed noreturn instruction in the block of \p I starting
1407   /// from, thus including, \p I.
1408   ///
1409   /// The caller is responsible to monitor the ToBeExploredPaths set as new
1410   /// instructions discovered in other basic block will be placed in there.
1411   ///
1412   /// \returns The next assumed noreturn instructions in the block of \p I
1413   ///          starting from, thus including, \p I.
1414   const Instruction *findNextNoReturn(Attributor &A, const Instruction *I);
1415 
1416   const std::string getAsStr() const override {
1417     return "LiveBBs(" + std::to_string(AssumedLiveBlocks.size()) + "/" +
1418            std::to_string(getAnchorScope().size()) + ")";
1419   }
1420 
1421   /// See AbstractAttribute::manifest(...).
1422   ChangeStatus manifest(Attributor &A) override {
1423     assert(getState().isValidState() &&
1424            "Attempted to manifest an invalid state!");
1425 
1426     ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
1427     const Function &F = getAnchorScope();
1428 
1429     // Flag to determine if we can change an invoke to a call assuming the callee
1430     // is nounwind. This is not possible if the personality of the function allows
1431     // to catch asynchronous exceptions.
1432     bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
1433 
1434     for (const Instruction *NRC : NoReturnCalls) {
1435       Instruction *I = const_cast<Instruction *>(NRC);
1436       BasicBlock *BB = I->getParent();
1437       Instruction *SplitPos = I->getNextNode();
1438 
1439       if (auto *II = dyn_cast<InvokeInst>(I)) {
1440         // If we keep the invoke the split position is at the beginning of the
1441         // normal desitination block (it invokes a noreturn function after all).
1442         BasicBlock *NormalDestBB = II->getNormalDest();
1443         SplitPos = &NormalDestBB->front();
1444 
1445         /// Invoke is replaced with a call and unreachable is placed after it if
1446         /// the callee is nounwind and noreturn. Otherwise, we keep the invoke
1447         /// and only place an unreachable in the normal successor.
1448         if (Invoke2CallAllowed) {
1449           if (Function *Callee = II->getCalledFunction()) {
1450             auto *AANoUnw = A.getAAFor<AANoUnwind>(*this, *Callee);
1451             if (Callee->hasFnAttribute(Attribute::NoUnwind) ||
1452                 (AANoUnw && AANoUnw->isAssumedNoUnwind())) {
1453               LLVM_DEBUG(dbgs()
1454                          << "[AAIsDead] Replace invoke with call inst\n");
1455               // We do not need an invoke (II) but instead want a call followed
1456               // by an unreachable. However, we do not remove II as other
1457               // abstract attributes might have it cached as part of their
1458               // results. Given that we modify the CFG anyway, we simply keep II
1459               // around but in a new dead block. To avoid II being live through
1460               // a different edge we have to ensure the block we place it in is
1461               // only reached from the current block of II and then not reached
1462               // at all when we insert the unreachable.
1463               SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c");
1464               CallInst *CI = createCallMatchingInvoke(II);
1465               CI->insertBefore(II);
1466               CI->takeName(II);
1467               II->replaceAllUsesWith(CI);
1468               SplitPos = CI->getNextNode();
1469             }
1470           }
1471         }
1472       }
1473 
1474       BB = SplitPos->getParent();
1475       SplitBlock(BB, SplitPos);
1476       changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false);
1477       HasChanged = ChangeStatus::CHANGED;
1478     }
1479 
1480     return HasChanged;
1481   }
1482 
1483   /// See AbstractAttribute::updateImpl(...).
1484   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1485 
1486   /// See AAIsDead::isAssumedDead(BasicBlock *).
1487   bool isAssumedDead(const BasicBlock *BB) const override {
1488     assert(BB->getParent() == &getAnchorScope() &&
1489            "BB must be in the same anchor scope function.");
1490 
1491     if (!getAssumed())
1492       return false;
1493     return !AssumedLiveBlocks.count(BB);
1494   }
1495 
1496   /// See AAIsDead::isKnownDead(BasicBlock *).
1497   bool isKnownDead(const BasicBlock *BB) const override {
1498     return getKnown() && isAssumedDead(BB);
1499   }
1500 
1501   /// See AAIsDead::isAssumed(Instruction *I).
1502   bool isAssumedDead(const Instruction *I) const override {
1503     assert(I->getParent()->getParent() == &getAnchorScope() &&
1504            "Instruction must be in the same anchor scope function.");
1505 
1506     if (!getAssumed())
1507       return false;
1508 
1509     // If it is not in AssumedLiveBlocks then it for sure dead.
1510     // Otherwise, it can still be after noreturn call in a live block.
1511     if (!AssumedLiveBlocks.count(I->getParent()))
1512       return true;
1513 
1514     // If it is not after a noreturn call, than it is live.
1515     return isAfterNoReturn(I);
1516   }
1517 
1518   /// See AAIsDead::isKnownDead(Instruction *I).
1519   bool isKnownDead(const Instruction *I) const override {
1520     return getKnown() && isAssumedDead(I);
1521   }
1522 
1523   /// Check if instruction is after noreturn call, in other words, assumed dead.
1524   bool isAfterNoReturn(const Instruction *I) const;
1525 
1526   /// Determine if \p F might catch asynchronous exceptions.
1527   static bool mayCatchAsynchronousExceptions(const Function &F) {
1528     return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
1529   }
1530 
1531   /// See AbstractAttribute::getState()
1532   /// {
1533   AbstractState &getState() override { return *this; }
1534   const AbstractState &getState() const override { return *this; }
1535   /// }
1536 
1537   /// Collection of to be explored paths.
1538   SmallSetVector<const Instruction *, 8> ToBeExploredPaths;
1539 
1540   /// Collection of all assumed live BasicBlocks.
1541   DenseSet<const BasicBlock *> AssumedLiveBlocks;
1542 
1543   /// Collection of calls with noreturn attribute, assumed or knwon.
1544   SmallSetVector<const Instruction *, 4> NoReturnCalls;
1545 };
1546 
1547 struct AAIsDeadFunction final : public AAIsDeadImpl {
1548   AAIsDeadFunction(Function &F) : AAIsDeadImpl(F, IRP_FUNCTION) {}
1549 };
1550 
1551 bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const {
1552   const Instruction *PrevI = I->getPrevNode();
1553   while (PrevI) {
1554     if (NoReturnCalls.count(PrevI))
1555       return true;
1556     PrevI = PrevI->getPrevNode();
1557   }
1558   return false;
1559 }
1560 
1561 const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A,
1562                                                   const Instruction *I) {
1563   const BasicBlock *BB = I->getParent();
1564   const Function &F = *BB->getParent();
1565 
1566   // Flag to determine if we can change an invoke to a call assuming the callee
1567   // is nounwind. This is not possible if the personality of the function allows
1568   // to catch asynchronous exceptions.
1569   bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
1570 
1571   // TODO: We should have a function that determines if an "edge" is dead.
1572   //       Edges could be from an instruction to the next or from a terminator
1573   //       to the successor. For now, we need to special case the unwind block
1574   //       of InvokeInst below.
1575 
1576   while (I) {
1577     ImmutableCallSite ICS(I);
1578 
1579     if (ICS) {
1580       // Regarless of the no-return property of an invoke instruction we only
1581       // learn that the regular successor is not reachable through this
1582       // instruction but the unwind block might still be.
1583       if (auto *Invoke = dyn_cast<InvokeInst>(I)) {
1584         // Use nounwind to justify the unwind block is dead as well.
1585         auto *AANoUnw = A.getAAFor<AANoUnwind>(*this, *Invoke);
1586         if (!Invoke2CallAllowed ||
1587             (!AANoUnw || !AANoUnw->isAssumedNoUnwind())) {
1588           AssumedLiveBlocks.insert(Invoke->getUnwindDest());
1589           ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front());
1590         }
1591       }
1592 
1593       auto *NoReturnAA = A.getAAFor<AANoReturn>(*this, *I);
1594       if (ICS.hasFnAttr(Attribute::NoReturn) ||
1595           (NoReturnAA && NoReturnAA->isAssumedNoReturn()))
1596         return I;
1597     }
1598 
1599     I = I->getNextNode();
1600   }
1601 
1602   // get new paths (reachable blocks).
1603   for (const BasicBlock *SuccBB : successors(BB)) {
1604     AssumedLiveBlocks.insert(SuccBB);
1605     ToBeExploredPaths.insert(&SuccBB->front());
1606   }
1607 
1608   // No noreturn instruction found.
1609   return nullptr;
1610 }
1611 
1612 ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A,
1613                                       InformationCache &InfoCache) {
1614   // Temporary collection to iterate over existing noreturn instructions. This
1615   // will alow easier modification of NoReturnCalls collection
1616   SmallVector<const Instruction *, 8> NoReturnChanged;
1617   ChangeStatus Status = ChangeStatus::UNCHANGED;
1618 
1619   for (const Instruction *I : NoReturnCalls)
1620     NoReturnChanged.push_back(I);
1621 
1622   for (const Instruction *I : NoReturnChanged) {
1623     size_t Size = ToBeExploredPaths.size();
1624 
1625     const Instruction *NextNoReturnI = findNextNoReturn(A, I);
1626     if (NextNoReturnI != I) {
1627       Status = ChangeStatus::CHANGED;
1628       NoReturnCalls.remove(I);
1629       if (NextNoReturnI)
1630         NoReturnCalls.insert(NextNoReturnI);
1631     }
1632 
1633     // Explore new paths.
1634     while (Size != ToBeExploredPaths.size()) {
1635       Status = ChangeStatus::CHANGED;
1636       if (const Instruction *NextNoReturnI =
1637               findNextNoReturn(A, ToBeExploredPaths[Size++]))
1638         NoReturnCalls.insert(NextNoReturnI);
1639     }
1640   }
1641 
1642   LLVM_DEBUG(
1643       dbgs() << "[AAIsDead] AssumedLiveBlocks: " << AssumedLiveBlocks.size()
1644              << " Total number of blocks: " << getAnchorScope().size() << "\n");
1645 
1646   return Status;
1647 }
1648 
1649 /// -------------------- Dereferenceable Argument Attribute --------------------
1650 
1651 struct DerefState : AbstractState {
1652 
1653   /// State representing for dereferenceable bytes.
1654   IntegerState DerefBytesState;
1655 
1656   /// State representing that whether the value is nonnull or global.
1657   IntegerState NonNullGlobalState;
1658 
1659   /// Bits encoding for NonNullGlobalState.
1660   enum {
1661     DEREF_NONNULL = 1 << 0,
1662     DEREF_GLOBAL = 1 << 1,
1663   };
1664 
1665   /// See AbstractState::isValidState()
1666   bool isValidState() const override { return DerefBytesState.isValidState(); }
1667 
1668   /// See AbstractState::isAtFixpoint()
1669   bool isAtFixpoint() const override {
1670     return !isValidState() || (DerefBytesState.isAtFixpoint() &&
1671                                NonNullGlobalState.isAtFixpoint());
1672   }
1673 
1674   /// See AbstractState::indicateOptimisticFixpoint(...)
1675   ChangeStatus indicateOptimisticFixpoint() override {
1676     DerefBytesState.indicateOptimisticFixpoint();
1677     NonNullGlobalState.indicateOptimisticFixpoint();
1678     return ChangeStatus::UNCHANGED;
1679   }
1680 
1681   /// See AbstractState::indicatePessimisticFixpoint(...)
1682   ChangeStatus indicatePessimisticFixpoint() override {
1683     DerefBytesState.indicatePessimisticFixpoint();
1684     NonNullGlobalState.indicatePessimisticFixpoint();
1685     return ChangeStatus::CHANGED;
1686   }
1687 
1688   /// Update known dereferenceable bytes.
1689   void takeKnownDerefBytesMaximum(uint64_t Bytes) {
1690     DerefBytesState.takeKnownMaximum(Bytes);
1691   }
1692 
1693   /// Update assumed dereferenceable bytes.
1694   void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
1695     DerefBytesState.takeAssumedMinimum(Bytes);
1696   }
1697 
1698   /// Update assumed NonNullGlobalState
1699   void updateAssumedNonNullGlobalState(bool IsNonNull, bool IsGlobal) {
1700     if (!IsNonNull)
1701       NonNullGlobalState.removeAssumedBits(DEREF_NONNULL);
1702     if (!IsGlobal)
1703       NonNullGlobalState.removeAssumedBits(DEREF_GLOBAL);
1704   }
1705 
1706   /// Equality for DerefState.
1707   bool operator==(const DerefState &R) {
1708     return this->DerefBytesState == R.DerefBytesState &&
1709            this->NonNullGlobalState == R.NonNullGlobalState;
1710   }
1711 };
1712 
1713 struct AADereferenceableImpl : AADereferenceable, DerefState {
1714   IRPositionConstructorForward(AADereferenceableImpl, AADereferenceable);
1715 
1716   /// See AbstractAttribute::getState()
1717   /// {
1718   AbstractState &getState() override { return *this; }
1719   const AbstractState &getState() const override { return *this; }
1720   /// }
1721 
1722   /// See AADereferenceable::getAssumedDereferenceableBytes().
1723   uint32_t getAssumedDereferenceableBytes() const override {
1724     return DerefBytesState.getAssumed();
1725   }
1726 
1727   /// See AADereferenceable::getKnownDereferenceableBytes().
1728   uint32_t getKnownDereferenceableBytes() const override {
1729     return DerefBytesState.getKnown();
1730   }
1731 
1732   // Helper function for syncing nonnull state.
1733   void syncNonNull(const AANonNull *NonNullAA) {
1734     if (!NonNullAA) {
1735       NonNullGlobalState.removeAssumedBits(DEREF_NONNULL);
1736       return;
1737     }
1738 
1739     if (NonNullAA->isKnownNonNull())
1740       NonNullGlobalState.addKnownBits(DEREF_NONNULL);
1741 
1742     if (!NonNullAA->isAssumedNonNull())
1743       NonNullGlobalState.removeAssumedBits(DEREF_NONNULL);
1744   }
1745 
1746   /// See AADereferenceable::isAssumedGlobal().
1747   bool isAssumedGlobal() const override {
1748     return NonNullGlobalState.isAssumed(DEREF_GLOBAL);
1749   }
1750 
1751   /// See AADereferenceable::isKnownGlobal().
1752   bool isKnownGlobal() const override {
1753     return NonNullGlobalState.isKnown(DEREF_GLOBAL);
1754   }
1755 
1756   /// See AADereferenceable::isAssumedNonNull().
1757   bool isAssumedNonNull() const override {
1758     return NonNullGlobalState.isAssumed(DEREF_NONNULL);
1759   }
1760 
1761   /// See AADereferenceable::isKnownNonNull().
1762   bool isKnownNonNull() const override {
1763     return NonNullGlobalState.isKnown(DEREF_NONNULL);
1764   }
1765 
1766   void getDeducedAttributes(LLVMContext &Ctx,
1767                             SmallVectorImpl<Attribute> &Attrs) const override {
1768     // TODO: Add *_globally support
1769     if (isAssumedNonNull())
1770       Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
1771           Ctx, getAssumedDereferenceableBytes()));
1772     else
1773       Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
1774           Ctx, getAssumedDereferenceableBytes()));
1775   }
1776   uint64_t computeAssumedDerefenceableBytes(Attributor &A, Value &V,
1777                                             bool &IsNonNull, bool &IsGlobal);
1778 
1779   void initialize(Attributor &A, InformationCache &InfoCache) override {
1780     Function &F = getAnchorScope();
1781     unsigned AttrIdx = getIRPosition().getAttrIdx();
1782 
1783     for (Attribute::AttrKind AK :
1784          {Attribute::Dereferenceable, Attribute::DereferenceableOrNull})
1785       if (F.getAttributes().hasAttribute(AttrIdx, AK))
1786         takeKnownDerefBytesMaximum(F.getAttribute(AttrIdx, AK).getValueAsInt());
1787   }
1788 
1789   /// See AbstractAttribute::getAsStr().
1790   const std::string getAsStr() const override {
1791     if (!getAssumedDereferenceableBytes())
1792       return "unknown-dereferenceable";
1793     return std::string("dereferenceable") +
1794            (isAssumedNonNull() ? "" : "_or_null") +
1795            (isAssumedGlobal() ? "_globally" : "") + "<" +
1796            std::to_string(getKnownDereferenceableBytes()) + "-" +
1797            std::to_string(getAssumedDereferenceableBytes()) + ">";
1798   }
1799 };
1800 
1801 struct AADereferenceableReturned : AADereferenceableImpl {
1802   AADereferenceableReturned(Function &F)
1803       : AADereferenceableImpl(F, IRP_RETURNED) {}
1804 
1805   /// See AbstractAttribute::updateImpl(...).
1806   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1807 };
1808 
1809 // Helper function that returns dereferenceable bytes.
1810 static uint64_t calcDifferenceIfBaseIsNonNull(int64_t DerefBytes,
1811                                               int64_t Offset, bool IsNonNull) {
1812   if (!IsNonNull)
1813     return 0;
1814   return std::max((int64_t)0, DerefBytes - Offset);
1815 }
1816 
1817 uint64_t AADereferenceableImpl::computeAssumedDerefenceableBytes(
1818     Attributor &A, Value &V, bool &IsNonNull, bool &IsGlobal) {
1819   // TODO: Tracking the globally flag.
1820   IsGlobal = false;
1821 
1822   // First, we try to get information about V from Attributor.
1823   if (auto *DerefAA = A.getAAFor<AADereferenceable>(*this, V)) {
1824     IsNonNull &= DerefAA->isAssumedNonNull();
1825     return DerefAA->getAssumedDereferenceableBytes();
1826   }
1827 
1828   // Otherwise, we try to compute assumed bytes from base pointer.
1829   const DataLayout &DL = getAnchorScope().getParent()->getDataLayout();
1830   unsigned IdxWidth =
1831       DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
1832   APInt Offset(IdxWidth, 0);
1833   Value *Base = V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
1834 
1835   if (auto *BaseDerefAA = A.getAAFor<AADereferenceable>(*this, *Base)) {
1836     IsNonNull &= Offset != 0;
1837     return calcDifferenceIfBaseIsNonNull(
1838         BaseDerefAA->getAssumedDereferenceableBytes(), Offset.getSExtValue(),
1839         Offset != 0 || BaseDerefAA->isAssumedNonNull());
1840   }
1841 
1842   // Then, use IR information.
1843 
1844   if (isDereferenceablePointer(Base, Base->getType(), DL))
1845     return calcDifferenceIfBaseIsNonNull(
1846         DL.getTypeStoreSize(Base->getType()->getPointerElementType()),
1847         Offset.getSExtValue(),
1848         !NullPointerIsDefined(&getAnchorScope(),
1849                               V.getType()->getPointerAddressSpace()));
1850 
1851   IsNonNull = false;
1852   return 0;
1853 }
1854 
1855 ChangeStatus
1856 AADereferenceableReturned::updateImpl(Attributor &A,
1857                                       InformationCache &InfoCache) {
1858   Function &F = getAnchorScope();
1859   auto BeforeState = static_cast<DerefState>(*this);
1860 
1861   syncNonNull(A.getAAFor<AANonNull>(*this, F));
1862 
1863   auto *AARetVal = A.getAAFor<AAReturnedValues>(*this, F);
1864   if (!AARetVal)
1865     return indicatePessimisticFixpoint();
1866 
1867   bool IsNonNull = isAssumedNonNull();
1868   bool IsGlobal = isAssumedGlobal();
1869 
1870   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
1871       [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool {
1872     takeAssumedDerefBytesMinimum(
1873         computeAssumedDerefenceableBytes(A, RV, IsNonNull, IsGlobal));
1874     return isValidState();
1875   };
1876 
1877   if (AARetVal->checkForallReturnedValues(Pred)) {
1878     updateAssumedNonNullGlobalState(IsNonNull, IsGlobal);
1879     return BeforeState == static_cast<DerefState>(*this)
1880                ? ChangeStatus::UNCHANGED
1881                : ChangeStatus::CHANGED;
1882   }
1883   return indicatePessimisticFixpoint();
1884 }
1885 
1886 struct AADereferenceableArgument : AADereferenceableImpl {
1887   AADereferenceableArgument(Argument &A) : AADereferenceableImpl(A) {}
1888 
1889   /// See AbstractAttribute::updateImpl(...).
1890   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1891 };
1892 
1893 ChangeStatus
1894 AADereferenceableArgument::updateImpl(Attributor &A,
1895                                       InformationCache &InfoCache) {
1896   Function &F = getAnchorScope();
1897   Argument &Arg = cast<Argument>(getAnchorValue());
1898 
1899   auto BeforeState = static_cast<DerefState>(*this);
1900 
1901   unsigned ArgNo = Arg.getArgNo();
1902 
1903   syncNonNull(A.getAAFor<AANonNull>(*this, F, ArgNo));
1904 
1905   bool IsNonNull = isAssumedNonNull();
1906   bool IsGlobal = isAssumedGlobal();
1907 
1908   // Callback function
1909   std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) -> bool {
1910     assert(CS && "Sanity check: Call site was not initialized properly!");
1911 
1912     // Check that DereferenceableAA is AADereferenceableCallSiteArgument.
1913     if (auto *DereferenceableAA =
1914             A.getAAFor<AADereferenceable>(*this, *CS.getInstruction(), ArgNo)) {
1915       ImmutableCallSite ICS(
1916           &DereferenceableAA->getIRPosition().getAnchorValue());
1917       if (ICS && CS.getInstruction() == ICS.getInstruction()) {
1918         takeAssumedDerefBytesMinimum(
1919             DereferenceableAA->getAssumedDereferenceableBytes());
1920         IsNonNull &= DereferenceableAA->isAssumedNonNull();
1921         IsGlobal &= DereferenceableAA->isAssumedGlobal();
1922         return isValidState();
1923       }
1924     }
1925 
1926     takeAssumedDerefBytesMinimum(computeAssumedDerefenceableBytes(
1927         A, *CS.getArgOperand(ArgNo), IsNonNull, IsGlobal));
1928 
1929     return isValidState();
1930   };
1931 
1932   if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
1933     return indicatePessimisticFixpoint();
1934 
1935   updateAssumedNonNullGlobalState(IsNonNull, IsGlobal);
1936 
1937   return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED
1938                                                        : ChangeStatus::CHANGED;
1939 }
1940 
1941 /// Dereferenceable attribute for a call site argument.
1942 struct AADereferenceableCallSiteArgument : AADereferenceableImpl {
1943 
1944   /// See AADereferenceableImpl::AADereferenceableImpl(...).
1945   AADereferenceableCallSiteArgument(Instruction &I, unsigned ArgNo)
1946       : AADereferenceableImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
1947 
1948   /// See AbstractAttribute::initialize(...).
1949   void initialize(Attributor &A, InformationCache &InfoCache) override {
1950     CallSite CS(&getAnchorValue());
1951     if (CS.paramHasAttr(getArgNo(), Attribute::Dereferenceable))
1952       takeKnownDerefBytesMaximum(CS.getDereferenceableBytes(getArgNo()));
1953 
1954     if (CS.paramHasAttr(getArgNo(), Attribute::DereferenceableOrNull))
1955       takeKnownDerefBytesMaximum(CS.getDereferenceableOrNullBytes(getArgNo()));
1956   }
1957 
1958   /// See AbstractAttribute::updateImpl(Attributor &A).
1959   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
1960 };
1961 
1962 ChangeStatus
1963 AADereferenceableCallSiteArgument::updateImpl(Attributor &A,
1964                                               InformationCache &InfoCache) {
1965   // NOTE: Never look at the argument of the callee in this method.
1966   //       If we do this, "dereferenceable" is always deduced because of the
1967   //       assumption.
1968 
1969   Value &V = *getAssociatedValue();
1970 
1971   auto BeforeState = static_cast<DerefState>(*this);
1972 
1973   syncNonNull(A.getAAFor<AANonNull>(*this, getAnchorValue(), getArgNo()));
1974   bool IsNonNull = isAssumedNonNull();
1975   bool IsGlobal = isKnownGlobal();
1976 
1977   takeAssumedDerefBytesMinimum(
1978       computeAssumedDerefenceableBytes(A, V, IsNonNull, IsGlobal));
1979   updateAssumedNonNullGlobalState(IsNonNull, IsGlobal);
1980 
1981   return BeforeState == static_cast<DerefState>(*this) ? ChangeStatus::UNCHANGED
1982                                                        : ChangeStatus::CHANGED;
1983 }
1984 
1985 // ------------------------ Align Argument Attribute ------------------------
1986 
1987 struct AAAlignImpl : AAAlign, IntegerState {
1988   IRPositionConstructorForward(AAAlignImpl, AAAlign);
1989 
1990   // Max alignemnt value allowed in IR
1991   static const unsigned MAX_ALIGN = 1U << 29;
1992 
1993   /// See AbstractAttribute::getState()
1994   /// {
1995   AbstractState &getState() override { return *this; }
1996   const AbstractState &getState() const override { return *this; }
1997   /// }
1998 
1999   virtual const std::string getAsStr() const override {
2000     return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
2001                                 "-" + std::to_string(getAssumedAlign()) + ">")
2002                              : "unknown-align";
2003   }
2004 
2005   /// See AAAlign::getAssumedAlign().
2006   unsigned getAssumedAlign() const override { return getAssumed(); }
2007 
2008   /// See AAAlign::getKnownAlign().
2009   unsigned getKnownAlign() const override { return getKnown(); }
2010 
2011   /// See AbstractAttriubute::initialize(...).
2012   void initialize(Attributor &A, InformationCache &InfoCache) override {
2013     takeAssumedMinimum(MAX_ALIGN);
2014 
2015     Function &F = getAnchorScope();
2016 
2017     unsigned AttrIdx = getIRPosition().getAttrIdx();
2018 
2019     // Already the function has align attribute on return value or argument.
2020     if (F.getAttributes().hasAttribute(AttrIdx, Attribute::Alignment))
2021       addKnownBits(
2022           F.getAttribute(AttrIdx, Attribute::Alignment).getAlignment());
2023   }
2024 
2025   /// See AbstractAttribute::getDeducedAttributes
2026   virtual void
2027   getDeducedAttributes(LLVMContext &Ctx,
2028                        SmallVectorImpl<Attribute> &Attrs) const override {
2029     Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign()));
2030   }
2031 };
2032 
2033 /// Align attribute for function return value.
2034 struct AAAlignReturned final : AAAlignImpl {
2035 
2036   AAAlignReturned(Function &F) : AAAlignImpl(F, IRP_RETURNED) {}
2037 
2038   /// See AbstractAttribute::updateImpl(...).
2039   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
2040 };
2041 
2042 ChangeStatus AAAlignReturned::updateImpl(Attributor &A,
2043                                          InformationCache &InfoCache) {
2044   Function &F = getAnchorScope();
2045   auto *AARetValImpl = A.getAAFor<AAReturnedValuesImpl>(*this, F);
2046   if (!AARetValImpl)
2047     return indicatePessimisticFixpoint();
2048 
2049   // Currently, align<n> is deduced if alignments in return values are assumed
2050   // as greater than n. We reach pessimistic fixpoint if any of the return value
2051   // wouldn't have align. If no assumed state was used for reasoning, an
2052   // optimistic fixpoint is reached earlier.
2053 
2054   base_t BeforeState = getAssumed();
2055   std::function<bool(Value &, const SmallPtrSetImpl<ReturnInst *> &)> Pred =
2056       [&](Value &RV, const SmallPtrSetImpl<ReturnInst *> &RetInsts) -> bool {
2057     auto *AlignAA = A.getAAFor<AAAlign>(*this, RV);
2058 
2059     if (AlignAA)
2060       takeAssumedMinimum(AlignAA->getAssumedAlign());
2061     else
2062       // Use IR information.
2063       takeAssumedMinimum(RV.getPointerAlignment(
2064           getAnchorScope().getParent()->getDataLayout()));
2065 
2066     return isValidState();
2067   };
2068 
2069   if (!AARetValImpl->checkForallReturnedValues(Pred))
2070     return indicatePessimisticFixpoint();
2071 
2072   return (getAssumed() != BeforeState) ? ChangeStatus::CHANGED
2073                                        : ChangeStatus::UNCHANGED;
2074 }
2075 
2076 /// Align attribute for function argument.
2077 struct AAAlignArgument final : AAAlignImpl {
2078 
2079   AAAlignArgument(Argument &A) : AAAlignImpl(A) {}
2080 
2081   /// See AbstractAttribute::updateImpl(...).
2082   virtual ChangeStatus updateImpl(Attributor &A,
2083                                   InformationCache &InfoCache) override;
2084 };
2085 
2086 ChangeStatus AAAlignArgument::updateImpl(Attributor &A,
2087                                          InformationCache &InfoCache) {
2088 
2089   Function &F = getAnchorScope();
2090   Argument &Arg = cast<Argument>(getAnchorValue());
2091 
2092   unsigned ArgNo = Arg.getArgNo();
2093   const DataLayout &DL = F.getParent()->getDataLayout();
2094 
2095   auto BeforeState = getAssumed();
2096 
2097   // Callback function
2098   std::function<bool(CallSite)> CallSiteCheck = [&](CallSite CS) {
2099     assert(CS && "Sanity check: Call site was not initialized properly!");
2100 
2101     auto *AlignAA = A.getAAFor<AAAlign>(*this, *CS.getInstruction(), ArgNo);
2102 
2103     // Check that AlignAA is AAAlignCallSiteArgument.
2104     if (AlignAA) {
2105       ImmutableCallSite ICS(&AlignAA->getIRPosition().getAnchorValue());
2106       if (ICS && CS.getInstruction() == ICS.getInstruction()) {
2107         takeAssumedMinimum(AlignAA->getAssumedAlign());
2108         return isValidState();
2109       }
2110     }
2111 
2112     Value *V = CS.getArgOperand(ArgNo);
2113     takeAssumedMinimum(V->getPointerAlignment(DL));
2114     return isValidState();
2115   };
2116 
2117   if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
2118     indicatePessimisticFixpoint();
2119 
2120   return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED
2121                                      : ChangeStatus ::CHANGED;
2122 }
2123 
2124 struct AAAlignCallSiteArgument final : AAAlignImpl {
2125 
2126   /// See AANonNullImpl::AANonNullImpl(...).
2127   AAAlignCallSiteArgument(Instruction &I, unsigned ArgNo)
2128       : AAAlignImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
2129 
2130   /// See AbstractAttribute::initialize(...).
2131   void initialize(Attributor &A, InformationCache &InfoCache) override {
2132     CallSite CS(&getAnchorValue());
2133     takeKnownMaximum(getAssociatedValue()->getPointerAlignment(
2134         getAnchorScope().getParent()->getDataLayout()));
2135   }
2136 
2137   /// See AbstractAttribute::updateImpl(Attributor &A).
2138   ChangeStatus updateImpl(Attributor &A, InformationCache &InfoCache) override;
2139 };
2140 
2141 ChangeStatus AAAlignCallSiteArgument::updateImpl(Attributor &A,
2142                                                  InformationCache &InfoCache) {
2143   // NOTE: Never look at the argument of the callee in this method.
2144   //       If we do this, "align" is always deduced because of the assumption.
2145 
2146   auto BeforeState = getAssumed();
2147 
2148   Value &V = *getAssociatedValue();
2149 
2150   auto *AlignAA = A.getAAFor<AAAlign>(*this, V);
2151 
2152   if (AlignAA)
2153     takeAssumedMinimum(AlignAA->getAssumedAlign());
2154   else
2155     indicatePessimisticFixpoint();
2156 
2157   return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED
2158                                      : ChangeStatus::CHANGED;
2159 }
2160 
2161 /// ------------------ Function No-Return Attribute ----------------------------
2162 struct AANoReturnImpl : public AANoReturn, BooleanState {
2163   IRPositionConstructorForward(AANoReturnImpl, AANoReturn);
2164 
2165   /// See AbstractAttribute::getState()
2166   /// {
2167   AbstractState &getState() override { return *this; }
2168   const AbstractState &getState() const override { return *this; }
2169   /// }
2170 
2171   /// Return true if the underlying object is known to never return.
2172   bool isKnownNoReturn() const override { return getKnown(); }
2173 
2174   /// Return true if the underlying object is assumed to never return.
2175   bool isAssumedNoReturn() const override { return getAssumed(); }
2176 
2177   /// See AbstractAttribute::getAsStr().
2178   const std::string getAsStr() const override {
2179     return getAssumed() ? "noreturn" : "may-return";
2180   }
2181 
2182   /// See AbstractAttribute::initialize(...).
2183   void initialize(Attributor &A, InformationCache &InfoCache) override {
2184     Function &F = getAnchorScope();
2185     if (F.hasFnAttribute(getAttrKind()))
2186       indicateOptimisticFixpoint();
2187   }
2188 
2189   /// See AbstractAttribute::updateImpl(Attributor &A).
2190   virtual ChangeStatus updateImpl(Attributor &A,
2191                                   InformationCache &InfoCache) override {
2192     const Function &F = getAnchorScope();
2193     auto CheckForNoReturn = [](Instruction &) { return false; };
2194     if (!A.checkForAllInstructions(F, CheckForNoReturn, *this, InfoCache,
2195                                    {(unsigned)Instruction::Ret}))
2196       return indicatePessimisticFixpoint();
2197     return ChangeStatus::UNCHANGED;
2198   }
2199 };
2200 
2201 struct AANoReturnFunction final : AANoReturnImpl {
2202   AANoReturnFunction(Function &F) : AANoReturnImpl(F, IRP_FUNCTION) {}
2203 };
2204 
2205 /// ----------------------------------------------------------------------------
2206 ///                               Attributor
2207 /// ----------------------------------------------------------------------------
2208 
2209 bool Attributor::checkForAllCallSites(Function &F,
2210                                       std::function<bool(CallSite)> &Pred,
2211                                       AbstractAttribute &QueryingAA,
2212                                       bool RequireAllCallSites) {
2213   // We can try to determine information from
2214   // the call sites. However, this is only possible all call sites are known,
2215   // hence the function has internal linkage.
2216   if (RequireAllCallSites && !F.hasInternalLinkage()) {
2217     LLVM_DEBUG(
2218         dbgs()
2219         << "Attributor: Function " << F.getName()
2220         << " has no internal linkage, hence not all call sites are known\n");
2221     return false;
2222   }
2223 
2224   for (const Use &U : F.uses()) {
2225     Instruction *I = cast<Instruction>(U.getUser());
2226     Function *AnchorValue = I->getParent()->getParent();
2227 
2228     auto *LivenessAA = getAAFor<AAIsDead>(QueryingAA, *AnchorValue);
2229 
2230     // Skip dead calls.
2231     if (LivenessAA && LivenessAA->isAssumedDead(I))
2232       continue;
2233 
2234     CallSite CS(U.getUser());
2235     if (!CS || !CS.isCallee(&U) || !CS.getCaller()->hasExactDefinition()) {
2236       if (!RequireAllCallSites)
2237         continue;
2238 
2239       LLVM_DEBUG(dbgs() << "Attributor: User " << *U.getUser()
2240                         << " is an invalid use of " << F.getName() << "\n");
2241       return false;
2242     }
2243 
2244     if (Pred(CS))
2245       continue;
2246 
2247     LLVM_DEBUG(dbgs() << "Attributor: Call site callback failed for "
2248                       << *CS.getInstruction() << "\n");
2249     return false;
2250   }
2251 
2252   return true;
2253 }
2254 
2255 bool Attributor::checkForAllInstructions(
2256     const Function &F, const llvm::function_ref<bool(Instruction &)> &Pred,
2257     AbstractAttribute &QueryingAA, InformationCache &InfoCache,
2258     const ArrayRef<unsigned> &Opcodes) {
2259 
2260   auto *LivenessAA = getAAFor<AAIsDead>(QueryingAA, F);
2261 
2262   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
2263   for (unsigned Opcode : Opcodes) {
2264     for (Instruction *I : OpcodeInstMap[Opcode]) {
2265       // Skip dead instructions.
2266       if (LivenessAA && LivenessAA->isAssumedDead(I))
2267         continue;
2268 
2269       if (!Pred(*I))
2270         return false;
2271     }
2272   }
2273 
2274   return true;
2275 }
2276 
2277 ChangeStatus Attributor::run(InformationCache &InfoCache) {
2278   // Initialize all abstract attributes.
2279   for (AbstractAttribute *AA : AllAbstractAttributes)
2280     AA->initialize(*this, InfoCache);
2281 
2282   LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
2283                     << AllAbstractAttributes.size()
2284                     << " abstract attributes.\n");
2285 
2286   // Now that all abstract attributes are collected and initialized we start
2287   // the abstract analysis.
2288 
2289   unsigned IterationCounter = 1;
2290 
2291   SmallVector<AbstractAttribute *, 64> ChangedAAs;
2292   SetVector<AbstractAttribute *> Worklist;
2293   Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
2294 
2295   do {
2296     LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
2297                       << ", Worklist size: " << Worklist.size() << "\n");
2298 
2299     // Add all abstract attributes that are potentially dependent on one that
2300     // changed to the work list.
2301     for (AbstractAttribute *ChangedAA : ChangedAAs) {
2302       auto &QuerriedAAs = QueryMap[ChangedAA];
2303       Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end());
2304     }
2305 
2306     // Reset the changed set.
2307     ChangedAAs.clear();
2308 
2309     // Update all abstract attribute in the work list and record the ones that
2310     // changed.
2311     for (AbstractAttribute *AA : Worklist)
2312       if (AA->update(*this, InfoCache) == ChangeStatus::CHANGED)
2313         ChangedAAs.push_back(AA);
2314 
2315     // Reset the work list and repopulate with the changed abstract attributes.
2316     // Note that dependent ones are added above.
2317     Worklist.clear();
2318     Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
2319 
2320   } while (!Worklist.empty() && ++IterationCounter < MaxFixpointIterations);
2321 
2322   LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
2323                     << IterationCounter << "/" << MaxFixpointIterations
2324                     << " iterations\n");
2325 
2326   bool FinishedAtFixpoint = Worklist.empty();
2327 
2328   // Reset abstract arguments not settled in a sound fixpoint by now. This
2329   // happens when we stopped the fixpoint iteration early. Note that only the
2330   // ones marked as "changed" *and* the ones transitively depending on them
2331   // need to be reverted to a pessimistic state. Others might not be in a
2332   // fixpoint state but we can use the optimistic results for them anyway.
2333   SmallPtrSet<AbstractAttribute *, 32> Visited;
2334   for (unsigned u = 0; u < ChangedAAs.size(); u++) {
2335     AbstractAttribute *ChangedAA = ChangedAAs[u];
2336     if (!Visited.insert(ChangedAA).second)
2337       continue;
2338 
2339     AbstractState &State = ChangedAA->getState();
2340     if (!State.isAtFixpoint()) {
2341       State.indicatePessimisticFixpoint();
2342 
2343       NumAttributesTimedOut++;
2344     }
2345 
2346     auto &QuerriedAAs = QueryMap[ChangedAA];
2347     ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end());
2348   }
2349 
2350   LLVM_DEBUG({
2351     if (!Visited.empty())
2352       dbgs() << "\n[Attributor] Finalized " << Visited.size()
2353              << " abstract attributes.\n";
2354   });
2355 
2356   unsigned NumManifested = 0;
2357   unsigned NumAtFixpoint = 0;
2358   ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
2359   for (AbstractAttribute *AA : AllAbstractAttributes) {
2360     AbstractState &State = AA->getState();
2361 
2362     // If there is not already a fixpoint reached, we can now take the
2363     // optimistic state. This is correct because we enforced a pessimistic one
2364     // on abstract attributes that were transitively dependent on a changed one
2365     // already above.
2366     if (!State.isAtFixpoint())
2367       State.indicateOptimisticFixpoint();
2368 
2369     // If the state is invalid, we do not try to manifest it.
2370     if (!State.isValidState())
2371       continue;
2372 
2373     // Manifest the state and record if we changed the IR.
2374     ChangeStatus LocalChange = AA->manifest(*this);
2375     ManifestChange = ManifestChange | LocalChange;
2376 
2377     NumAtFixpoint++;
2378     NumManifested += (LocalChange == ChangeStatus::CHANGED);
2379   }
2380 
2381   (void)NumManifested;
2382   (void)NumAtFixpoint;
2383   LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
2384                     << " arguments while " << NumAtFixpoint
2385                     << " were in a valid fixpoint state\n");
2386 
2387   // If verification is requested, we finished this run at a fixpoint, and the
2388   // IR was changed, we re-run the whole fixpoint analysis, starting at
2389   // re-initialization of the arguments. This re-run should not result in an IR
2390   // change. Though, the (virtual) state of attributes at the end of the re-run
2391   // might be more optimistic than the known state or the IR state if the better
2392   // state cannot be manifested.
2393   if (VerifyAttributor && FinishedAtFixpoint &&
2394       ManifestChange == ChangeStatus::CHANGED) {
2395     VerifyAttributor = false;
2396     ChangeStatus VerifyStatus = run(InfoCache);
2397     if (VerifyStatus != ChangeStatus::UNCHANGED)
2398       llvm_unreachable(
2399           "Attributor verification failed, re-run did result in an IR change "
2400           "even after a fixpoint was reached in the original run. (False "
2401           "positives possible!)");
2402     VerifyAttributor = true;
2403   }
2404 
2405   NumAttributesManifested += NumManifested;
2406   NumAttributesValidFixpoint += NumAtFixpoint;
2407 
2408   return ManifestChange;
2409 }
2410 
2411 /// Helper function that checks if an abstract attribute of type \p AAType
2412 /// should be created for \p V (with argument number \p ArgNo) and if so creates
2413 /// and registers it with the Attributor \p A.
2414 ///
2415 /// This method will look at the provided whitelist. If one is given and the
2416 /// kind \p AAType::ID is not contained, no abstract attribute is created.
2417 ///
2418 /// \returns The created abstract argument, or nullptr if none was created.
2419 template <typename AAType, typename ValueType, typename... ArgsTy>
2420 static AAType *checkAndRegisterAA(const Function &F, Attributor &A,
2421                                   DenseSet<const char *> *Whitelist,
2422                                   ValueType &V, int ArgNo, ArgsTy... Args) {
2423   if (Whitelist && !Whitelist->count(&AAType::ID))
2424     return nullptr;
2425 
2426   return &A.registerAA<AAType>(*new AAType(V, Args...), ArgNo);
2427 }
2428 
2429 void Attributor::identifyDefaultAbstractAttributes(
2430     Function &F, InformationCache &InfoCache,
2431     DenseSet<const char *> *Whitelist) {
2432 
2433   // Check for dead BasicBlocks in every function.
2434   // We need dead instruction detection because we do not want to deal with
2435   // broken IR in which SSA rules do not apply.
2436   checkAndRegisterAA<AAIsDeadFunction>(F, *this, /* Whitelist */ nullptr, F,
2437                                        -1);
2438 
2439   // Every function might be "will-return".
2440   checkAndRegisterAA<AAWillReturnFunction>(F, *this, Whitelist, F, -1);
2441 
2442   // Every function can be nounwind.
2443   checkAndRegisterAA<AANoUnwindFunction>(F, *this, Whitelist, F, -1);
2444 
2445   // Every function might be marked "nosync"
2446   checkAndRegisterAA<AANoSyncFunction>(F, *this, Whitelist, F, -1);
2447 
2448   // Every function might be "no-free".
2449   checkAndRegisterAA<AANoFreeFunction>(F, *this, Whitelist, F, -1);
2450 
2451   // Every function might be "no-return".
2452   checkAndRegisterAA<AANoReturnFunction>(F, *this, Whitelist, F, -1);
2453 
2454   // Return attributes are only appropriate if the return type is non void.
2455   Type *ReturnType = F.getReturnType();
2456   if (!ReturnType->isVoidTy()) {
2457     // Argument attribute "returned" --- Create only one per function even
2458     // though it is an argument attribute.
2459     checkAndRegisterAA<AAReturnedValuesFunction>(F, *this, Whitelist, F, -1);
2460 
2461     if (ReturnType->isPointerTy()) {
2462       // Every function with pointer return type might be marked align.
2463       checkAndRegisterAA<AAAlignReturned>(F, *this, Whitelist, F, -1);
2464 
2465       // Every function with pointer return type might be marked nonnull.
2466       checkAndRegisterAA<AANonNullReturned>(F, *this, Whitelist, F, -1);
2467 
2468       // Every function with pointer return type might be marked noalias.
2469       checkAndRegisterAA<AANoAliasReturned>(F, *this, Whitelist, F, -1);
2470 
2471       // Every function with pointer return type might be marked
2472       // dereferenceable.
2473       checkAndRegisterAA<AADereferenceableReturned>(F, *this, Whitelist, F, -1);
2474     }
2475   }
2476 
2477   for (Argument &Arg : F.args()) {
2478     if (Arg.getType()->isPointerTy()) {
2479       // Every argument with pointer type might be marked nonnull.
2480       checkAndRegisterAA<AANonNullArgument>(F, *this, Whitelist, Arg,
2481                                             Arg.getArgNo());
2482 
2483       // Every argument with pointer type might be marked dereferenceable.
2484       checkAndRegisterAA<AADereferenceableArgument>(F, *this, Whitelist, Arg,
2485                                                     Arg.getArgNo());
2486 
2487       // Every argument with pointer type might be marked align.
2488       checkAndRegisterAA<AAAlignArgument>(F, *this, Whitelist, Arg,
2489                                           Arg.getArgNo());
2490     }
2491   }
2492 
2493   // Walk all instructions to find more attribute opportunities and also
2494   // interesting instructions that might be queried by abstract attributes
2495   // during their initialization or update.
2496   auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
2497   auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
2498 
2499   for (Instruction &I : instructions(&F)) {
2500     bool IsInterestingOpcode = false;
2501 
2502     // To allow easy access to all instructions in a function with a given
2503     // opcode we store them in the InfoCache. As not all opcodes are interesting
2504     // to concrete attributes we only cache the ones that are as identified in
2505     // the following switch.
2506     // Note: There are no concrete attributes now so this is initially empty.
2507     switch (I.getOpcode()) {
2508     default:
2509       assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
2510              "New call site/base instruction type needs to be known int the "
2511              "attributor.");
2512       break;
2513     case Instruction::Call:
2514     case Instruction::CallBr:
2515     case Instruction::Invoke:
2516     case Instruction::CleanupRet:
2517     case Instruction::CatchSwitch:
2518     case Instruction::Resume:
2519     case Instruction::Ret:
2520       IsInterestingOpcode = true;
2521     }
2522     if (IsInterestingOpcode)
2523       InstOpcodeMap[I.getOpcode()].push_back(&I);
2524     if (I.mayReadOrWriteMemory())
2525       ReadOrWriteInsts.push_back(&I);
2526 
2527     CallSite CS(&I);
2528     if (CS && CS.getCalledFunction()) {
2529       for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) {
2530         if (!CS.getArgument(i)->getType()->isPointerTy())
2531           continue;
2532 
2533         // Call site argument attribute "non-null".
2534         checkAndRegisterAA<AANonNullCallSiteArgument>(F, *this, Whitelist, I, i,
2535                                                       i);
2536 
2537         // Call site argument attribute "dereferenceable".
2538         checkAndRegisterAA<AADereferenceableCallSiteArgument>(
2539             F, *this, Whitelist, I, i, i);
2540 
2541         // Call site argument attribute "align".
2542         checkAndRegisterAA<AAAlignCallSiteArgument>(F, *this, Whitelist, I, i,
2543                                                     i);
2544       }
2545     }
2546   }
2547 }
2548 
2549 /// Helpers to ease debugging through output streams and print calls.
2550 ///
2551 ///{
2552 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
2553   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
2554 }
2555 
2556 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
2557   switch (AP) {
2558   case IRPosition::IRP_ARGUMENT:
2559     return OS << "arg";
2560   case IRPosition::IRP_CALL_SITE_ARGUMENT:
2561     return OS << "cs_arg";
2562   case IRPosition::IRP_FUNCTION:
2563     return OS << "fn";
2564   case IRPosition::IRP_RETURNED:
2565     return OS << "fn_ret";
2566   }
2567   llvm_unreachable("Unknown attribute position!");
2568 }
2569 
2570 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
2571   const Value *AV = Pos.getAssociatedValue();
2572   return OS << "{" << Pos.getPositionKind() << ":"
2573             << (AV ? AV->getName() : "n/a") << " ["
2574             << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
2575 }
2576 
2577 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
2578   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
2579 }
2580 
2581 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
2582   AA.print(OS);
2583   return OS;
2584 }
2585 
2586 void AbstractAttribute::print(raw_ostream &OS) const {
2587   OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
2588      << "]";
2589 }
2590 ///}
2591 
2592 /// ----------------------------------------------------------------------------
2593 ///                       Pass (Manager) Boilerplate
2594 /// ----------------------------------------------------------------------------
2595 
2596 static bool runAttributorOnModule(Module &M) {
2597   if (DisableAttributor)
2598     return false;
2599 
2600   LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size()
2601                     << " functions.\n");
2602 
2603   // Create an Attributor and initially empty information cache that is filled
2604   // while we identify default attribute opportunities.
2605   Attributor A;
2606   InformationCache InfoCache;
2607 
2608   for (Function &F : M) {
2609     // TODO: Not all attributes require an exact definition. Find a way to
2610     //       enable deduction for some but not all attributes in case the
2611     //       definition might be changed at runtime, see also
2612     //       http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
2613     // TODO: We could always determine abstract attributes and if sufficient
2614     //       information was found we could duplicate the functions that do not
2615     //       have an exact definition.
2616     if (!F.hasExactDefinition()) {
2617       NumFnWithoutExactDefinition++;
2618       continue;
2619     }
2620 
2621     // For now we ignore naked and optnone functions.
2622     if (F.hasFnAttribute(Attribute::Naked) ||
2623         F.hasFnAttribute(Attribute::OptimizeNone))
2624       continue;
2625 
2626     NumFnWithExactDefinition++;
2627 
2628     // Populate the Attributor with abstract attribute opportunities in the
2629     // function and the information cache with IR information.
2630     A.identifyDefaultAbstractAttributes(F, InfoCache);
2631   }
2632 
2633   return A.run(InfoCache) == ChangeStatus::CHANGED;
2634 }
2635 
2636 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
2637   if (runAttributorOnModule(M)) {
2638     // FIXME: Think about passes we will preserve and add them here.
2639     return PreservedAnalyses::none();
2640   }
2641   return PreservedAnalyses::all();
2642 }
2643 
2644 namespace {
2645 
2646 struct AttributorLegacyPass : public ModulePass {
2647   static char ID;
2648 
2649   AttributorLegacyPass() : ModulePass(ID) {
2650     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
2651   }
2652 
2653   bool runOnModule(Module &M) override {
2654     if (skipModule(M))
2655       return false;
2656     return runAttributorOnModule(M);
2657   }
2658 
2659   void getAnalysisUsage(AnalysisUsage &AU) const override {
2660     // FIXME: Think about passes we will preserve and add them here.
2661     AU.setPreservesCFG();
2662   }
2663 };
2664 
2665 } // end anonymous namespace
2666 
2667 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
2668 
2669 char AttributorLegacyPass::ID = 0;
2670 
2671 const char AAReturnedValues::ID = 0;
2672 const char AANoUnwind::ID = 0;
2673 const char AANoSync::ID = 0;
2674 const char AANoFree::ID = 0;
2675 const char AANonNull::ID = 0;
2676 const char AANoRecurse::ID = 0;
2677 const char AAWillReturn::ID = 0;
2678 const char AANoAlias::ID = 0;
2679 const char AANoReturn::ID = 0;
2680 const char AAIsDead::ID = 0;
2681 const char AADereferenceable::ID = 0;
2682 const char AAAlign::ID = 0;
2683 
2684 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
2685                       "Deduce and propagate attributes", false, false)
2686 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
2687                     "Deduce and propagate attributes", false, false)
2688