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/SetVector.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/GlobalsModRef.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Attributes.h"
25 #include "llvm/IR/InstIterator.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <cassert>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "attributor"
34 
35 STATISTIC(NumFnWithExactDefinition,
36           "Number of function with exact definitions");
37 STATISTIC(NumFnWithoutExactDefinition,
38           "Number of function without exact definitions");
39 STATISTIC(NumAttributesTimedOut,
40           "Number of abstract attributes timed out before fixpoint");
41 STATISTIC(NumAttributesValidFixpoint,
42           "Number of abstract attributes in a valid fixpoint state");
43 STATISTIC(NumAttributesManifested,
44           "Number of abstract attributes manifested in IR");
45 STATISTIC(NumFnNoUnwind, "Number of functions marked nounwind");
46 
47 STATISTIC(NumFnUniqueReturned, "Number of function with unique return");
48 STATISTIC(NumFnKnownReturns, "Number of function with known return values");
49 STATISTIC(NumFnArgumentReturned,
50           "Number of function arguments marked returned");
51 
52 // TODO: Determine a good default value.
53 //
54 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
55 // (when run with the first 5 abstract attributes). The results also indicate
56 // that we never reach 32 iterations but always find a fixpoint sooner.
57 //
58 // This will become more evolved once we perform two interleaved fixpoint
59 // iterations: bottom-up and top-down.
60 static cl::opt<unsigned>
61     MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
62                           cl::desc("Maximal number of fixpoint iterations."),
63                           cl::init(32));
64 
65 static cl::opt<bool> DisableAttributor(
66     "attributor-disable", cl::Hidden,
67     cl::desc("Disable the attributor inter-procedural deduction pass."),
68     cl::init(true));
69 
70 static cl::opt<bool> VerifyAttributor(
71     "attributor-verify", cl::Hidden,
72     cl::desc("Verify the Attributor deduction and "
73              "manifestation of attributes -- may issue false-positive errors"),
74     cl::init(false));
75 
76 /// Logic operators for the change status enum class.
77 ///
78 ///{
79 ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
80   return l == ChangeStatus::CHANGED ? l : r;
81 }
82 ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
83   return l == ChangeStatus::UNCHANGED ? l : r;
84 }
85 ///}
86 
87 /// Helper to adjust the statistics.
88 static void bookkeeping(AbstractAttribute::ManifestPosition MP,
89                         const Attribute &Attr) {
90   if (!AreStatisticsEnabled())
91     return;
92 
93   if (!Attr.isEnumAttribute())
94     return;
95   switch (Attr.getKindAsEnum()) {
96   case Attribute::NoUnwind:
97     NumFnNoUnwind++;
98     return;
99   case Attribute::Returned:
100     NumFnArgumentReturned++;
101     return;
102   default:
103     return;
104   }
105 }
106 
107 template <typename StateTy>
108 using followValueCB_t = std::function<bool(Value *, StateTy &State)>;
109 template <typename StateTy>
110 using visitValueCB_t = std::function<void(Value *, StateTy &State)>;
111 
112 /// Recursively visit all values that might become \p InitV at some point. This
113 /// will be done by looking through cast instructions, selects, phis, and calls
114 /// with the "returned" attribute. The callback \p FollowValueCB is asked before
115 /// a potential origin value is looked at. If no \p FollowValueCB is passed, a
116 /// default one is used that will make sure we visit every value only once. Once
117 /// we cannot look through the value any further, the callback \p VisitValueCB
118 /// is invoked and passed the current value and the \p State. To limit how much
119 /// effort is invested, we will never visit more than \p MaxValues values.
120 template <typename StateTy>
121 static bool genericValueTraversal(
122     Value *InitV, StateTy &State, visitValueCB_t<StateTy> &VisitValueCB,
123     followValueCB_t<StateTy> *FollowValueCB = nullptr, int MaxValues = 8) {
124 
125   SmallPtrSet<Value *, 16> Visited;
126   followValueCB_t<bool> DefaultFollowValueCB = [&](Value *Val, bool &) {
127     return Visited.insert(Val).second;
128   };
129 
130   if (!FollowValueCB)
131     FollowValueCB = &DefaultFollowValueCB;
132 
133   SmallVector<Value *, 16> Worklist;
134   Worklist.push_back(InitV);
135 
136   int Iteration = 0;
137   do {
138     Value *V = Worklist.pop_back_val();
139 
140     // Check if we should process the current value. To prevent endless
141     // recursion keep a record of the values we followed!
142     if (!(*FollowValueCB)(V, State))
143       continue;
144 
145     // Make sure we limit the compile time for complex expressions.
146     if (Iteration++ >= MaxValues)
147       return false;
148 
149     // Explicitly look through calls with a "returned" attribute if we do
150     // not have a pointer as stripPointerCasts only works on them.
151     if (V->getType()->isPointerTy()) {
152       V = V->stripPointerCasts();
153     } else {
154       CallSite CS(V);
155       if (CS && CS.getCalledFunction()) {
156         Value *NewV = nullptr;
157         for (Argument &Arg : CS.getCalledFunction()->args())
158           if (Arg.hasReturnedAttr()) {
159             NewV = CS.getArgOperand(Arg.getArgNo());
160             break;
161           }
162         if (NewV) {
163           Worklist.push_back(NewV);
164           continue;
165         }
166       }
167     }
168 
169     // Look through select instructions, visit both potential values.
170     if (auto *SI = dyn_cast<SelectInst>(V)) {
171       Worklist.push_back(SI->getTrueValue());
172       Worklist.push_back(SI->getFalseValue());
173       continue;
174     }
175 
176     // Look through phi nodes, visit all operands.
177     if (auto *PHI = dyn_cast<PHINode>(V)) {
178       Worklist.append(PHI->op_begin(), PHI->op_end());
179       continue;
180     }
181 
182     // Once a leaf is reached we inform the user through the callback.
183     VisitValueCB(V, State);
184   } while (!Worklist.empty());
185 
186   // All values have been visited.
187   return true;
188 }
189 
190 /// Helper to identify the correct offset into an attribute list.
191 static unsigned getAttrIndex(AbstractAttribute::ManifestPosition MP,
192                              unsigned ArgNo = 0) {
193   switch (MP) {
194   case AbstractAttribute::MP_ARGUMENT:
195   case AbstractAttribute::MP_CALL_SITE_ARGUMENT:
196     return ArgNo + AttributeList::FirstArgIndex;
197   case AbstractAttribute::MP_FUNCTION:
198     return AttributeList::FunctionIndex;
199   case AbstractAttribute::MP_RETURNED:
200     return AttributeList::ReturnIndex;
201   }
202   llvm_unreachable("Unknown manifest position!");
203 }
204 
205 /// Return true if \p New is equal or worse than \p Old.
206 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
207   if (!Old.isIntAttribute())
208     return true;
209 
210   return Old.getValueAsInt() >= New.getValueAsInt();
211 }
212 
213 /// Return true if the information provided by \p Attr was added to the
214 /// attribute list \p Attrs. This is only the case if it was not already present
215 /// in \p Attrs at the position describe by \p MP and \p ArgNo.
216 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
217                              AttributeList &Attrs,
218                              AbstractAttribute::ManifestPosition MP,
219                              unsigned ArgNo = 0) {
220   unsigned AttrIdx = getAttrIndex(MP, ArgNo);
221 
222   if (Attr.isEnumAttribute()) {
223     Attribute::AttrKind Kind = Attr.getKindAsEnum();
224     if (Attrs.hasAttribute(AttrIdx, Kind))
225       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
226         return false;
227     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
228     return true;
229   }
230   if (Attr.isStringAttribute()) {
231     StringRef Kind = Attr.getKindAsString();
232     if (Attrs.hasAttribute(AttrIdx, Kind))
233       if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
234         return false;
235     Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
236     return true;
237   }
238 
239   llvm_unreachable("Expected enum or string attribute!");
240 }
241 
242 ChangeStatus AbstractAttribute::update(Attributor &A) {
243   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
244   if (getState().isAtFixpoint())
245     return HasChanged;
246 
247   LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
248 
249   HasChanged = updateImpl(A);
250 
251   LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
252                     << "\n");
253 
254   return HasChanged;
255 }
256 
257 ChangeStatus AbstractAttribute::manifest(Attributor &A) {
258   assert(getState().isValidState() &&
259          "Attempted to manifest an invalid state!");
260   assert(getAssociatedValue() &&
261          "Attempted to manifest an attribute without associated value!");
262 
263   ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
264   SmallVector<Attribute, 4> DeducedAttrs;
265   getDeducedAttributes(DeducedAttrs);
266 
267   Function &ScopeFn = getAnchorScope();
268   LLVMContext &Ctx = ScopeFn.getContext();
269   ManifestPosition MP = getManifestPosition();
270 
271   AttributeList Attrs;
272   SmallVector<unsigned, 4> ArgNos;
273 
274   // In the following some generic code that will manifest attributes in
275   // DeducedAttrs if they improve the current IR. Due to the different
276   // annotation positions we use the underlying AttributeList interface.
277   // Note that MP_CALL_SITE_ARGUMENT can annotate multiple locations.
278 
279   switch (MP) {
280   case MP_ARGUMENT:
281     ArgNos.push_back(cast<Argument>(getAssociatedValue())->getArgNo());
282     Attrs = ScopeFn.getAttributes();
283     break;
284   case MP_FUNCTION:
285   case MP_RETURNED:
286     ArgNos.push_back(0);
287     Attrs = ScopeFn.getAttributes();
288     break;
289   case MP_CALL_SITE_ARGUMENT: {
290     CallSite CS(&getAnchoredValue());
291     for (unsigned u = 0, e = CS.getNumArgOperands(); u != e; u++)
292       if (CS.getArgOperand(u) == getAssociatedValue())
293         ArgNos.push_back(u);
294     Attrs = CS.getAttributes();
295   }
296   }
297 
298   for (const Attribute &Attr : DeducedAttrs) {
299     for (unsigned ArgNo : ArgNos) {
300       if (!addIfNotExistent(Ctx, Attr, Attrs, MP, ArgNo))
301         continue;
302 
303       HasChanged = ChangeStatus::CHANGED;
304       bookkeeping(MP, Attr);
305     }
306   }
307 
308   if (HasChanged == ChangeStatus::UNCHANGED)
309     return HasChanged;
310 
311   switch (MP) {
312   case MP_ARGUMENT:
313   case MP_FUNCTION:
314   case MP_RETURNED:
315     ScopeFn.setAttributes(Attrs);
316     break;
317   case MP_CALL_SITE_ARGUMENT:
318     CallSite(&getAnchoredValue()).setAttributes(Attrs);
319   }
320 
321   return HasChanged;
322 }
323 
324 Function &AbstractAttribute::getAnchorScope() {
325   Value &V = getAnchoredValue();
326   if (isa<Function>(V))
327     return cast<Function>(V);
328   if (isa<Argument>(V))
329     return *cast<Argument>(V).getParent();
330   if (isa<Instruction>(V))
331     return *cast<Instruction>(V).getFunction();
332   llvm_unreachable("No scope for anchored value found!");
333 }
334 
335 const Function &AbstractAttribute::getAnchorScope() const {
336   return const_cast<AbstractAttribute *>(this)->getAnchorScope();
337 }
338 
339 /// -----------------------NoUnwind Function Attribute--------------------------
340 
341 struct AANoUnwindFunction : AANoUnwind, BooleanState {
342 
343   AANoUnwindFunction(Function &F, InformationCache &InfoCache)
344       : AANoUnwind(F, InfoCache) {}
345 
346   /// See AbstractAttribute::getState()
347   /// {
348   AbstractState &getState() override { return *this; }
349   const AbstractState &getState() const override { return *this; }
350   /// }
351 
352   /// See AbstractAttribute::getManifestPosition().
353   virtual ManifestPosition getManifestPosition() const override {
354     return MP_FUNCTION;
355   }
356 
357   virtual const std::string getAsStr() const override {
358     return getAssumed() ? "nounwind" : "may-unwind";
359   }
360 
361   /// See AbstractAttribute::updateImpl(...).
362   virtual ChangeStatus updateImpl(Attributor &A) override;
363 
364   /// See AANoUnwind::isAssumedNoUnwind().
365   virtual bool isAssumedNoUnwind() const override { return getAssumed(); }
366 
367   /// See AANoUnwind::isKnownNoUnwind().
368   virtual bool isKnownNoUnwind() const override { return getKnown(); }
369 };
370 
371 ChangeStatus AANoUnwindFunction::updateImpl(Attributor &A) {
372   Function &F = getAnchorScope();
373 
374   // The map from instruction opcodes to those instructions in the function.
375   auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
376   auto Opcodes = {
377       (unsigned)Instruction::Invoke,      (unsigned)Instruction::CallBr,
378       (unsigned)Instruction::Call,        (unsigned)Instruction::CleanupRet,
379       (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
380 
381   for (unsigned Opcode : Opcodes) {
382     for (Instruction *I : OpcodeInstMap[Opcode]) {
383       if (!I->mayThrow())
384         continue;
385 
386       auto *NoUnwindAA = A.getAAFor<AANoUnwind>(*this, *I);
387 
388       if (!NoUnwindAA || !NoUnwindAA->isAssumedNoUnwind()) {
389         indicatePessimisticFixpoint();
390         return ChangeStatus::CHANGED;
391       }
392     }
393   }
394   return ChangeStatus::UNCHANGED;
395 }
396 
397 /// --------------------- Function Return Values -------------------------------
398 
399 /// "Attribute" that collects all potential returned values and the return
400 /// instructions that they arise from.
401 ///
402 /// If there is a unique returned value R, the manifest method will:
403 ///   - mark R with the "returned" attribute, if R is an argument.
404 class AAReturnedValuesImpl final : public AAReturnedValues, AbstractState {
405 
406   /// Mapping of values potentially returned by the associated function to the
407   /// return instructions that might return them.
408   DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> ReturnedValues;
409 
410   /// State flags
411   ///
412   ///{
413   bool IsFixed;
414   bool IsValidState;
415   bool HasOverdefinedReturnedCalls;
416   ///}
417 
418   /// Collect values that could become \p V in the set \p Values, each mapped to
419   /// \p ReturnInsts.
420   void collectValuesRecursively(
421       Attributor &A, Value *V, SmallPtrSetImpl<ReturnInst *> &ReturnInsts,
422       DenseMap<Value *, SmallPtrSet<ReturnInst *, 2>> &Values) {
423 
424     visitValueCB_t<bool> VisitValueCB = [&](Value *Val, bool &) {
425       assert(!isa<Instruction>(Val) ||
426              &getAnchorScope() == cast<Instruction>(Val)->getFunction());
427       Values[Val].insert(ReturnInsts.begin(), ReturnInsts.end());
428     };
429 
430     bool UnusedBool;
431     bool Success = genericValueTraversal(V, UnusedBool, VisitValueCB);
432 
433     // If we did abort the above traversal we haven't see all the values.
434     // Consequently, we cannot know if the information we would derive is
435     // accurate so we give up early.
436     if (!Success)
437       indicatePessimisticFixpoint();
438   }
439 
440 public:
441   /// See AbstractAttribute::AbstractAttribute(...).
442   AAReturnedValuesImpl(Function &F, InformationCache &InfoCache)
443       : AAReturnedValues(F, InfoCache) {
444     // We do not have an associated argument yet.
445     AssociatedVal = nullptr;
446   }
447 
448   /// See AbstractAttribute::initialize(...).
449   void initialize(Attributor &A) override {
450     // Reset the state.
451     AssociatedVal = nullptr;
452     IsFixed = false;
453     IsValidState = true;
454     HasOverdefinedReturnedCalls = false;
455     ReturnedValues.clear();
456 
457     Function &F = cast<Function>(getAnchoredValue());
458 
459     // The map from instruction opcodes to those instructions in the function.
460     auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
461 
462     // Look through all arguments, if one is marked as returned we are done.
463     for (Argument &Arg : F.args()) {
464       if (Arg.hasReturnedAttr()) {
465 
466         auto &ReturnInstSet = ReturnedValues[&Arg];
467         for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
468           ReturnInstSet.insert(cast<ReturnInst>(RI));
469 
470         indicateOptimisticFixpoint();
471         return;
472       }
473     }
474 
475     // If no argument was marked as returned we look at all return instructions
476     // and collect potentially returned values.
477     for (Instruction *RI : OpcodeInstMap[Instruction::Ret]) {
478       SmallPtrSet<ReturnInst *, 1> RISet({cast<ReturnInst>(RI)});
479       collectValuesRecursively(A, cast<ReturnInst>(RI)->getReturnValue(), RISet,
480                                ReturnedValues);
481     }
482   }
483 
484   /// See AbstractAttribute::manifest(...).
485   virtual ChangeStatus manifest(Attributor &A) override;
486 
487   /// See AbstractAttribute::getState(...).
488   virtual AbstractState &getState() override { return *this; }
489 
490   /// See AbstractAttribute::getState(...).
491   virtual const AbstractState &getState() const override { return *this; }
492 
493   /// See AbstractAttribute::getManifestPosition().
494   virtual ManifestPosition getManifestPosition() const override {
495     return MP_ARGUMENT;
496   }
497 
498   /// See AbstractAttribute::updateImpl(Attributor &A).
499   virtual ChangeStatus updateImpl(Attributor &A) override;
500 
501   /// Return the number of potential return values, -1 if unknown.
502   size_t getNumReturnValues() const {
503     return isValidState() ? ReturnedValues.size() : -1;
504   }
505 
506   /// Return an assumed unique return value if a single candidate is found. If
507   /// there cannot be one, return a nullptr. If it is not clear yet, return the
508   /// Optional::NoneType.
509   Optional<Value *> getAssumedUniqueReturnValue() const;
510 
511   /// See AbstractState::checkForallReturnedValues(...).
512   virtual bool
513   checkForallReturnedValues(std::function<bool(Value &)> &Pred) const override;
514 
515   /// Pretty print the attribute similar to the IR representation.
516   virtual const std::string getAsStr() const override;
517 
518   /// See AbstractState::isAtFixpoint().
519   bool isAtFixpoint() const override { return IsFixed; }
520 
521   /// See AbstractState::isValidState().
522   bool isValidState() const override { return IsValidState; }
523 
524   /// See AbstractState::indicateOptimisticFixpoint(...).
525   void indicateOptimisticFixpoint() override {
526     IsFixed = true;
527     IsValidState &= true;
528   }
529   void indicatePessimisticFixpoint() override {
530     IsFixed = true;
531     IsValidState = false;
532   }
533 };
534 
535 ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
536   ChangeStatus Changed = ChangeStatus::UNCHANGED;
537 
538   // Bookkeeping.
539   assert(isValidState());
540   NumFnKnownReturns++;
541 
542   // Check if we have an assumed unique return value that we could manifest.
543   Optional<Value *> UniqueRV = getAssumedUniqueReturnValue();
544 
545   if (!UniqueRV.hasValue() || !UniqueRV.getValue())
546     return Changed;
547 
548   // Bookkeeping.
549   NumFnUniqueReturned++;
550 
551   // If the assumed unique return value is an argument, annotate it.
552   if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
553     AssociatedVal = UniqueRVArg;
554     Changed = AbstractAttribute::manifest(A) | Changed;
555   }
556 
557   return Changed;
558 }
559 
560 const std::string AAReturnedValuesImpl::getAsStr() const {
561   return (isAtFixpoint() ? "returns(#" : "may-return(#") +
562          (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
563 }
564 
565 Optional<Value *> AAReturnedValuesImpl::getAssumedUniqueReturnValue() const {
566   // If checkForallReturnedValues provides a unique value, ignoring potential
567   // undef values that can also be present, it is assumed to be the actual
568   // return value and forwarded to the caller of this method. If there are
569   // multiple, a nullptr is returned indicating there cannot be a unique
570   // returned value.
571   Optional<Value *> UniqueRV;
572 
573   std::function<bool(Value &)> Pred = [&](Value &RV) -> bool {
574     // If we found a second returned value and neither the current nor the saved
575     // one is an undef, there is no unique returned value. Undefs are special
576     // since we can pretend they have any value.
577     if (UniqueRV.hasValue() && UniqueRV != &RV &&
578         !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
579       UniqueRV = nullptr;
580       return false;
581     }
582 
583     // Do not overwrite a value with an undef.
584     if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
585       UniqueRV = &RV;
586 
587     return true;
588   };
589 
590   if (!checkForallReturnedValues(Pred))
591     UniqueRV = nullptr;
592 
593   return UniqueRV;
594 }
595 
596 bool AAReturnedValuesImpl::checkForallReturnedValues(
597     std::function<bool(Value &)> &Pred) const {
598   if (!isValidState())
599     return false;
600 
601   // Check all returned values but ignore call sites as long as we have not
602   // encountered an overdefined one during an update.
603   for (auto &It : ReturnedValues) {
604     Value *RV = It.first;
605 
606     ImmutableCallSite ICS(RV);
607     if (ICS && !HasOverdefinedReturnedCalls)
608       continue;
609 
610     if (!Pred(*RV))
611       return false;
612   }
613 
614   return true;
615 }
616 
617 ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
618 
619   // Check if we know of any values returned by the associated function,
620   // if not, we are done.
621   if (getNumReturnValues() == 0) {
622     indicateOptimisticFixpoint();
623     return ChangeStatus::UNCHANGED;
624   }
625 
626   // Check if any of the returned values is a call site we can refine.
627   decltype(ReturnedValues) AddRVs;
628   bool HasCallSite = false;
629 
630   // Look at all returned call sites.
631   for (auto &It : ReturnedValues) {
632     SmallPtrSet<ReturnInst *, 2> &ReturnInsts = It.second;
633     Value *RV = It.first;
634     LLVM_DEBUG(dbgs() << "[AAReturnedValues] Potentially returned value " << *RV
635                       << "\n");
636 
637     // Only call sites can change during an update, ignore the rest.
638     CallSite RetCS(RV);
639     if (!RetCS)
640       continue;
641 
642     // For now, any call site we see will prevent us from directly fixing the
643     // state. However, if the information on the callees is fixed, the call
644     // sites will be removed and we will fix the information for this state.
645     HasCallSite = true;
646 
647     // Try to find a assumed unique return value for the called function.
648     auto *RetCSAA = A.getAAFor<AAReturnedValuesImpl>(*this, *RV);
649     if (!RetCSAA || !RetCSAA->isValidState()) {
650       HasOverdefinedReturnedCalls = true;
651       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned call site (" << *RV
652                         << ") with " << (RetCSAA ? "invalid" : "no")
653                         << " associated state\n");
654       continue;
655     }
656 
657     // Try to find a assumed unique return value for the called function.
658     Optional<Value *> AssumedUniqueRV = RetCSAA->getAssumedUniqueReturnValue();
659 
660     // If no assumed unique return value was found due to the lack of
661     // candidates, we may need to resolve more calls (through more update
662     // iterations) or the called function will not return. Either way, we simply
663     // stick with the call sites as return values. Because there were not
664     // multiple possibilities, we do not treat it as overdefined.
665     if (!AssumedUniqueRV.hasValue())
666       continue;
667 
668     // If multiple, non-refinable values were found, there cannot be a unique
669     // return value for the called function. The returned call is overdefined!
670     if (!AssumedUniqueRV.getValue()) {
671       HasOverdefinedReturnedCalls = true;
672       LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned call site has multiple "
673                            "potentially returned values\n");
674       continue;
675     }
676 
677     LLVM_DEBUG({
678       bool UniqueRVIsKnown = RetCSAA->isAtFixpoint();
679       dbgs() << "[AAReturnedValues] Returned call site "
680              << (UniqueRVIsKnown ? "known" : "assumed")
681              << " unique return value: " << *AssumedUniqueRV << "\n";
682     });
683 
684     // The assumed unique return value.
685     Value *AssumedRetVal = AssumedUniqueRV.getValue();
686 
687     // If the assumed unique return value is an argument, lookup the matching
688     // call site operand and recursively collect new returned values.
689     // If it is not an argument, it is just put into the set of returned values
690     // as we would have already looked through casts, phis, and similar values.
691     if (Argument *AssumedRetArg = dyn_cast<Argument>(AssumedRetVal))
692       collectValuesRecursively(A,
693                                RetCS.getArgOperand(AssumedRetArg->getArgNo()),
694                                ReturnInsts, AddRVs);
695     else
696       AddRVs[AssumedRetVal].insert(ReturnInsts.begin(), ReturnInsts.end());
697   }
698 
699   // Keep track of any change to trigger updates on dependent attributes.
700   ChangeStatus Changed = ChangeStatus::UNCHANGED;
701 
702   for (auto &It : AddRVs) {
703     assert(!It.second.empty() && "Entry does not add anything.");
704     auto &ReturnInsts = ReturnedValues[It.first];
705     for (ReturnInst *RI : It.second)
706       if (ReturnInsts.insert(RI).second) {
707         LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
708                           << *It.first << " => " << *RI << "\n");
709         Changed = ChangeStatus::CHANGED;
710       }
711   }
712 
713   // If there is no call site in the returned values we are done.
714   if (!HasCallSite) {
715     indicateOptimisticFixpoint();
716     return ChangeStatus::CHANGED;
717   }
718 
719   return Changed;
720 }
721 
722 /// ----------------------------------------------------------------------------
723 ///                               Attributor
724 /// ----------------------------------------------------------------------------
725 
726 ChangeStatus Attributor::run() {
727   // Initialize all abstract attributes.
728   for (AbstractAttribute *AA : AllAbstractAttributes)
729     AA->initialize(*this);
730 
731   LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
732                     << AllAbstractAttributes.size()
733                     << " abstract attributes.\n");
734 
735   // Now that all abstract attributes are collected and initialized we start
736   // the abstract analysis.
737 
738   unsigned IterationCounter = 1;
739 
740   SmallVector<AbstractAttribute *, 64> ChangedAAs;
741   SetVector<AbstractAttribute *> Worklist;
742   Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
743 
744   do {
745     LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
746                       << ", Worklist size: " << Worklist.size() << "\n");
747 
748     // Add all abstract attributes that are potentially dependent on one that
749     // changed to the work list.
750     for (AbstractAttribute *ChangedAA : ChangedAAs) {
751       auto &QuerriedAAs = QueryMap[ChangedAA];
752       Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end());
753     }
754 
755     // Reset the changed set.
756     ChangedAAs.clear();
757 
758     // Update all abstract attribute in the work list and record the ones that
759     // changed.
760     for (AbstractAttribute *AA : Worklist)
761       if (AA->update(*this) == ChangeStatus::CHANGED)
762         ChangedAAs.push_back(AA);
763 
764     // Reset the work list and repopulate with the changed abstract attributes.
765     // Note that dependent ones are added above.
766     Worklist.clear();
767     Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
768 
769   } while (!Worklist.empty() && ++IterationCounter < MaxFixpointIterations);
770 
771   LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
772                     << IterationCounter << "/" << MaxFixpointIterations
773                     << " iterations\n");
774 
775   bool FinishedAtFixpoint = Worklist.empty();
776 
777   // Reset abstract arguments not settled in a sound fixpoint by now. This
778   // happens when we stopped the fixpoint iteration early. Note that only the
779   // ones marked as "changed" *and* the ones transitively depending on them
780   // need to be reverted to a pessimistic state. Others might not be in a
781   // fixpoint state but we can use the optimistic results for them anyway.
782   SmallPtrSet<AbstractAttribute *, 32> Visited;
783   for (unsigned u = 0; u < ChangedAAs.size(); u++) {
784     AbstractAttribute *ChangedAA = ChangedAAs[u];
785     if (!Visited.insert(ChangedAA).second)
786       continue;
787 
788     AbstractState &State = ChangedAA->getState();
789     if (!State.isAtFixpoint()) {
790       State.indicatePessimisticFixpoint();
791 
792       NumAttributesTimedOut++;
793     }
794 
795     auto &QuerriedAAs = QueryMap[ChangedAA];
796     ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end());
797   }
798 
799   LLVM_DEBUG({
800     if (!Visited.empty())
801       dbgs() << "\n[Attributor] Finalized " << Visited.size()
802              << " abstract attributes.\n";
803   });
804 
805   unsigned NumManifested = 0;
806   unsigned NumAtFixpoint = 0;
807   ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
808   for (AbstractAttribute *AA : AllAbstractAttributes) {
809     AbstractState &State = AA->getState();
810 
811     // If there is not already a fixpoint reached, we can now take the
812     // optimistic state. This is correct because we enforced a pessimistic one
813     // on abstract attributes that were transitively dependent on a changed one
814     // already above.
815     if (!State.isAtFixpoint())
816       State.indicateOptimisticFixpoint();
817 
818     // If the state is invalid, we do not try to manifest it.
819     if (!State.isValidState())
820       continue;
821 
822     // Manifest the state and record if we changed the IR.
823     ChangeStatus LocalChange = AA->manifest(*this);
824     ManifestChange = ManifestChange | LocalChange;
825 
826     NumAtFixpoint++;
827     NumManifested += (LocalChange == ChangeStatus::CHANGED);
828   }
829 
830   (void)NumManifested;
831   (void)NumAtFixpoint;
832   LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
833                     << " arguments while " << NumAtFixpoint
834                     << " were in a valid fixpoint state\n");
835 
836   // If verification is requested, we finished this run at a fixpoint, and the
837   // IR was changed, we re-run the whole fixpoint analysis, starting at
838   // re-initialization of the arguments. This re-run should not result in an IR
839   // change. Though, the (virtual) state of attributes at the end of the re-run
840   // might be more optimistic than the known state or the IR state if the better
841   // state cannot be manifested.
842   if (VerifyAttributor && FinishedAtFixpoint &&
843       ManifestChange == ChangeStatus::CHANGED) {
844     VerifyAttributor = false;
845     ChangeStatus VerifyStatus = run();
846     if (VerifyStatus != ChangeStatus::UNCHANGED)
847       llvm_unreachable(
848           "Attributor verification failed, re-run did result in an IR change "
849           "even after a fixpoint was reached in the original run. (False "
850           "positives possible!)");
851     VerifyAttributor = true;
852   }
853 
854   NumAttributesManifested += NumManifested;
855   NumAttributesValidFixpoint += NumAtFixpoint;
856 
857   return ManifestChange;
858 }
859 
860 void Attributor::identifyDefaultAbstractAttributes(
861     Function &F, InformationCache &InfoCache,
862     DenseSet</* Attribute::AttrKind */ unsigned> *Whitelist) {
863 
864   // Every function can be nounwind.
865   registerAA(*new AANoUnwindFunction(F, InfoCache));
866 
867   // Return attributes are only appropriate if the return type is non void.
868   Type *ReturnType = F.getReturnType();
869   if (!ReturnType->isVoidTy()) {
870     // Argument attribute "returned" --- Create only one per function even
871     // though it is an argument attribute.
872     if (!Whitelist || Whitelist->count(AAReturnedValues::ID))
873       registerAA(*new AAReturnedValuesImpl(F, InfoCache));
874   }
875 
876   // Walk all instructions to find more attribute opportunities and also
877   // interesting instructions that might be queried by abstract attributes
878   // during their initialization or update.
879   auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
880   auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
881 
882   for (Instruction &I : instructions(&F)) {
883     bool IsInterestingOpcode = false;
884 
885     // To allow easy access to all instructions in a function with a given
886     // opcode we store them in the InfoCache. As not all opcodes are interesting
887     // to concrete attributes we only cache the ones that are as identified in
888     // the following switch.
889     // Note: There are no concrete attributes now so this is initially empty.
890     switch (I.getOpcode()) {
891     default:
892       assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
893              "New call site/base instruction type needs to be known int the "
894              "attributor.");
895       break;
896     case Instruction::Call:
897     case Instruction::CallBr:
898     case Instruction::Invoke:
899     case Instruction::CleanupRet:
900     case Instruction::CatchSwitch:
901     case Instruction::Resume:
902     case Instruction::Ret:
903       IsInterestingOpcode = true;
904     }
905     if (IsInterestingOpcode)
906       InstOpcodeMap[I.getOpcode()].push_back(&I);
907     if (I.mayReadOrWriteMemory())
908       ReadOrWriteInsts.push_back(&I);
909   }
910 }
911 
912 /// Helpers to ease debugging through output streams and print calls.
913 ///
914 ///{
915 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
916   return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
917 }
918 
919 raw_ostream &llvm::operator<<(raw_ostream &OS,
920                               AbstractAttribute::ManifestPosition AP) {
921   switch (AP) {
922   case AbstractAttribute::MP_ARGUMENT:
923     return OS << "arg";
924   case AbstractAttribute::MP_CALL_SITE_ARGUMENT:
925     return OS << "cs_arg";
926   case AbstractAttribute::MP_FUNCTION:
927     return OS << "fn";
928   case AbstractAttribute::MP_RETURNED:
929     return OS << "fn_ret";
930   }
931   llvm_unreachable("Unknown attribute position!");
932 }
933 
934 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
935   return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
936 }
937 
938 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
939   AA.print(OS);
940   return OS;
941 }
942 
943 void AbstractAttribute::print(raw_ostream &OS) const {
944   OS << "[" << getManifestPosition() << "][" << getAsStr() << "]["
945      << AnchoredVal.getName() << "]";
946 }
947 ///}
948 
949 /// ----------------------------------------------------------------------------
950 ///                       Pass (Manager) Boilerplate
951 /// ----------------------------------------------------------------------------
952 
953 static bool runAttributorOnModule(Module &M) {
954   if (DisableAttributor)
955     return false;
956 
957   LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size()
958                     << " functions.\n");
959 
960   // Create an Attributor and initially empty information cache that is filled
961   // while we identify default attribute opportunities.
962   Attributor A;
963   InformationCache InfoCache;
964 
965   for (Function &F : M) {
966     // TODO: Not all attributes require an exact definition. Find a way to
967     //       enable deduction for some but not all attributes in case the
968     //       definition might be changed at runtime, see also
969     //       http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
970     // TODO: We could always determine abstract attributes and if sufficient
971     //       information was found we could duplicate the functions that do not
972     //       have an exact definition.
973     if (!F.hasExactDefinition()) {
974       NumFnWithoutExactDefinition++;
975       continue;
976     }
977 
978     // For now we ignore naked and optnone functions.
979     if (F.hasFnAttribute(Attribute::Naked) ||
980         F.hasFnAttribute(Attribute::OptimizeNone))
981       continue;
982 
983     NumFnWithExactDefinition++;
984 
985     // Populate the Attributor with abstract attribute opportunities in the
986     // function and the information cache with IR information.
987     A.identifyDefaultAbstractAttributes(F, InfoCache);
988   }
989 
990   return A.run() == ChangeStatus::CHANGED;
991 }
992 
993 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
994   if (runAttributorOnModule(M)) {
995     // FIXME: Think about passes we will preserve and add them here.
996     return PreservedAnalyses::none();
997   }
998   return PreservedAnalyses::all();
999 }
1000 
1001 namespace {
1002 
1003 struct AttributorLegacyPass : public ModulePass {
1004   static char ID;
1005 
1006   AttributorLegacyPass() : ModulePass(ID) {
1007     initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
1008   }
1009 
1010   bool runOnModule(Module &M) override {
1011     if (skipModule(M))
1012       return false;
1013     return runAttributorOnModule(M);
1014   }
1015 
1016   void getAnalysisUsage(AnalysisUsage &AU) const override {
1017     // FIXME: Think about passes we will preserve and add them here.
1018     AU.setPreservesCFG();
1019   }
1020 };
1021 
1022 } // end anonymous namespace
1023 
1024 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
1025 
1026 char AttributorLegacyPass::ID = 0;
1027 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
1028                       "Deduce and propagate attributes", false, false)
1029 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
1030                     "Deduce and propagate attributes", false, false)
1031