llvm.org GIT mirror llvm / f788836
[Attributor] Use IRPosition consistently Summary: The next attempt to clean up the Attributor interface before we grow it further. Before, we used a combination of two values (associated + anchor) and an argument number (or -1) to determine a location. This was very fragile. The new system uses exclusively IR positions and we restrict the generation of IR positions to special constructor methods that verify internal constraints we have. This will catch misuse early. The auto-conversion, e.g., in getAAFor, is now performed through the SubsumingPositionIterator. This iterator takes an IR position and allows to visit all IR positions that "subsume" the given one, e.g., function attributes "subsume" argument attributes of that function. For a detailed breakdown see the class comment of SubsumingPositionIterator. This patch also introduces the IRPosition::getAttrs() to extract IR attributes at a certain position. The method knows how to look up in different positions that are equivalent, e.g., the argument position for call site arguments. We also introduce three new positions kinds such that we have all IR positions where attributes can be placed and one for "floating" values. Reviewers: sstefan1, uenoku Subscribers: hiraditya, bollu, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D65977 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368919 91177308-0d34-0410-b5e6-96231b3b80d8 Johannes Doerfert a month ago
2 changed file(s) with 812 addition(s) and 486 deletion(s). Raw diff Collapse all Expand all
118118 ChangeStatus operator&(ChangeStatus l, ChangeStatus r);
119119 ///}
120120
121 /// Helper to describe and deal with positions in the LLVM-IR.
122 ///
123 /// A position in the IR is described by an anchor value and an "offset" that
124 /// could be the argument number, for call sites and arguments, or an indicator
125 /// of the "position kind". The kinds, specified in the Kind enum below, include
126 /// the locations in the attribute list, i.a., function scope and return value,
127 /// as well as a distinction between call sites and functions. Finally, there
128 /// are floating values that do not have a corresponding attribute list
129 /// position.
130 struct IRPosition {
131 virtual ~IRPosition() {}
132
133 /// The positions we distinguish in the IR.
134 ///
135 /// The values are chosen such that the KindOrArgNo member has a value >= 1
136 /// if it is an argument or call site argument while a value < 1 indicates the
137 /// respective kind of that value.
138 enum Kind : int {
139 IRP_INVALID = -6, ///< An invalid position.
140 IRP_FLOAT = -5, ///< A position that is not associated with a spot suitable
141 ///< for attributes. This could be any value or instruction.
142 IRP_RETURNED = -4, ///< An attribute for the function return value.
143 IRP_CALL_SITE_RETURNED = -3, ///< An attribute for a call site return value.
144 IRP_FUNCTION = -2, ///< An attribute for a function (scope).
145 IRP_CALL_SITE = -1, ///< An attribute for a call site (function scope).
146 IRP_ARGUMENT = 0, ///< An attribute for a function argument.
147 IRP_CALL_SITE_ARGUMENT = 1, ///< An attribute for a call site argument.
148 };
149
150 /// Default constructor available to create invalid positions implicitly. All
151 /// other positions need to be created explicitly through the appropriate
152 /// static member function.
153 IRPosition() : AnchorVal(nullptr), KindOrArgNo(IRP_INVALID) { verify(); }
154
155 /// Create a position describing the value of \p V.
156 static const IRPosition value(const Value &V) {
157 if (auto *Arg = dyn_cast(&V))
158 return IRPosition::argument(*Arg);
159 if (auto *CB = dyn_cast(&V))
160 return IRPosition::callsite_returned(*CB);
161 return IRPosition(const_cast(V), IRP_FLOAT);
162 }
163
164 /// Create a position describing the function scope of \p F.
165 static const IRPosition function(const Function &F) {
166 return IRPosition(const_cast(F), IRP_FUNCTION);
167 }
168
169 /// Create a position describing the returned value of \p F.
170 static const IRPosition returned(const Function &F) {
171 return IRPosition(const_cast(F), IRP_RETURNED);
172 }
173
174 /// Create a position describing the argument \p Arg.
175 static const IRPosition argument(const Argument &Arg) {
176 return IRPosition(const_cast(Arg), Kind(Arg.getArgNo()));
177 }
178
179 /// Create a position describing the function scope of \p CB.
180 static const IRPosition callsite_function(const CallBase &CB) {
181 return IRPosition(const_cast(CB), IRP_CALL_SITE);
182 }
183
184 /// Create a position describing the returned value of \p CB.
185 static const IRPosition callsite_returned(const CallBase &CB) {
186 return IRPosition(const_cast(CB), IRP_CALL_SITE_RETURNED);
187 }
188
189 /// Create a position describing the argument of \p CB at position \p ArgNo.
190 static const IRPosition callsite_argument(const CallBase &CB,
191 unsigned ArgNo) {
192 return IRPosition(const_cast(CB), Kind(ArgNo));
193 }
194
195 /// Create a position describing the function scope of \p ICS.
196 static const IRPosition callsite_function(ImmutableCallSite ICS) {
197 return IRPosition::callsite_function(cast(*ICS.getInstruction()));
198 }
199
200 /// Create a position describing the returned value of \p ICS.
201 static const IRPosition callsite_returned(ImmutableCallSite ICS) {
202 return IRPosition::callsite_returned(cast(*ICS.getInstruction()));
203 }
204
205 /// Create a position describing the argument of \p ICS at position \p ArgNo.
206 static const IRPosition callsite_argument(ImmutableCallSite ICS,
207 unsigned ArgNo) {
208 return IRPosition::callsite_argument(cast(*ICS.getInstruction()),
209 ArgNo);
210 }
211
212 /// Create a position with function scope matching the "context" of \p IRP.
213 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
214 /// will be a call site position, otherwise the function position of the
215 /// associated function.
216 static const IRPosition function_scope(const IRPosition &IRP) {
217 if (IRP.isAnyCallSitePosition()) {
218 return IRPosition::callsite_function(
219 cast(IRP.getAnchorValue()));
220 }
221 assert(IRP.getAssociatedFunction());
222 return IRPosition::function(*IRP.getAssociatedFunction());
223 }
224
225 bool operator==(const IRPosition &RHS) const {
226 return (AnchorVal == RHS.AnchorVal) && (KindOrArgNo == RHS.KindOrArgNo);
227 }
228 bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
229
230 /// Return the value this abstract attribute is anchored with.
231 ///
232 /// The anchor value might not be the associated value if the latter is not
233 /// sufficient to determine where arguments will be manifested. This is, so
234 /// far, only the case for call site arguments as the value is not sufficient
235 /// to pinpoint them. Instead, we can use the call site as an anchor.
236 ///
237 ///{
238 Value &getAnchorValue() {
239 assert(KindOrArgNo != IRP_INVALID &&
240 "Invalid position does not have an anchor value!");
241 return *AnchorVal;
242 }
243 const Value &getAnchorValue() const {
244 return const_cast(this)->getAnchorValue();
245 }
246 ///}
247
248 /// Return the associated function, if any.
249 ///
250 ///{
251 Function *getAssociatedFunction() {
252 if (auto *CB = dyn_cast(AnchorVal))
253 return CB->getCalledFunction();
254 assert(KindOrArgNo != IRP_INVALID &&
255 "Invalid position does not have an anchor scope!");
256 Value &V = getAnchorValue();
257 if (isa(V))
258 return &cast(V);
259 if (isa(V))
260 return cast(V).getParent();
261 if (isa(V))
262 return cast(V).getFunction();
263 return nullptr;
264 }
265 const Function *getAssociatedFunction() const {
266 return const_cast(this)->getAssociatedFunction();
267 }
268 ///}
269
270 /// Return the Function surrounding the anchor value.
271 ///
272 ///{
273 Function &getAnchorScope() {
274 Value &V = getAnchorValue();
275 if (isa(V))
276 return cast(V);
277 if (isa(V))
278 return *cast(V).getParent();
279 if (isa(V))
280 return *cast(V).getFunction();
281 llvm_unreachable("No anchor scope");
282 }
283 const Function &getAnchorScope() const {
284 return const_cast(this)->getAnchorScope();
285 }
286 ///}
287
288 /// Return the context instruction, if any.
289 ///
290 ///{
291 Instruction *getCtxI() {
292 Value &V = getAnchorValue();
293 if (auto *I = dyn_cast(&V))
294 return I;
295 if (auto *Arg = dyn_cast(&V))
296 if (!Arg->getParent()->isDeclaration())
297 return &Arg->getParent()->getEntryBlock().front();
298 return nullptr;
299 }
300 const Instruction *getCtxI() const {
301 return const_cast(this)->getCtxI();
302 }
303 ///}
304
305 /// Return the value this abstract attribute is associated with.
306 ///
307 ///{
308 Value &getAssociatedValue() {
309 assert(KindOrArgNo != IRP_INVALID &&
310 "Invalid position does not have an associated value!");
311 if (getArgNo() < 0 || isa(AnchorVal))
312 return *AnchorVal;
313 assert(isa(AnchorVal) && "Expected a call base!");
314 return *cast(AnchorVal)->getArgOperand(getArgNo());
315 }
316 const Value &getAssociatedValue() const {
317 return const_cast(this)->getAssociatedValue();
318 }
319 ///}
320
321 /// Return the argument number of the associated value if it is an argument or
322 /// call site argument, otherwise a negative value.
323 int getArgNo() const { return KindOrArgNo; }
324
325 /// Return the index in the attribute list for this position.
326 unsigned getAttrIdx() const {
327 switch (getPositionKind()) {
328 case IRPosition::IRP_INVALID:
329 case IRPosition::IRP_FLOAT:
330 break;
331 case IRPosition::IRP_FUNCTION:
332 case IRPosition::IRP_CALL_SITE:
333 return AttributeList::FunctionIndex;
334 case IRPosition::IRP_RETURNED:
335 case IRPosition::IRP_CALL_SITE_RETURNED:
336 return AttributeList::ReturnIndex;
337 case IRPosition::IRP_ARGUMENT:
338 case IRPosition::IRP_CALL_SITE_ARGUMENT:
339 return KindOrArgNo + AttributeList::FirstArgIndex;
340 }
341 llvm_unreachable(
342 "There is no attribute index for a floating or invalid position!");
343 }
344
345 /// Return the associated position kind.
346 Kind getPositionKind() const {
347 if (getArgNo() >= 0) {
348 assert(((isa(getAnchorValue()) &&
349 isa(getAssociatedValue())) ||
350 isa(getAnchorValue())) &&
351 "Expected argument or call base due to argument number!");
352 if (isa(getAnchorValue()))
353 return IRP_CALL_SITE_ARGUMENT;
354 return IRP_ARGUMENT;
355 }
356
357 assert(KindOrArgNo < 0 &&
358 "Expected (call site) arguments to never reach this point!");
359 assert(!isa(getAnchorValue()) &&
360 "Expected arguments to have an associated argument position!");
361 return Kind(KindOrArgNo);
362 }
363
364 /// TODO: Figure out if the attribute related helper functions should live
365 /// here or somewhere else.
366
367 /// Return true if any kind in \p AKs existing in the IR at a position that
368 /// will affect this one. See also getAttrs(...).
369 bool hasAttr(ArrayRef AKs) const;
370
371 /// Return the attributes of any kind in \p AKs existing in the IR at a
372 /// position that will affect this one. While each position can only have a
373 /// single attribute of any kind in \p AKs, there are "subsuming" positions
374 /// that could have an attribute as well. This method returns all attributes
375 /// found in \p Attrs.
376 void getAttrs(ArrayRef AKs,
377 SmallVectorImpl &Attrs) const;
378
379 /// Return the attribute of kind \p AK existing in the IR at this position.
380 Attribute getAttr(Attribute::AttrKind AK) const {
381 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
382 return Attribute();
383
384 AttributeList AttrList;
385 if (ImmutableCallSite ICS = ImmutableCallSite(&getAnchorValue()))
386 AttrList = ICS.getAttributes();
387 else
388 AttrList = getAssociatedFunction()->getAttributes();
389
390 if (AttrList.hasAttribute(getAttrIdx(), AK))
391 return AttrList.getAttribute(getAttrIdx(), AK);
392 return Attribute();
393 }
394
395 bool isAnyCallSitePosition() const {
396 switch (getPositionKind()) {
397 case IRPosition::IRP_CALL_SITE:
398 case IRPosition::IRP_CALL_SITE_RETURNED:
399 case IRPosition::IRP_CALL_SITE_ARGUMENT:
400 return true;
401 default:
402 return false;
403 }
404 }
405
406 /// Special DenseMap key values.
407 ///
408 ///{
409 static const IRPosition EmptyKey;
410 static const IRPosition TombstoneKey;
411 ///}
412
413 private:
414 /// Private constructor for special values only!
415 explicit IRPosition(int KindOrArgNo)
416 : AnchorVal(0), KindOrArgNo(KindOrArgNo) {}
417
418 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
419 explicit IRPosition(Value &AnchorVal, Kind PK)
420 : AnchorVal(&AnchorVal), KindOrArgNo(PK) {
421 verify();
422 }
423
424 /// Verify internal invariants.
425 void verify();
426
427 /// The value this position is anchored at.
428 Value *AnchorVal;
429
430 /// The argument number, if non-negative, or the position "kind".
431 int KindOrArgNo;
432 };
433
434 /// Helper that allows IRPosition as a key in a DenseMap.
435 template <> struct DenseMapInfo {
436 static inline IRPosition getEmptyKey() { return IRPosition::EmptyKey; }
437 static inline IRPosition getTombstoneKey() {
438 return IRPosition::TombstoneKey;
439 }
440 static unsigned getHashValue(const IRPosition &IRP) {
441 return (DenseMapInfo::getHashValue(&IRP.getAnchorValue()) << 4) ^
442 (unsigned(IRP.getArgNo()));
443 }
444 static bool isEqual(const IRPosition &LHS, const IRPosition &RHS) {
445 return LHS == RHS;
446 }
447 };
448
449 /// A visitor class for IR positions.
450 ///
451 /// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
452 /// positions" wrt. attributes/information. Thus, if a piece of information
453 /// holds for a subsuming position, it also holds for the position P.
454 ///
455 /// The subsuming positions always include the initial position and then,
456 /// depending on the position kind, additionally the following ones:
457 /// - for IRP_RETURNED:
458 /// - the function (IRP_FUNCTION)
459 /// - for IRP_ARGUMENT:
460 /// - the function (IRP_FUNCTION)
461 /// - for IRP_CALL_SITE:
462 /// - the callee (IRP_FUNCTION), if known
463 /// - for IRP_CALL_SITE_RETURNED:
464 /// - the callee (IRP_RETURNED), if known
465 /// - the call site (IRP_FUNCTION)
466 /// - the callee (IRP_FUNCTION), if known
467 /// - for IRP_CALL_SITE_ARGUMENT:
468 /// - the argument of the callee (IRP_ARGUMENT), if known
469 /// - the callee (IRP_FUNCTION), if known
470 /// - the position the call site argument is associated with if it is not
471 /// anchored to the call site, e.g., if it is an arugment then the argument
472 /// (IRP_ARGUMENT)
473 class SubsumingPositionIterator {
474 SmallVector IRPositions;
475 using iterator = decltype(IRPositions)::iterator;
476
477 public:
478 SubsumingPositionIterator(const IRPosition &IRP);
479 iterator begin() { return IRPositions.begin(); }
480 iterator end() { return IRPositions.end(); }
481 };
482
121483 /// Data structure to hold cached (LLVM-IR) information.
122484 ///
123485 /// All attributes are given an InformationCache object at creation time to
211573 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
212574 ChangeStatus run();
213575
214 /// Lookup an abstract attribute of type \p AAType anchored at value \p V and
215 /// argument number \p ArgNo. If no attribute is found and \p V is a call base
216 /// instruction, the called function is tried as a value next. Thus, the
217 /// returned abstract attribute might be anchored at the callee of \p V.
576 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
577 /// no abstract attribute is found equivalent positions are checked, see
578 /// SubsumingPositionIterator. Thus, the returned abstract attribute
579 /// might be anchored at a different position, e.g., the callee if \p IRP is a
580 /// call base.
218581 ///
219582 /// This method is the only (supported) way an abstract attribute can retrieve
220583 /// information from another abstract attribute. As an example, take an
224587 /// the one reasoning about the "captured" state for the argument or the one
225588 /// reasoning on the memory access behavior of the function as a whole.
226589 template
227 const AAType *getAAFor(const AbstractAttribute &QueryingAA, const Value &V,
228 int ArgNo = -1) {
590 const AAType *getAAFor(const AbstractAttribute &QueryingAA,
591 const IRPosition &IRP) {
229592 static_assert(std::is_base_of::value,
230593 "Cannot query an attribute with a type not derived from "
231594 "'AbstractAttribute'!");
232595
233 // Determine the argument number automatically for llvm::Arguments if none
234 // is set. Do not override a given one as it could be a use of the argument
235 // in a call site.
236 if (ArgNo == -1)
237 if (auto *Arg = dyn_cast(&V))
238 ArgNo = Arg->getArgNo();
239
240 // If a function was given together with an argument number, perform the
241 // lookup for the actual argument instead. Don't do it for variadic
242 // arguments.
243 if (ArgNo >= 0 && isa(&V) &&
244 cast(&V)->arg_size() > (size_t)ArgNo)
245 return getAAFor(
246 QueryingAA, *(cast(&V)->arg_begin() + ArgNo), ArgNo);
247
248 // Lookup the abstract attribute of type AAType. If found, return it after
249 // registering a dependence of QueryingAA on the one returned attribute.
250 const auto &KindToAbstractAttributeMap = AAMap.lookup({&V, ArgNo});
251 if (AAType *AA = static_cast(
252 KindToAbstractAttributeMap.lookup(&AAType::ID))) {
253 // Do not return an attribute with an invalid state. This minimizes checks
254 // at the calls sites and allows the fallback below to kick in.
255 if (AA->getState().isValidState()) {
256 QueryMap[AA].insert(const_cast(&QueryingAA));
257 return AA;
596 // Let's try an equivalent position if available, see
597 // SubsumingPositionIterator for more information.
598 for (const IRPosition &EquivIRP : SubsumingPositionIterator(IRP)) {
599 // Lookup the abstract attribute of type AAType. If found, return it after
600 // registering a dependence of QueryingAA on the one returned attribute.
601 const auto &KindToAbstractAttributeMap =
602 AAMap.lookup(const_cast(EquivIRP));
603 if (AAType *AA = static_cast(
604 KindToAbstractAttributeMap.lookup(&AAType::ID))) {
605 // Do not return an attribute with an invalid state. This minimizes
606 // checks at the calls sites and allows the fallback below to kick in.
607 if (AA->getState().isValidState()) {
608 QueryMap[AA].insert(const_cast(&QueryingAA));
609 return AA;
610 }
258611 }
259612 }
260613
261 // If no abstract attribute was found and we look for a call site argument,
262 // defer to the actual argument instead.
263 ImmutableCallSite ICS(&V);
264 if (ICS && ICS.getCalledValue())
265 return getAAFor(QueryingAA, *ICS.getCalledValue(), ArgNo);
266
267614 // No matching attribute found
268615 return nullptr;
269616 }
273620 /// Note that ownership of the attribute is given to the Attributor. It will
274621 /// invoke delete for the Attributor on destruction of the Attributor.
275622 ///
276 /// Attributes are identified by
277 /// (1) their anchored value (see AA.getAnchoredValue()),
278 /// (2) their argument number (\p ArgNo, or Argument::getArgNo()), and
279 /// (3) the address of their static member (see AAType::ID).
280 template AAType ®isterAA(AAType &AA, int ArgNo = -1) {
623 /// Attributes are identified by their IR position (AAType::getIRPosition())
624 /// and the address of their static member (see AAType::ID).
625 template AAType ®isterAA(AAType &AA) {
281626 static_assert(std::is_base_of::value,
282627 "Cannot register an attribute with a type not derived from "
283628 "'AbstractAttribute'!");
284
285 // Determine the anchor value and the argument number which are used to
286 // lookup the attribute together with AAType::ID. If passed an argument,
287 // use its argument number but do not override a given one as it could be a
288 // use of the argument at a call site.
289 Value &AnchorVal = AA.getIRPosition().getAnchorValue();
290 if (ArgNo == -1)
291 if (auto *Arg = dyn_cast(&AnchorVal))
292 ArgNo = Arg->getArgNo();
293
294629 // Put the attribute in the lookup map structure and the container we use to
295630 // keep track of all attributes.
296 AAMap[{&AnchorVal, ArgNo}][&AAType::ID] = &AA;
631 IRPosition &IRP = AA.getIRPosition();
632 AAMap[IRP][&AAType::ID] = &AA;
297633 AllAbstractAttributes.push_back(&AA);
298634 return AA;
299635 }
320656 /// This method will evaluate \p Pred on call sites and return
321657 /// true if \p Pred holds in every call sites. However, this is only possible
322658 /// all call sites are known, hence the function has internal linkage.
323 bool checkForAllCallSites(Function &F, std::function &Pred,
659 bool checkForAllCallSites(const function_ref &Pred,
324660 const AbstractAttribute &QueryingAA,
325661 bool RequireAllCallSites);
326662
327663 /// Check \p Pred on all values potentially returned by \p F.
328664 ///
329665 /// This method will evaluate \p Pred on all values potentially returned by
330 /// \p F associated to their respective return instructions. Return true if
331 /// \p Pred holds on all of them.
666 /// the function associated with \p QueryingAA. The returned values are
667 /// matched with their respective return instructions. Returns true if \p Pred
668 /// holds on all of them.
332669 bool checkForAllReturnedValuesAndReturnInsts(
333 const Function &F,
334670 const function_ref &)>
335671 &Pred,
336672 const AbstractAttribute &QueryingAA);
337673
338 /// Check \p Pred on all values potentially returned by \p F.
674 /// Check \p Pred on all values potentially returned by the function
675 /// associated with \p QueryingAA.
339676 ///
340677 /// This is the context insensitive version of the method above.
341 bool checkForAllReturnedValues(const Function &F,
342 const function_ref &Pred,
678 bool checkForAllReturnedValues(const function_ref &Pred,
343679 const AbstractAttribute &QueryingAA);
344680
345681 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
346682 ///
347683 /// This method will evaluate \p Pred on all instructions with an opcode
348684 /// present in \p Opcode and return true if \p Pred holds on all of them.
349 bool checkForAllInstructions(const Function &F,
350 const function_ref &Pred,
685 bool checkForAllInstructions(const function_ref &Pred,
351686 const AbstractAttribute &QueryingAA,
352687 const ArrayRef &Opcodes);
353688
355690 ///
356691 /// See checkForAllCallLikeInstructions(...) for more information.
357692 bool
358 checkForAllCallLikeInstructions(const Function &F,
359 const function_ref &Pred,
693 checkForAllCallLikeInstructions(const function_ref &Pred,
360694 const AbstractAttribute &QueryingAA) {
361 return checkForAllInstructions(F, Pred, QueryingAA,
695 return checkForAllInstructions(Pred, QueryingAA,
362696 {(unsigned)Instruction::Invoke,
363697 (unsigned)Instruction::CallBr,
364698 (unsigned)Instruction::Call});
370704 /// to memory present in the information cache and return true if \p Pred
371705 /// holds on all of them.
372706 bool checkForAllReadWriteInstructions(
373 const Function &F, const llvm::function_ref &Pred,
707 const llvm::function_ref &Pred,
374708 AbstractAttribute &QueryingAA);
375709
376710 /// Return the data layout associated with the anchor scope.
383717 AAVector AllAbstractAttributes;
384718 ///}
385719
386 /// A nested map to lookup abstract attributes based on the anchored value and
387 /// an argument positions (or -1) on the outer level, and the addresses of the
388 /// static member (AAType::ID) on the inner level.
720 /// A nested map to lookup abstract attributes based on the argument position
721 /// on the outer level, and the addresses of the static member (AAType::ID) on
722 /// the inner level.
389723 ///{
390724 using KindToAbstractAttributeMap =
391725 DenseMap;
392 DenseMap<std::pair, KindToAbstractAttributeMap> AAMap;
726 DenseMap<IRPosition, KindToAbstractAttributeMap> AAMap;
393727 ///}
394728
395729 /// A map from abstract attributes to the ones that queried them through calls
559893 BooleanState() : IntegerState(1){};
560894 };
561895
562 /// Struct to encode the position in the LLVM-IR with regards to the associated
563 /// value but also the attribute lists.
564 struct IRPosition {
565
566 /// The positions attributes can be manifested in.
567 enum Kind {
568 IRP_FUNCTION = AttributeList::FunctionIndex, ///< An attribute for a
569 ///< function as a whole.
570 IRP_RETURNED = AttributeList::ReturnIndex, ///< An attribute for the
571 ///< function return value.
572 IRP_ARGUMENT, ///< An attribute for a function argument.
573 IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
574 };
575
576 /// Create an IRPosition for an argument.
577 explicit IRPosition(Argument &Arg) : IRPosition(&Arg, Arg, Arg.getArgNo()) {}
578
579 /// Create an IRPosition for a function return or function body position.
580 ///
581 /// \param Fn The value this abstract attributes is anchored at and
582 /// associated with.
583 /// \param PK The kind of attribute position, can not be a (call site)
584 /// argument.
585 explicit IRPosition(Function &Fn, Kind PK)
586 : AssociatedVal(&Fn), AnchorVal(Fn), AttributeIdx(PK) {
587 assert((PK == IRP_RETURNED || PK == IRP_FUNCTION) &&
588 "Expected non-argument position!");
589 }
590
591 /// An abstract attribute associated with \p AssociatedVal and anchored at
592 /// \p AnchorVal.
593 ///
594 /// \param AssociatedVal The value this abstract attribute is associated with.
595 /// \param AnchorVal The value this abstract attributes is anchored at.
596 /// \param ArgumentNo The index in the attribute list, encodes also the
597 /// argument number if this is one.
598 explicit IRPosition(Value *AssociatedVal, Value &AnchorVal,
599 unsigned ArgumentNo)
600 : AssociatedVal(AssociatedVal), AnchorVal(AnchorVal),
601 AttributeIdx(ArgumentNo + AttributeList::FirstArgIndex) {
602 assert(((isa(&AnchorVal) &&
603 cast(&AnchorVal)->arg_size() > ArgumentNo) ||
604 (isa(AnchorVal) &&
605 cast(AnchorVal).getArgNo() == ArgumentNo)) &&
606 "Expected a valid argument index!");
607 }
608
609 #define IRPositionConstructorForward(NAME, BASE) \
610 explicit NAME(Argument &Arg) : BASE(Arg) {} \
611 explicit NAME(Function &Fn, IRPosition::Kind PK) : BASE(Fn, PK) {} \
612 NAME(Value *AssociatedVal, Value &AnchorVal, unsigned ArgumentNo) \
613 : BASE(AssociatedVal, AnchorVal, ArgumentNo) {}
614
615 IRPosition(const IRPosition &AAP)
616 : IRPosition(AAP.AssociatedVal, AAP.AnchorVal, AAP.AttributeIdx) {}
617
618 /// Virtual destructor.
619 virtual ~IRPosition() {}
620
621 /// Return the value this abstract attribute is anchored with.
622 ///
623 /// The anchored value might not be the associated value if the latter is not
624 /// sufficient to determine where arguments will be manifested. This is mostly
625 /// the case for call site arguments as the value is not sufficient to
626 /// pinpoint them. Instead, we can use the call site as an anchor.
627 ///
628 ///{
629 Value &getAnchorValue() { return AnchorVal; }
630 const Value &getAnchorValue() const { return AnchorVal; }
631 ///}
632
633 /// Return the llvm::Function surrounding the anchored value.
634 ///
635 ///{
636 Function &getAnchorScope() {
637 Value &V = getAnchorValue();
638 if (isa(V))
639 return cast(V);
640 if (isa(V))
641 return *cast(V).getParent();
642 if (isa(V))
643 return *cast(V).getFunction();
644 llvm_unreachable("No scope for anchored value found!");
645 }
646 const Function &getAnchorScope() const {
647 return const_cast(this)->getAnchorScope();
648 }
649 ///}
650
651 /// Return the value this abstract attribute is associated with.
652 ///
653 /// The abstract state usually represents this value.
654 ///
655 ///{
656 Value *getAssociatedValue() { return AssociatedVal; }
657 const Value *getAssociatedValue() const { return AssociatedVal; }
658 ///}
659
660 /// Return the argument number of the associated value if it is an argument or
661 /// call site argument, otherwise a negative value.
662 int getArgNo() const { return AttributeIdx - AttributeList::FirstArgIndex; }
663
664 /// Return the position this abstract state is manifested in.
665 Kind getPositionKind() const {
666 if (getArgNo() >= 0) {
667 if (isa(getAnchorValue()))
668 return IRP_CALL_SITE_ARGUMENT;
669 assert((isa(getAnchorValue()) ||
670 isa_and_nonnull(getAssociatedValue()) ||
671 (!getAssociatedValue() && isa(getAnchorValue()))) &&
672 "Expected argument or call base due to argument number!");
673 return IRP_ARGUMENT;
674 }
675 return (Kind)AttributeIdx;
676 }
677
678 /// Return the index in the attribute list for this position.
679 int getAttrIdx() const { return AttributeIdx; }
680
681 /// Change the associated value.
682 void setAssociatedValue(Value *V) { AssociatedVal = V; }
683
684 /// Change the associated attribue list position.
685 void setAttributeIdx(int AttrIdx) { AttributeIdx = AttrIdx; }
686
687 protected:
688 /// The value this abstract attribute is associated with.
689 Value *AssociatedVal;
690
691 /// The value this abstract attribute is anchored at.
692 Value &AnchorVal;
693
694 /// The index in the attribute list.
695 int AttributeIdx;
696 };
697
698896 /// Helper struct necessary as the modular build fails if the virtual method
699897 /// IRAttribute::manifest is defined in the Attributor.cpp.
700898 struct IRAttributeManifest {
718916 /// Helper class that provides common functionality to manifest IR attributes.
719917 template
720918 struct IRAttribute : public IRPosition, public Base {
919 IRAttribute(const IRPosition &IRP) : IRPosition(IRP) {}
721920 ~IRAttribute() {}
722921
723 /// Constructors for the IRPosition.
724 ///
725 ///{
726 IRPositionConstructorForward(IRAttribute, IRPosition);
727 ///}
728
729922 /// See AbstractAttribute::manifest(...).
730 ChangeStatus manifest(Attributor &A) {
923 ChangeStatus manifest(Attributor &A) override {
731924 SmallVector DeducedAttrs;
732 getDeducedAttributes(getAnchorScope().getContext(), DeducedAttrs);
925 getDeducedAttributes(getAnchorValue().getContext(), DeducedAttrs);
733926 return IRAttributeManifest::manifestAttrs(A, getIRPosition(), DeducedAttrs);
734927 }
735928
745938 /// Return an IR position, see struct IRPosition.
746939 ///
747940 ///{
748 IRPosition &getIRPosition() { return *this; }
749 const IRPosition &getIRPosition() const { return *this; }
941 IRPosition &getIRPosition() override { return *this; }
942 const IRPosition &getIRPosition() const override { return *this; }
750943 ///}
751944 };
752945
8891082 /// An abstract attribute for the returned values of a function.
8901083 struct AAReturnedValues
8911084 : public IRAttribute {
892 IRPositionConstructorForward(AAReturnedValues, IRAttribute);
1085 AAReturnedValues(const IRPosition &IRP) : IRAttribute(IRP) {}
8931086
8941087 /// Check \p Pred on all returned values.
8951088 ///
9101103 struct AANoUnwind
9111104 : public IRAttribute
9121105 StateWrapper> {
913 IRPositionConstructorForward(AANoUnwind, IRAttribute);
1106 AANoUnwind(const IRPosition &IRP) : IRAttribute(IRP) {}
9141107
9151108 /// Returns true if nounwind is assumed.
9161109 bool isAssumedNoUnwind() const { return getAssumed(); }
9251118 struct AANoSync
9261119 : public IRAttribute
9271120 StateWrapper> {
928 IRPositionConstructorForward(AANoSync, IRAttribute);
1121 AANoSync(const IRPosition &IRP) : IRAttribute(IRP) {}
9291122
9301123 /// Returns true if "nosync" is assumed.
9311124 bool isAssumedNoSync() const { return getAssumed(); }
9411134 struct AANonNull
9421135 : public IRAttribute
9431136 StateWrapper> {
944 IRPositionConstructorForward(AANonNull, IRAttribute);
1137 AANonNull(const IRPosition &IRP) : IRAttribute(IRP) {}
9451138
9461139 /// Return true if we assume that the underlying value is nonnull.
9471140 bool isAssumedNonNull() const { return getAssumed(); }
9571150 struct AANoRecurse
9581151 : public IRAttribute
9591152 StateWrapper> {
960 IRPositionConstructorForward(AANoRecurse, IRAttribute);
1153 AANoRecurse(const IRPosition &IRP) : IRAttribute(IRP) {}
9611154
9621155 /// Return true if "norecurse" is assumed.
9631156 bool isAssumedNoRecurse() const { return getAssumed(); }
9731166 struct AAWillReturn
9741167 : public IRAttribute
9751168 StateWrapper> {
976 IRPositionConstructorForward(AAWillReturn, IRAttribute);
1169 AAWillReturn(const IRPosition &IRP) : IRAttribute(IRP) {}
9771170
9781171 /// Return true if "willreturn" is assumed.
9791172 bool isAssumedWillReturn() const { return getAssumed(); }
9891182 struct AANoAlias
9901183 : public IRAttribute
9911184 StateWrapper> {
992 IRPositionConstructorForward(AANoAlias, IRAttribute);
1185 AANoAlias(const IRPosition &IRP) : IRAttribute(IRP) {}
9931186
9941187 /// Return true if we assume that the underlying value is alias.
9951188 bool isAssumedNoAlias() const { return getAssumed(); }
10051198 struct AANoFree
10061199 : public IRAttribute
10071200 StateWrapper> {
1008 IRPositionConstructorForward(AANoFree, IRAttribute);
1201 AANoFree(const IRPosition &IRP) : IRAttribute(IRP) {}
10091202
10101203 /// Return true if "nofree" is assumed.
10111204 bool isAssumedNoFree() const { return getAssumed(); }
10211214 struct AANoReturn
10221215 : public IRAttribute
10231216 StateWrapper> {
1024 IRPositionConstructorForward(AANoReturn, IRAttribute);
1217 AANoReturn(const IRPosition &IRP) : IRAttribute(IRP) {}
10251218
10261219 /// Return true if the underlying object is assumed to never return.
10271220 bool isAssumedNoReturn() const { return getAssumed(); }
10361229 /// An abstract interface for liveness abstract attribute.
10371230 struct AAIsDead : public StateWrapper,
10381231 public IRPosition {
1039 IRPositionConstructorForward(AAIsDead, IRPosition);
1232 AAIsDead(const IRPosition &IRP) : IRPosition(IRP) {}
10401233
10411234 /// Returns true if \p BB is assumed dead.
10421235 virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
10781271 /// An abstract interface for all dereferenceable attribute.
10791272 struct AADereferenceable
10801273 : public IRAttribute {
1081 IRPositionConstructorForward(AADereferenceable, IRAttribute);
1274 AADereferenceable(const IRPosition &IRP) : IRAttribute(IRP) {}
10821275
10831276 /// Return true if we assume that the underlying value is nonnull.
10841277 virtual bool isAssumedNonNull() const = 0;
11081301 struct AAAlign
11091302 : public IRAttribute
11101303 StateWrapper> {
1111 IRPositionConstructorForward(AAAlign, IRAttribute);
1304 AAAlign(const IRPosition &IRP) : IRAttribute(IRP) {}
11121305
11131306 /// Return assumed alignment.
11141307 unsigned getAssumedAlign() const { return getAssumed(); }
264264 ChangeStatus
265265 IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP,
266266 const ArrayRef &DeducedAttrs) {
267 assert(IRP.getAssociatedValue() &&
268 "Attempted to manifest an attribute without associated value!");
269
270267 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
271268
272 Function &ScopeFn = IRP.getAnchorScope();
273 LLVMContext &Ctx = ScopeFn.getContext();
269 Function *ScopeFn = IRP.getAssociatedFunction();
274270 IRPosition::Kind PK = IRP.getPositionKind();
275271
276272 // In the following some generic code that will manifest attributes in
278274 // annotation positions we use the underlying AttributeList interface.
279275
280276 AttributeList Attrs;
277 switch (PK) {
278 case IRPosition::IRP_INVALID:
279 case IRPosition::IRP_FLOAT:
280 llvm_unreachable("Cannot manifest at a floating or invalid position!");
281 case IRPosition::IRP_ARGUMENT:
282 case IRPosition::IRP_FUNCTION:
283 case IRPosition::IRP_RETURNED:
284 Attrs = ScopeFn->getAttributes();
285 break;
286 case IRPosition::IRP_CALL_SITE:
287 case IRPosition::IRP_CALL_SITE_RETURNED:
288 case IRPosition::IRP_CALL_SITE_ARGUMENT:
289 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
290 break;
291 }
292
293 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
294 for (const Attribute &Attr : DeducedAttrs) {
295 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
296 continue;
297
298 HasChanged = ChangeStatus::CHANGED;
299 }
300
301 if (HasChanged == ChangeStatus::UNCHANGED)
302 return HasChanged;
303
281304 switch (PK) {
282305 case IRPosition::IRP_ARGUMENT:
283306 case IRPosition::IRP_FUNCTION:
284307 case IRPosition::IRP_RETURNED:
285 Attrs = ScopeFn.getAttributes();
308 ScopeFn->setAttributes(Attrs);
286309 break;
287 case IRPosition::IRP_CALL_SITE_ARGUMENT:
288 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
289 break;
290 }
291
292 for (const Attribute &Attr : DeducedAttrs) {
293 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
294 continue;
295
296 HasChanged = ChangeStatus::CHANGED;
297 }
298
299 if (HasChanged == ChangeStatus::UNCHANGED)
300 return HasChanged;
301
302 switch (PK) {
303 case IRPosition::IRP_ARGUMENT:
304 case IRPosition::IRP_FUNCTION:
305 case IRPosition::IRP_RETURNED:
306 ScopeFn.setAttributes(Attrs);
307 break;
310 case IRPosition::IRP_CALL_SITE:
311 case IRPosition::IRP_CALL_SITE_RETURNED:
308312 case IRPosition::IRP_CALL_SITE_ARGUMENT:
309313 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
314 case IRPosition::IRP_FLOAT:
315 case IRPosition::IRP_INVALID:
316 break;
310317 }
311318
312319 return HasChanged;
313320 }
314321
322 const IRPosition IRPosition::EmptyKey(255);
323 const IRPosition IRPosition::TombstoneKey(256);
324
325 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
326 IRPositions.emplace_back(IRP);
327
328 ImmutableCallSite ICS(&IRP.getAnchorValue());
329 switch (IRP.getPositionKind()) {
330 case IRPosition::IRP_INVALID:
331 case IRPosition::IRP_FLOAT:
332 case IRPosition::IRP_FUNCTION:
333 return;
334 case IRPosition::IRP_ARGUMENT:
335 case IRPosition::IRP_RETURNED:
336 IRPositions.emplace_back(
337 IRPosition::function(*IRP.getAssociatedFunction()));
338 return;
339 case IRPosition::IRP_CALL_SITE:
340 assert(ICS && "Expected call site!");
341 // TODO: We need to look at the operand bundles similar to the redirection
342 // in CallBase.
343 if (!ICS.hasOperandBundles())
344 if (const Function *Callee = ICS.getCalledFunction())
345 IRPositions.emplace_back(IRPosition::function(*Callee));
346 return;
347 case IRPosition::IRP_CALL_SITE_RETURNED:
348 assert(ICS && "Expected call site!");
349 // TODO: We need to look at the operand bundles similar to the redirection
350 // in CallBase.
351 if (!ICS.hasOperandBundles()) {
352 if (const Function *Callee = ICS.getCalledFunction()) {
353 IRPositions.emplace_back(IRPosition::returned(*Callee));
354 IRPositions.emplace_back(IRPosition::function(*Callee));
355 }
356 }
357 IRPositions.emplace_back(
358 IRPosition::callsite_function(cast(*ICS.getInstruction())));
359 return;
360 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
361 int ArgNo = IRP.getArgNo();
362 assert(ICS && ArgNo >= 0 && "Expected call site!");
363 // TODO: We need to look at the operand bundles similar to the redirection
364 // in CallBase.
365 if (!ICS.hasOperandBundles()) {
366 const Function *Callee = ICS.getCalledFunction();
367 if (Callee && Callee->arg_size() > unsigned(ArgNo))
368 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
369 if (Callee)
370 IRPositions.emplace_back(IRPosition::function(*Callee));
371 }
372 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
373 return;
374 }
375 }
376 }
377
378 bool IRPosition::hasAttr(ArrayRef AKs) const {
379 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this))
380 for (Attribute::AttrKind AK : AKs)
381 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
382 return true;
383 return false;
384 }
385
386 void IRPosition::getAttrs(ArrayRef AKs,
387 SmallVectorImpl &Attrs) const {
388 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this))
389 for (Attribute::AttrKind AK : AKs) {
390 const Attribute &Attr = EquivIRP.getAttr(AK);
391 if (Attr.getKindAsEnum() == AK)
392 Attrs.push_back(Attr);
393 }
394 }
395
396 void IRPosition::verify() {
397 switch (KindOrArgNo) {
398 default:
399 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
400 assert((isa(AnchorVal) || isa(AnchorVal)) &&
401 "Expected call base or argument for positive attribute index!");
402 if (auto *Arg = dyn_cast(AnchorVal)) {
403 assert(Arg->getArgNo() == unsigned(getArgNo()) &&
404 "Argument number mismatch!");
405 assert(Arg == &getAssociatedValue() && "Associated value mismatch!");
406 } else {
407 auto &CB = cast(*AnchorVal);
408 assert(CB.arg_size() > unsigned(getArgNo()) &&
409 "Call site argument number mismatch!");
410 assert(CB.getArgOperand(getArgNo()) == &getAssociatedValue() &&
411 "Associated value mismatch!");
412 }
413 break;
414 case IRP_INVALID:
415 assert(!AnchorVal && "Expected no value for an invalid position!");
416 break;
417 case IRP_FLOAT:
418 assert((!isa(&getAssociatedValue()) &&
419 !isa(&getAssociatedValue())) &&
420 "Expected specialized kind for call base and argument values!");
421 break;
422 case IRP_RETURNED:
423 assert(isa(AnchorVal) &&
424 "Expected function for a 'returned' position!");
425 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
426 break;
427 case IRP_CALL_SITE_RETURNED:
428 assert((isa(AnchorVal)) &&
429 "Expected call base for 'call site returned' position!");
430 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
431 break;
432 case IRP_CALL_SITE:
433 assert((isa(AnchorVal)) &&
434 "Expected call base for 'call site function' position!");
435 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
436 break;
437 case IRP_FUNCTION:
438 assert(isa(AnchorVal) &&
439 "Expected function for a 'function' position!");
440 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
441 break;
442 }
443 }
444
315445 /// -----------------------NoUnwind Function Attribute--------------------------
316446
317447 struct AANoUnwindImpl : AANoUnwind {
318 IRPositionConstructorForward(AANoUnwindImpl, AANoUnwind);
448 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
319449
320450 const std::string getAsStr() const override {
321451 return getAssumed() ? "nounwind" : "may-unwind";
326456 };
327457
328458 struct AANoUnwindFunction final : public AANoUnwindImpl {
329 AANoUnwindFunction(Function &F) : AANoUnwindImpl(F, IRP_FUNCTION) {}
459 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
330460
331461 /// See AbstractAttribute::trackStatistics()
332462 void trackStatistics() const override {
335465 };
336466
337467 ChangeStatus AANoUnwindImpl::updateImpl(Attributor &A) {
338 Function &F = getAnchorScope();
339468
340469 // The map from instruction opcodes to those instructions in the function.
341470 auto Opcodes = {
347476 if (!I.mayThrow())
348477 return true;
349478
350 auto *NoUnwindAA = A.getAAFor(*this, I);
479 auto *NoUnwindAA = A.getAAFor(*this, IRPosition::value(I));
351480 return NoUnwindAA && NoUnwindAA->isAssumedNoUnwind();
352481 };
353482
354 if (!A.checkForAllInstructions(F, CheckForNoUnwind, *this, Opcodes))
483 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
355484 return indicatePessimisticFixpoint();
356485
357486 return ChangeStatus::UNCHANGED;
404533 }
405534
406535 public:
407 IRPositionConstructorForward(AAReturnedValuesImpl, AAReturnedValues);
536 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
408537
409538 /// See AbstractAttribute::initialize(...).
410539 void initialize(Attributor &A) override {
411540 // Reset the state.
412 setAssociatedValue(nullptr);
413541 IsFixed = false;
414542 IsValidState = true;
415543 HasOverdefinedReturnedCalls = false;
493621 };
494622
495623 struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
496 AAReturnedValuesFunction(Function &F)
497 : AAReturnedValuesImpl(F, IRP_FUNCTION) {}
624 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
498625
499626 /// See AbstractAttribute::trackStatistics()
500627 void trackStatistics() const override {
522649
523650 // If the assumed unique return value is an argument, annotate it.
524651 if (auto *UniqueRVArg = dyn_cast(UniqueRV.getValue())) {
525 setAssociatedValue(UniqueRVArg);
526 setAttributeIdx(UniqueRVArg->getArgNo() + AttributeList::FirstArgIndex);
652 getIRPosition() = IRPosition::argument(*UniqueRVArg);
527653 Changed = IRAttribute::manifest(A) | Changed;
528654 }
529655
562688 return true;
563689 };
564690
565 if (!A.checkForAllReturnedValues(getAnchorScope(), Pred, *this))
691 if (!A.checkForAllReturnedValues(Pred, *this))
566692 UniqueRV = nullptr;
567693
568694 return UniqueRV;
607733 // Keep track of any change to trigger updates on dependent attributes.
608734 ChangeStatus Changed = ChangeStatus::UNCHANGED;
609735
610 auto *LivenessAA = A.getAAFor(*this, getAnchorScope());
736 auto *LivenessAA = A.getAAFor(*this, getIRPosition());
611737
612738 // Look at all returned call sites.
613739 for (auto &It : ReturnedValues) {
636762 }
637763
638764 // Try to find a assumed unique return value for the called function.
639 auto *RetCSAA = A.getAAFor(*this, *RV);
765 auto *RetCSAA = A.getAAFor(
766 *this, IRPosition::callsite_returned(RetCS));
640767 if (!RetCSAA) {
641768 if (!HasOverdefinedReturnedCalls)
642769 Changed = ChangeStatus::CHANGED;
652779
653780 // If no assumed unique return value was found due to the lack of
654781 // candidates, we may need to resolve more calls (through more update
655 // iterations) or the called function will not return. Either way, we simply
656 // stick with the call sites as return values. Because there were not
657 // multiple possibilities, we do not treat it as overdefined.
782 // iterations) or the called function will not return. Either way, we
783 // simply stick with the call sites as return values. Because there were
784 // not multiple possibilities, we do not treat it as overdefined.
658785 if (!AssumedUniqueRV.hasValue())
659786 continue;
660787
681808
682809 // If the assumed unique return value is an argument, lookup the matching
683810 // call site operand and recursively collect new returned values.
684 // If it is not an argument, it is just put into the set of returned values
685 // as we would have already looked through casts, phis, and similar values.
811 // If it is not an argument, it is just put into the set of returned
812 // values as we would have already looked through casts, phis, and similar
813 // values.
686814 if (Argument *AssumedRetArg = dyn_cast(AssumedRetVal))
687815 collectValuesRecursively(A,
688816 RetCS.getArgOperand(AssumedRetArg->getArgNo()),
714842 /// ------------------------ NoSync Function Attribute -------------------------
715843
716844 struct AANoSyncImpl : AANoSync {
717 IRPositionConstructorForward(AANoSyncImpl, AANoSync);
845 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
718846
719847 const std::string getAsStr() const override {
720848 return getAssumed() ? "nosync" : "may-sync";
737865 };
738866
739867 struct AANoSyncFunction final : public AANoSyncImpl {
740 AANoSyncFunction(Function &F) : AANoSyncImpl(F, IRP_FUNCTION) {}
868 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
741869
742870 /// See AbstractAttribute::trackStatistics()
743871 void trackStatistics() const override { STATS_DECL_AND_TRACK_FN_ATTR(nosync) }
833961 }
834962
835963 ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
836 Function &F = getAnchorScope();
837964
838965 auto CheckRWInstForNoSync = [&](Instruction &I) {
839966 /// We are looking for volatile instructions or Non-Relaxed atomics.
840967 /// FIXME: We should ipmrove the handling of intrinsics.
841968
842 ImmutableCallSite ICS(&I);
843 auto *NoSyncAA = A.getAAFor(*this, I);
844
845969 if (isa(&I) && isNoSyncIntrinsic(&I))
846970 return true;
847971
848 if (ICS && (!NoSyncAA || !NoSyncAA->isAssumedNoSync()) &&
849 !ICS.hasFnAttr(Attribute::NoSync))
972 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
973 if (ICS.hasFnAttr(Attribute::NoSync))
974 return true;
975
976 auto *NoSyncAA =
977 A.getAAFor(*this, IRPosition::callsite_function(ICS));
978 if (NoSyncAA && NoSyncAA->isAssumedNoSync())
979 return true;
850980 return false;
851
852 if (ICS)
853 return true;
981 }
854982
855983 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
856984 return true;
868996 return !ImmutableCallSite(&I).isConvergent();
869997 };
870998
871 if (!A.checkForAllReadWriteInstructions(F, CheckRWInstForNoSync, *this) ||
872 !A.checkForAllCallLikeInstructions(F, CheckForNoSync, *this))
999 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1000 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
8731001 return indicatePessimisticFixpoint();
8741002
8751003 return ChangeStatus::UNCHANGED;
8781006 /// ------------------------ No-Free Attributes ----------------------------
8791007
8801008 struct AANoFreeImpl : public AANoFree {
881 IRPositionConstructorForward(AANoFreeImpl, AANoFree);
1009 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
8821010
8831011 /// See AbstractAttribute::getAsStr().
8841012 const std::string getAsStr() const override {
8901018 };
8911019
8921020 struct AANoFreeFunction final : public AANoFreeImpl {
893 AANoFreeFunction(Function &F) : AANoFreeImpl(F, IRP_FUNCTION) {}
1021 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
8941022
8951023 /// See AbstractAttribute::trackStatistics()
8961024 void trackStatistics() const override { STATS_DECL_AND_TRACK_FN_ATTR(nofree) }
8971025 };
8981026
8991027 ChangeStatus AANoFreeImpl::updateImpl(Attributor &A) {
900 Function &F = getAnchorScope();
9011028
9021029 auto CheckForNoFree = [&](Instruction &I) {
903 if (ImmutableCallSite(&I).hasFnAttr(Attribute::NoFree))
1030 ImmutableCallSite ICS(&I);
1031 if (ICS.hasFnAttr(Attribute::NoFree))
9041032 return true;
9051033
906 auto *NoFreeAA = A.getAAFor(*this, I);
1034 auto *NoFreeAA =
1035 A.getAAFor(*this, IRPosition::callsite_function(ICS));
9071036 return NoFreeAA && NoFreeAA->isAssumedNoFree();
9081037 };
9091038
910 if (!A.checkForAllCallLikeInstructions(F, CheckForNoFree, *this))
1039 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
9111040 return indicatePessimisticFixpoint();
9121041 return ChangeStatus::UNCHANGED;
9131042 }
9141043
9151044 /// ------------------------ NonNull Argument Attribute ------------------------
9161045 struct AANonNullImpl : AANonNull {
917 IRPositionConstructorForward(AANonNullImpl, AANonNull);
1046 AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {}
9181047
9191048 /// See AbstractAttribute::getAsStr().
9201049 const std::string getAsStr() const override {
9211050 return getAssumed() ? "nonnull" : "may-null";
1051 }
1052
1053 /// See AbstractAttribute::initialize(...).
1054 void initialize(Attributor &A) override {
1055 if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1056 indicateOptimisticFixpoint();
9221057 }
9231058
9241059 /// Generate a predicate that checks if a given value is assumed nonnull.
9441079 if (isKnownNonZero(&RV, A.getDataLayout()))
9451080 return true;
9461081
947 auto *NonNullAA = A.getAAFor(*this, RV);
948
949 ImmutableCallSite ICS(&RV);
950
951 if ((!NonNullAA || !NonNullAA->isAssumedNonNull()) &&
952 (!ICS || !ICS.hasRetAttr(Attribute::NonNull)))
953 return false;
954
955 return true;
1082 if (ImmutableCallSite ICS = ImmutableCallSite(&RV))
1083 if (ICS.hasRetAttr(Attribute::NonNull))
1084 return true;
1085
1086 auto *NonNullAA = A.getAAFor(*this, IRPosition::value(RV));
1087 return (NonNullAA && NonNullAA->isAssumedNonNull());
9561088 };
9571089
9581090 return Pred;
9601092
9611093 /// NonNull attribute for function return value.
9621094 struct AANonNullReturned final : AANonNullImpl {
963 AANonNullReturned(Function &F) : AANonNullImpl(F, IRP_RETURNED) {}
964
965 /// See AbstractAttribute::initialize(...).
966 void initialize(Attributor &A) override {
967 Function &F = getAnchorScope();
968
969 // Already nonnull.
970 if (F.getAttributes().hasAttribute(AttributeList::ReturnIndex,
971 Attribute::NonNull) ||
972 F.getAttributes().hasAttribute(AttributeList::ReturnIndex,
973 Attribute::Dereferenceable))
974 indicateOptimisticFixpoint();
975 }
1095 AANonNullReturned(const IRPosition &IRP) : AANonNullImpl(IRP) {}
9761096
9771097 /// See AbstractAttribute::updateImpl(...).
9781098 ChangeStatus updateImpl(Attributor &A) override;
9841104 };
9851105
9861106 ChangeStatus AANonNullReturned::updateImpl(Attributor &A) {
987 Function &F = getAnchorScope();
9881107
9891108 std::function &)> Pred =
9901109 this->generatePredicate(A);
9911110
992 if (!A.checkForAllReturnedValuesAndReturnInsts(F, Pred, *this))
1111 if (!A.checkForAllReturnedValuesAndReturnInsts(Pred, *this))
9931112 return indicatePessimisticFixpoint();
9941113 return ChangeStatus::UNCHANGED;
9951114 }
9961115
9971116 /// NonNull attribute for function argument.
9981117 struct AANonNullArgument final : AANonNullImpl {
999 AANonNullArgument(Argument &A) : AANonNullImpl(A) {}
1000
1001 /// See AbstractAttriubute::initialize(...).
1002 void initialize(Attributor &A) override {
1003 Argument *Arg = cast(getAssociatedValue());
1004 if (Arg->hasNonNullAttr())
1005 indicateOptimisticFixpoint();
1006 }
1118 AANonNullArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {}
10071119
10081120 /// See AbstractAttribute::updateImpl(...).
10091121 ChangeStatus updateImpl(Attributor &A) override;
10161128
10171129 /// NonNull attribute for a call site argument.
10181130 struct AANonNullCallSiteArgument final : AANonNullImpl {
1019 AANonNullCallSiteArgument(Instruction &I, unsigned ArgNo)
1020 : AANonNullImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
1131 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullImpl(IRP) {}
10211132
10221133 /// See AbstractAttribute::initialize(...).
10231134 void initialize(Attributor &A) override {
1024 CallSite CS(&getAnchorValue());
1025 if (CS.paramHasAttr(getArgNo(), getAttrKind()) ||
1026 CS.paramHasAttr(getArgNo(), Attribute::Dereferenceable) ||
1027 isKnownNonZero(getAssociatedValue(), A.getDataLayout()))
1135 AANonNullImpl::initialize(A);
1136 if (!isKnownNonNull() &&
1137 isKnownNonZero(&getAssociatedValue(), A.getDataLayout()))
10281138 indicateOptimisticFixpoint();
10291139 }
10301140
10381148 };
10391149
10401150 ChangeStatus AANonNullArgument::updateImpl(Attributor &A) {
1041 Function &F = getAnchorScope();
10421151 unsigned ArgNo = getArgNo();
10431152
10441153 // Callback function
10451154 std::function CallSiteCheck = [&](CallSite CS) {
10461155 assert(CS && "Sanity check: Call site was not initialized properly!");
10471156
1048 auto *NonNullAA =
1049 A.getAAFor(*this, *CS.getInstruction(), ArgNo);
1157 IRPosition CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
1158 if (CSArgPos.hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1159 return true;
10501160
10511161 // Check that NonNullAA is AANonNullCallSiteArgument.
1052 if (NonNullAA) {
1162 if (auto *NonNullAA = A.getAAFor(*this, CSArgPos)) {
10531163 ImmutableCallSite ICS(&NonNullAA->getAnchorValue());
10541164 if (ICS && CS.getInstruction() == ICS.getInstruction())
10551165 return NonNullAA->isAssumedNonNull();
10561166 return false;
10571167 }
10581168
1059 if (CS.paramHasAttr(ArgNo, Attribute::NonNull))
1060 return true;
1061
10621169 Value *V = CS.getArgOperand(ArgNo);
10631170 if (isKnownNonZero(V, A.getDataLayout()))
10641171 return true;
10651172
10661173 return false;
10671174 };
1068 if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
1175 if (!A.checkForAllCallSites(CallSiteCheck, *this, true))
10691176 return indicatePessimisticFixpoint();
10701177 return ChangeStatus::UNCHANGED;
10711178 }
10741181 // NOTE: Never look at the argument of the callee in this method.
10751182 // If we do this, "nonnull" is always deduced because of the assumption.
10761183
1077 Value &V = *getAssociatedValue();
1078
1079 auto *NonNullAA = A.getAAFor(*this, V);
1184 Value &V = getAssociatedValue();
1185 auto *NonNullAA = A.getAAFor(*this, IRPosition::value(V));
10801186
10811187 if (!NonNullAA || !NonNullAA->isAssumedNonNull())
10821188 return indicatePessimisticFixpoint();
10871193 /// ------------------------ Will-Return Attributes ----------------------------
10881194
10891195 struct AAWillReturnImpl : public AAWillReturn {
1090 IRPositionConstructorForward(AAWillReturnImpl, AAWillReturn);
1196 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
10911197
10921198 /// See AbstractAttribute::getAsStr()
10931199 const std::string getAsStr() const override {
10961202 };
10971203
10981204 struct AAWillReturnFunction final : AAWillReturnImpl {
1099 AAWillReturnFunction(Function &F) : AAWillReturnImpl(F, IRP_FUNCTION) {}
1205 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
11001206
11011207 /// See AbstractAttribute::initialize(...).
11021208 void initialize(Attributor &A) override;
11401246 }
11411247
11421248 ChangeStatus AAWillReturnFunction::updateImpl(Attributor &A) {
1143 const Function &F = getAnchorScope();
11441249 // The map from instruction opcodes to those instructions in the function.
11451250
11461251 auto CheckForWillReturn = [&](Instruction &I) {
11481253 if (ICS.hasFnAttr(Attribute::WillReturn))
11491254 return true;
11501255
1151 auto *WillReturnAA = A.getAAFor(*this, I);
1256 IRPosition IPos = IRPosition::callsite_function(ICS);
1257 auto *WillReturnAA = A.getAAFor(*this, IPos);
11521258 if (!WillReturnAA || !WillReturnAA->isAssumedWillReturn())
11531259 return false;
11541260
11561262 if (ICS.hasFnAttr(Attribute::NoRecurse))
11571263 return true;
11581264
1159 auto *NoRecurseAA = A.getAAFor(*this, I);
1265 auto *NoRecurseAA = A.getAAFor(*this, IPos);
11601266 return NoRecurseAA && NoRecurseAA->isAssumedNoRecurse();
11611267 };
11621268
1163 if (!A.checkForAllCallLikeInstructions(F, CheckForWillReturn, *this))
1269 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
11641270 return indicatePessimisticFixpoint();
11651271
11661272 return ChangeStatus::UNCHANGED;
11691275 /// ------------------------ NoAlias Argument Attribute ------------------------
11701276
11711277 struct AANoAliasImpl : AANoAlias {
1172 IRPositionConstructorForward(AANoAliasImpl, AANoAlias);
1278 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
11731279
11741280 const std::string getAsStr() const override {
11751281 return getAssumed() ? "noalias" : "may-alias";
11781284
11791285 /// NoAlias attribute for function return value.
11801286 struct AANoAliasReturned final : AANoAliasImpl {
1181 AANoAliasReturned(Function &F) : AANoAliasImpl(F, IRP_RETURNED) {}
1287 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
11821288
11831289 /// See AbstractAttriubute::initialize(...).
11841290 void initialize(Attributor &A) override {
12011307 };
12021308
12031309 ChangeStatus AANoAliasReturned::updateImpl(Attributor &A) {
1204 Function &F = getAnchorScope();
12051310
12061311 auto CheckReturnValue = [&](Value &RV) -> bool {
12071312 if (Constant *C = dyn_cast(&RV))
12151320 return false;
12161321
12171322 if (!ICS.returnDoesNotAlias()) {
1218 auto *NoAliasAA = A.getAAFor(*this, RV);
1323 auto *NoAliasAA =
1324 A.getAAFor(*this, IRPosition::callsite_returned(ICS));
12191325 if (!NoAliasAA || !NoAliasAA->isAssumedNoAlias())
12201326 return false;
12211327 }
12301336 return true;
12311337 };
12321338
1233 if (!A.checkForAllReturnedValues(F, CheckReturnValue, *this))
1339 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
12341340 return indicatePessimisticFixpoint();
12351341
12361342 return ChangeStatus::UNCHANGED;
12391345 /// -------------------AAIsDead Function Attribute-----------------------
12401346
12411347 struct AAIsDeadImpl : public AAIsDead {
1242 IRPositionConstructorForward(AAIsDeadImpl, AAIsDead);
1348 AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
12431349
12441350 void initialize(Attributor &A) override {
12451351 const Function &F = getAnchorScope();
12981404 /// and only place an unreachable in the normal successor.
12991405 if (Invoke2CallAllowed) {
13001406 if (Function *Callee = II->getCalledFunction()) {
1301 auto *AANoUnw = A.getAAFor(*this, *Callee);
1407 auto *AANoUnw =
1408 A.getAAFor(*this, IRPosition::function(*Callee));
13021409 if (Callee->hasFnAttribute(Attribute::NoUnwind) ||
13031410 (AANoUnw && AANoUnw->isAssumedNoUnwind())) {
13041411 LLVM_DEBUG(dbgs()
13901497 };
13911498
13921499 struct AAIsDeadFunction final : public AAIsDeadImpl {
1393 AAIsDeadFunction(Function &F) : AAIsDeadImpl(F, IRP_FUNCTION) {}
1500 AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
13941501
13951502 /// See AbstractAttribute::trackStatistics()
13961503 void trackStatistics() const override {
14331540 ImmutableCallSite ICS(I);
14341541
14351542 if (ICS) {
1543 const IRPosition &IPos = IRPosition::callsite_function(ICS);
14361544 // Regarless of the no-return property of an invoke instruction we only
14371545 // learn that the regular successor is not reachable through this
14381546 // instruction but the unwind block might still be.
14391547 if (auto *Invoke = dyn_cast(I)) {
14401548 // Use nounwind to justify the unwind block is dead as well.
1441 auto *AANoUnw = A.getAAFor(*this, *Invoke);
1549 auto *AANoUnw = A.getAAFor(*this, IPos);
14421550 if (!Invoke2CallAllowed ||
14431551 (!AANoUnw || !AANoUnw->isAssumedNoUnwind())) {
14441552 AssumedLiveBlocks.insert(Invoke->getUnwindDest());
14461554 }
14471555 }
14481556
1449 auto *NoReturnAA = A.getAAFor(*this, *I);
1557 auto *NoReturnAA = A.getAAFor(*this, IPos);
14501558 if (ICS.hasFnAttr(Attribute::NoReturn) ||
14511559 (NoReturnAA && NoReturnAA->isAssumedNoReturn()))
14521560 return I;
15761684 };
15771685
15781686 struct AADereferenceableImpl : AADereferenceable, DerefState {
1579 IRPositionConstructorForward(AADereferenceableImpl, AADereferenceable);
1687 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
15801688 using StateType = DerefState;
15811689
15821690 /// See AbstractAttribute::getState()
16431751 bool &IsNonNull, bool &IsGlobal);
16441752
16451753 void initialize(Attributor &A) override {
1646 Function &F = getAnchorScope();
1647 unsigned AttrIdx = getIRPosition().getAttrIdx();
1648
1649 for (Attribute::AttrKind AK :
1650 {Attribute::Dereferenceable, Attribute::DereferenceableOrNull})
1651 if (F.getAttributes().hasAttribute(AttrIdx, AK))
1652 takeKnownDerefBytesMaximum(F.getAttribute(AttrIdx, AK).getValueAsInt());
1754 SmallVector Attrs;
1755 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
1756 Attrs);
1757 for (const Attribute &Attr : Attrs)
1758 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
16531759 }
16541760
16551761 /// See AbstractAttribute::getAsStr().
16651771 };
16661772
16671773 struct AADereferenceableReturned final : AADereferenceableImpl {
1668 AADereferenceableReturned(Function &F)
1669 : AADereferenceableImpl(F, IRP_RETURNED) {}
1774 AADereferenceableReturned(const IRPosition &IRP)
1775 : AADereferenceableImpl(IRP) {}
16701776
16711777 /// See AbstractAttribute::updateImpl(...).
16721778 ChangeStatus updateImpl(Attributor &A) override;
16911797 IsGlobal = false;
16921798
16931799 // First, we try to get information about V from Attributor.
1694 if (auto *DerefAA = A.getAAFor(*this, V)) {
1800 if (auto *DerefAA =
1801 A.getAAFor(*this, IRPosition::value(V))) {
16951802 IsNonNull &= DerefAA->isAssumedNonNull();
16961803 return DerefAA->getAssumedDereferenceableBytes();
16971804 }
17031810 APInt Offset(IdxWidth, 0);
17041811 Value *Base = V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
17051812
1706 if (auto *BaseDerefAA = A.getAAFor(*this, *Base)) {
1813 if (auto *BaseDerefAA =
1814 A.getAAFor(*this, IRPosition::value(*Base))) {
17071815 IsNonNull &= Offset != 0;
17081816 return calcDifferenceIfBaseIsNonNull(
17091817 BaseDerefAA->getAssumedDereferenceableBytes(), Offset.getSExtValue(),
17241832 }
17251833
17261834 ChangeStatus AADereferenceableReturned::updateImpl(Attributor &A) {
1727 Function &F = getAnchorScope();
17281835 auto BeforeState = static_cast(*this);
17291836
1730 syncNonNull(A.getAAFor(*this, F));
1837 syncNonNull(A.getAAFor(*this, getIRPosition()));
17311838
17321839 bool IsNonNull = isAssumedNonNull();
17331840 bool IsGlobal = isAssumedGlobal();
17381845 return isValidState();
17391846 };
17401847
1741 if (A.checkForAllReturnedValues(F, CheckReturnValue, *this)) {
1848 if (A.checkForAllReturnedValues(CheckReturnValue, *this)) {
17421849 updateAssumedNonNullGlobalState(IsNonNull, IsGlobal);
17431850 return BeforeState == static_cast(*this)
17441851 ? ChangeStatus::UNCHANGED
17481855 }
17491856
17501857 struct AADereferenceableArgument final : AADereferenceableImpl {
1751 AADereferenceableArgument(Argument &A) : AADereferenceableImpl(A) {}
1858 AADereferenceableArgument(const IRPosition &IRP)
1859 : AADereferenceableImpl(IRP) {}
17521860
17531861 /// See AbstractAttribute::updateImpl(...).
17541862 ChangeStatus updateImpl(Attributor &A) override;
17601868 };
17611869
17621870 ChangeStatus AADereferenceableArgument::updateImpl(Attributor &A) {
1763 Function &F = getAnchorScope();
17641871 Argument &Arg = cast(getAnchorValue());
17651872
17661873 auto BeforeState = static_cast(*this);
17671874
17681875 unsigned ArgNo = Arg.getArgNo();
17691876
1770 syncNonNull(A.getAAFor(*this, F, ArgNo));
1877 syncNonNull(A.getAAFor(*this, getIRPosition()));
17711878
17721879 bool IsNonNull = isAssumedNonNull();
17731880 bool IsGlobal = isAssumedGlobal();
17771884 assert(CS && "Sanity check: Call site was not initialized properly!");
17781885
17791886 // Check that DereferenceableAA is AADereferenceableCallSiteArgument.
1780 if (auto *DereferenceableAA =
1781 A.getAAFor(*this, *CS.getInstruction(), ArgNo)) {
1887 if (auto *DereferenceableAA = A.getAAFor(
1888 *this, IRPosition::callsite_argument(CS, ArgNo))) {
17821889 ImmutableCallSite ICS(
17831890 &DereferenceableAA->getIRPosition().getAnchorValue());
17841891 if (ICS && CS.getInstruction() == ICS.getInstruction()) {
17961903 return isValidState();
17971904 };
17981905
1799 if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
1906 if (!A.checkForAllCallSites(CallSiteCheck, *this, true))
18001907 return indicatePessimisticFixpoint();
18011908
18021909 updateAssumedNonNullGlobalState(IsNonNull, IsGlobal);
18071914
18081915 /// Dereferenceable attribute for a call site argument.
18091916 struct AADereferenceableCallSiteArgument final : AADereferenceableImpl {
1810 AADereferenceableCallSiteArgument(Instruction &I, unsigned ArgNo)
1811 : AADereferenceableImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
1812
1813 /// See AbstractAttribute::initialize(...).
1814 void initialize(Attributor &A) override {
1815 CallSite CS(&getAnchorValue());
1816 if (CS.paramHasAttr(getArgNo(), Attribute::Dereferenceable))
1817 takeKnownDerefBytesMaximum(CS.getDereferenceableBytes(getArgNo()));
1818
1819 if (CS.paramHasAttr(getArgNo(), Attribute::DereferenceableOrNull))
1820 takeKnownDerefBytesMaximum(CS.getDereferenceableOrNullBytes(getArgNo()));
1821 }
1917 AADereferenceableCallSiteArgument(const IRPosition &IRP)
1918 : AADereferenceableImpl(IRP) {}
18221919
18231920 /// See AbstractAttribute::updateImpl(Attributor &A).
18241921 ChangeStatus updateImpl(Attributor &A) override;
18341931 // If we do this, "dereferenceable" is always deduced because of the
18351932 // assumption.
18361933
1837 Value &V = *getAssociatedValue();
1934 Value &V = getAssociatedValue();
18381935
18391936 auto BeforeState = static_cast(*this);
18401937
1841 syncNonNull(A.getAAFor(*this, getAnchorValue(), getArgNo()));
1938 syncNonNull(A.getAAFor(*this, getIRPosition()));
18421939 bool IsNonNull = isAssumedNonNull();
18431940 bool IsGlobal = isKnownGlobal();
18441941
18531950 // ------------------------ Align Argument Attribute ------------------------
18541951
18551952 struct AAAlignImpl : AAAlign {
1856 IRPositionConstructorForward(AAAlignImpl, AAAlign);
1953 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
18571954
18581955 // Max alignemnt value allowed in IR
18591956 static const unsigned MAX_ALIGN = 1U << 29;
18681965 void initialize(Attributor &A) override {
18691966 takeAssumedMinimum(MAX_ALIGN);
18701967
1871 Function &F = getAnchorScope();
1872
1873 unsigned AttrIdx = getAttrIdx();
1874 // Already the function has align attribute on return value or argument.
1875 if (F.getAttributes().hasAttribute(AttrIdx, Attribute::Alignment))
1876 addKnownBits(
1877 F.getAttribute(AttrIdx, Attribute::Alignment).getAlignment());
1968 SmallVector Attrs;
1969 getAttrs({Attribute::Alignment}, Attrs);
1970 for (const Attribute &Attr : Attrs)
1971 takeKnownMaximum(Attr.getValueAsInt());
18781972 }
18791973
18801974 /// See AbstractAttribute::getDeducedAttributes
18871981
18881982 /// Align attribute for function return value.
18891983 struct AAAlignReturned final : AAAlignImpl {
1890 AAAlignReturned(Function &F) : AAAlignImpl(F, IRP_RETURNED) {}
1984 AAAlignReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {}
18911985
18921986 /// See AbstractAttribute::updateImpl(...).
18931987 ChangeStatus updateImpl(Attributor &A) override;
18991993 };
19001994
19011995 ChangeStatus AAAlignReturned::updateImpl(Attributor &A) {
1902 Function &F = getAnchorScope();
19031996
19041997 // Currently, align is deduced if alignments in return values are assumed
19051998 // as greater than n. We reach pessimistic fixpoint if any of the return value
19092002 base_t BeforeState = getAssumed();
19102003 auto CheckReturnValue =
19112004 [&](Value &RV, const SmallPtrSetImpl &RetInsts) -> bool {
1912 auto *AlignAA = A.getAAFor(*this, RV);
2005 auto *AlignAA = A.getAAFor(*this, IRPosition::value(RV));
19132006
19142007 if (AlignAA)
19152008 takeAssumedMinimum(AlignAA->getAssumedAlign());
19202013 return isValidState();
19212014 };
19222015
1923 if (!A.checkForAllReturnedValuesAndReturnInsts(F, CheckReturnValue, *this))
2016 if (!A.checkForAllReturnedValuesAndReturnInsts(CheckReturnValue, *this))
19242017 return indicatePessimisticFixpoint();
19252018
19262019 return (getAssumed() != BeforeState) ? ChangeStatus::CHANGED
19292022
19302023 /// Align attribute for function argument.
19312024 struct AAAlignArgument final : AAAlignImpl {
1932 AAAlignArgument(Argument &A) : AAAlignImpl(A) {}
2025 AAAlignArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {}
19332026
19342027 /// See AbstractAttribute::updateImpl(...).
19352028 virtual ChangeStatus updateImpl(Attributor &A) override;
19402033
19412034 ChangeStatus AAAlignArgument::updateImpl(Attributor &A) {
19422035
1943 Function &F = getAnchorScope();
19442036 Argument &Arg = cast(getAnchorValue());
19452037
19462038 unsigned ArgNo = Arg.getArgNo();
19522044 std::function CallSiteCheck = [&](CallSite CS) {
19532045 assert(CS && "Sanity check: Call site was not initialized properly!");
19542046
1955 auto *AlignAA = A.getAAFor(*this, *CS.getInstruction(), ArgNo);
2047 auto *AlignAA =
2048 A.getAAFor(*this, IRPosition::callsite_argument(CS, ArgNo));
19562049
19572050 // Check that AlignAA is AAAlignCallSiteArgument.
19582051 if (AlignAA) {
19682061 return isValidState();
19692062 };
19702063
1971 if (!A.checkForAllCallSites(F, CallSiteCheck, *this, true))
2064 if (!A.checkForAllCallSites(CallSiteCheck, *this, true))
19722065 indicatePessimisticFixpoint();
19732066
19742067 return BeforeState == getAssumed() ? ChangeStatus::UNCHANGED
19762069 }
19772070
19782071 struct AAAlignCallSiteArgument final : AAAlignImpl {
1979 AAAlignCallSiteArgument(Instruction &I, unsigned ArgNo)
1980 : AAAlignImpl(CallSite(&I).getArgOperand(ArgNo), I, ArgNo) {}
2072 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignImpl(IRP) {}
19812073
19822074 /// See AbstractAttribute::initialize(...).
19832075 void initialize(Attributor &A) override {
1984 CallSite CS(&getAnchorValue());
19852076 takeKnownMaximum(
1986 getAssociatedValue()->getPointerAlignment(A.getDataLayout()));
2077 getAssociatedValue().getPointerAlignment(A.getDataLayout()));
19872078 }
19882079
19892080 /// See AbstractAttribute::updateImpl(Attributor &A).
20012092
20022093 auto BeforeState = getAssumed();
20032094
2004 Value &V = *getAssociatedValue();
2005
2006 auto *AlignAA = A.getAAFor(*this, V);
2095 Value &V = getAssociatedValue();
2096 auto *AlignAA = A.getAAFor(*this, IRPosition::value(V));
20072097
20082098 if (AlignAA)
20092099 takeAssumedMinimum(AlignAA->getAssumedAlign());
20162106
20172107 /// ------------------ Function No-Return Attribute ----------------------------
20182108 struct AANoReturnImpl : public AANoReturn {
2019 IRPositionConstructorForward(AANoReturnImpl, AANoReturn);
2109 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
20202110
20212111 /// See AbstractAttribute::getAsStr().
20222112 const std::string getAsStr() const override {
20252115
20262116 /// See AbstractAttribute::initialize(...).
20272117 void initialize(Attributor &A) override {
2028 Function &F = getAnchorScope();
2029 if (F.hasFnAttribute(getAttrKind()))
2118 if (hasAttr({getAttrKind()}))
20302119 indicateOptimisticFixpoint();
20312120 }
20322121
20332122 /// See AbstractAttribute::updateImpl(Attributor &A).
20342123 virtual ChangeStatus updateImpl(Attributor &A) override {
2035 const Function &F = getAnchorScope();
20362124 auto CheckForNoReturn = [](Instruction &) { return false; };
2037 if (!A.checkForAllInstructions(F, CheckForNoReturn, *this,
2125 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
20382126 {(unsigned)Instruction::Ret}))
20392127 return indicatePessimisticFixpoint();
20402128 return ChangeStatus::UNCHANGED;
20422130 };
20432131
20442132 struct AANoReturnFunction final : AANoReturnImpl {
2045 AANoReturnFunction(Function &F) : AANoReturnImpl(F, IRP_FUNCTION) {}
2133 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
20462134
20472135 /// See AbstractAttribute::trackStatistics()
20482136 void trackStatistics() const override {
20542142 /// Attributor
20552143 /// ----------------------------------------------------------------------------
20562144
2057 bool Attributor::checkForAllCallSites(Function &F,
2058 std::function &Pred,
2145 bool Attributor::checkForAllCallSites(const function_ref &Pred,
20592146 const AbstractAttribute &QueryingAA,
20602147 bool RequireAllCallSites) {
20612148 // We can try to determine information from
20622149 // the call sites. However, this is only possible all call sites are known,
20632150 // hence the function has internal linkage.
2064 if (RequireAllCallSites && !F.hasInternalLinkage()) {
2151 const IRPosition &IRP = QueryingAA.getIRPosition();
2152 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2153 if (!AssociatedFunction)
2154 return false;
2155
2156 if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) {
20652157 LLVM_DEBUG(
20662158 dbgs()
2067 << "Attributor: Function " << F.getName()
2159 << "Attributor: Function " << AssociatedFunction->getName()
20682160 << " has no internal linkage, hence not all call sites are known\n");
20692161 return false;
20702162 }
20712163
2072 for (const Use &U : F.uses()) {
2164 for (const Use &U : AssociatedFunction->uses()) {
20732165 Instruction *I = cast(U.getUser());
2074 Function *AnchorValue = I->getParent()->getParent();
2075
2076 auto *LivenessAA = getAAFor(QueryingAA, *AnchorValue);
2166 Function *Caller = I->getFunction();
2167
2168 auto *LivenessAA =
2169 getAAFor(QueryingAA, IRPosition::function(*Caller));
20772170
20782171 // Skip dead calls.
20792172 if (LivenessAA && LivenessAA->isAssumedDead(I))
20852178 continue;
20862179
20872180 LLVM_DEBUG(dbgs() << "Attributor: User " << *U.getUser()
2088 << " is an invalid use of " << F.getName() << "\n");
2181 << " is an invalid use of "
2182 << AssociatedFunction->getName() << "\n");
20892183 return false;
20902184 }
20912185
21012195 }
21022196
21032197 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
2104 const Function &F,
21052198 const function_ref &)>
21062199 &Pred,
21072200 const AbstractAttribute &QueryingAA) {
21082201
2109 auto *AARetVal = getAAFor(QueryingAA, F);
2110 if (!AARetVal)
2202 const IRPosition &IRP = QueryingAA.getIRPosition();
2203 // Since we need to provide return instructions we have to have an exact
2204 // definition.
2205 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2206 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition())
21112207 return false;
21122208
2113 auto *LivenessAA = getAAFor(QueryingAA, F);
2209 // If this is a call site query we use the call site specific return values
2210 // and liveness information.
2211 const IRPosition &QueryIRP = IRPosition::function_scope(IRP);
2212 const auto &AARetVal = getAAFor(QueryingAA, QueryIRP);
2213 if (!AARetVal || !AARetVal->getState().isValidState())
2214 return false;
2215
2216 auto *LivenessAA =
2217 getAAFor(QueryingAA, IRPosition::function(*AssociatedFunction));
21142218 if (!LivenessAA)
21152219 return AARetVal->checkForAllReturnedValuesAndReturnInsts(Pred);
21162220
21292233 }
21302234
21312235 bool Attributor::checkForAllReturnedValues(
2132 const Function &F, const function_ref &Pred,
2236 const function_ref &Pred,
21332237 const AbstractAttribute &QueryingAA) {
21342238
2135 auto *AARetVal = getAAFor(QueryingAA, F);
2136 if (!AARetVal)
2239 const IRPosition &IRP = QueryingAA.getIRPosition();
2240 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2241 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition())
21372242 return false;
21382243
2139 auto *LivenessAA = getAAFor(QueryingAA, F);
2244 const IRPosition &QueryIRP = IRPosition::function_scope(IRP);
2245 const auto &AARetVal = getAAFor(QueryingAA, QueryIRP);
2246 if (!AARetVal || !AARetVal->getState().isValidState())
2247 return false;
2248
2249 auto *LivenessAA =
2250 getAAFor(QueryingAA, IRPosition::function(*AssociatedFunction));
21402251 if (!LivenessAA)
21412252 return AARetVal->checkForAllReturnedValuesAndReturnInsts(
21422253 [&](Value &RV, const SmallPtrSetImpl &) {
21542265 }
21552266
21562267 bool Attributor::checkForAllInstructions(
2157 const Function &F, const llvm::function_ref &Pred,
2268 const llvm::function_ref &Pred,
21582269 const AbstractAttribute &QueryingAA, const ArrayRef &Opcodes) {
21592270
2160 auto *LivenessAA = getAAFor(QueryingAA, F);
2161
2162 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
2271 const IRPosition &IRP = QueryingAA.getIRPosition();
2272 // Since we need to provide instructions we have to have an exact definition.
2273 const Function *AssociatedFunction = IRP.getAssociatedFunction();
2274 if (!AssociatedFunction || !AssociatedFunction->hasExactDefinition())
2275 return false;
2276
2277 const IRPosition &QueryIRP = IRPosition::function_scope(IRP);
2278 const auto &LivenessAA = getAAFor(QueryingAA, QueryIRP);
2279
2280 auto &OpcodeInstMap =
2281 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
21632282 for (unsigned Opcode : Opcodes) {
21642283 for (Instruction *I : OpcodeInstMap[Opcode]) {
21652284 // Skip dead instructions.
21752294 }
21762295
21772296 bool Attributor::checkForAllReadWriteInstructions(
2178 const Function &F, const llvm::function_ref &Pred,
2297 const llvm::function_ref &Pred,
21792298 AbstractAttribute &QueryingAA) {
21802299
2181 auto *LivenessAA = getAAFor(QueryingAA, F);
2182
2183 for (Instruction *I : InfoCache.getReadOrWriteInstsForFunction(F)) {
2300 const Function *AssociatedFunction =
2301 QueryingAA.getIRPosition().getAssociatedFunction();
2302 if (!AssociatedFunction)
2303 return false;
2304
2305 const auto &LivenessAA =
2306 getAAFor(QueryingAA, QueryingAA.getIRPosition());
2307
2308 for (Instruction *I :
2309 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
21842310 // Skip dead instructions.
21852311 if (LivenessAA && LivenessAA->isAssumedDead(I))
21862312 continue;
23302456 }
23312457
23322458 /// Helper function that checks if an abstract attribute of type \p AAType
2333 /// should be created for \p V (with argument number \p ArgNo) and if so creates
2334 /// and registers it with the Attributor \p A.
2459 /// should be created for IR position \p IRP and if so creates and registers it
2460 /// with the Attributor \p A.
23352461 ///
23362462 /// This method will look at the provided whitelist. If one is given and the
23372463 /// kind \p AAType::ID is not contained, no abstract attribute is created.
23382464 ///
23392465 /// \returns The created abstract argument, or nullptr if none was created.
2340 template
2341 static AAType *checkAndRegisterAA(const Function &F, Attributor &A,
2342 DenseSet *Whitelist,
2343 ValueType &V, int ArgNo, ArgsTy... Args) {
2466 template
2467 static AAType *checkAndRegisterAA(IRPosition &IRP, Attributor &A,
2468 DenseSet *Whitelist) {
23442469 if (Whitelist && !Whitelist->count(&AAType::ID))
23452470 return nullptr;
23462471
2347 return &A.registerAA(*new AAType(V, Args...), ArgNo);
2472 return &A.registerAA(*new AAType(IRP));
23482473 }
23492474
23502475 void Attributor::identifyDefaultAbstractAttributes(
23512476 Function &F, DenseSet *Whitelist) {
2477
2478 IRPosition FPos = IRPosition::function(F);
23522479
23532480 // Check for dead BasicBlocks in every function.
23542481 // We need dead instruction detection because we do not want to deal with
23552482 // broken IR in which SSA rules do not apply.
2356 checkAndRegisterAA(F, *this, /* Whitelist */ nullptr, F,
2357 -1);
2483 checkAndRegisterAA(FPos, *this, /* Whitelist */ nullptr);
23582484
23592485 // Every function might be "will-return".
2360 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2486 checkAndRegisterAA(FPos, *this, Whitelist);
23612487
23622488 // Every function can be nounwind.
2363 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2489 checkAndRegisterAA(FPos, *this, Whitelist);
23642490
23652491 // Every function might be marked "nosync"
2366 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2492 checkAndRegisterAA(FPos, *this, Whitelist);
23672493
23682494 // Every function might be "no-free".
2369 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2495 checkAndRegisterAA(FPos, *this, Whitelist);
23702496
23712497 // Every function might be "no-return".
2372 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2498 checkAndRegisterAA(FPos, *this, Whitelist);
23732499
23742500 // Return attributes are only appropriate if the return type is non void.
23752501 Type *ReturnType = F.getReturnType();
23762502 if (!ReturnType->isVoidTy()) {
23772503 // Argument attribute "returned" --- Create only one per function even
23782504 // though it is an argument attribute.
2379 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2505 checkAndRegisterAA(FPos, *this, Whitelist);
23802506
23812507 if (ReturnType->isPointerTy()) {
2508 IRPosition RetPos = IRPosition::returned(F);
2509
23822510 // Every function with pointer return type might be marked align.
2383 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2511 checkAndRegisterAA(RetPos, *this, Whitelist);
23842512
23852513 // Every function with pointer return type might be marked nonnull.
2386 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2514 checkAndRegisterAA(RetPos, *this, Whitelist);
23872515
23882516 // Every function with pointer return type might be marked noalias.
2389 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2517 checkAndRegisterAA(RetPos, *this, Whitelist);
23902518
23912519 // Every function with pointer return type might be marked
23922520 // dereferenceable.
2393 checkAndRegisterAA(F, *this, Whitelist, F, -1);
2521 checkAndRegisterAA(RetPos, *this, Whitelist);
23942522 }
23952523 }
23962524
23972525 for (Argument &Arg : F.args()) {
23982526 if (Arg.getType()->isPointerTy()) {
2527 IRPosition ArgPos = IRPosition::argument(Arg);
23992528 // Every argument with pointer type might be marked nonnull.
2400 checkAndRegisterAA(F, *this, Whitelist, Arg,
2401 Arg.getArgNo());
2529 checkAndRegisterAA(ArgPos, *this, Whitelist);
24022530
24032531 // Every argument with pointer type might be marked dereferenceable.
2404 checkAndRegisterAA(F, *this, Whitelist, Arg,
2405 Arg.getArgNo());
2532 checkAndRegisterAA(ArgPos, *this, Whitelist);
24062533
24072534 // Every argument with pointer type might be marked align.
2408 checkAndRegisterAA(F, *this, Whitelist, Arg,
2409 Arg.getArgNo());
2535 checkAndRegisterAA(ArgPos, *this, Whitelist);
24102536 }
24112537 }
24122538
24492575 for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) {
24502576 if (!CS.getArgument(i)->getType()->isPointerTy())
24512577 continue;
2578 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
24522579
24532580 // Call site argument attribute "non-null".
2454 checkAndRegisterAA(F, *this, Whitelist, I, i,
2455 i);
2581 checkAndRegisterAA(CSArgPos, *this,
2582 Whitelist);
24562583
24572584 // Call site argument attribute "dereferenceable".
2458 checkAndRegisterAA(
2459 F, *this, Whitelist, I, i, i);
2585 checkAndRegisterAA(CSArgPos, *this,
2586 Whitelist);
24602587
24612588 // Call site argument attribute "align".
2462 checkAndRegisterAA(F, *this, Whitelist, I, i,
2463 i);
2589 checkAndRegisterAA(CSArgPos, *this, Whitelist);
24642590 }
24652591 }
24662592 }
24752601
24762602 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
24772603 switch (AP) {
2604 case IRPosition::IRP_INVALID:
2605 return OS << "inv";
2606 case IRPosition::IRP_FLOAT:
2607 return OS << "flt";
2608 case IRPosition::IRP_RETURNED:
2609 return OS << "fn_ret";
2610 case IRPosition::IRP_CALL_SITE_RETURNED:
2611 return OS << "cs_ret";
2612 case IRPosition::IRP_FUNCTION:
2613 return OS << "fn";
2614 case IRPosition::IRP_CALL_SITE:
2615 return OS << "cs";
24782616 case IRPosition::IRP_ARGUMENT:
24792617 return OS << "arg";
24802618 case IRPosition::IRP_CALL_SITE_ARGUMENT:
24812619 return OS << "cs_arg";
2482 case IRPosition::IRP_FUNCTION:
2483 return OS << "fn";
2484 case IRPosition::IRP_RETURNED:
2485 return OS << "fn_ret";
24862620 }
24872621 llvm_unreachable("Unknown attribute position!");
24882622 }
24892623
24902624 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
2491 const Value *AV = Pos.getAssociatedValue();
2492 return OS << "{" << Pos.getPositionKind() << ":"
2493 << (AV ? AV->getName() : "n/a") << " ["
2625 const Value &AV = Pos.getAssociatedValue();
2626 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
24942627 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
24952628 }
24962629