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