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