1 //===- Attributor.cpp - Module-wide attribute deduction -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements an interprocedural pass that deduces and/or propagates
10 // attributes. This is done in an abstract interpretation style fixpoint
11 // iteration. See the Attributor.h file comment and the class descriptions in
12 // that file for more information.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Transforms/IPO/Attributor.h"
17
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/TinyPtrVector.h"
22 #include "llvm/Analysis/InlineCost.h"
23 #include "llvm/Analysis/LazyValueInfo.h"
24 #include "llvm/Analysis/MemorySSAUpdater.h"
25 #include "llvm/Analysis/MustExecute.h"
26 #include "llvm/Analysis/ValueTracking.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/NoFolder.h"
30 #include "llvm/IR/Verifier.h"
31 #include "llvm/InitializePasses.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/DebugCounter.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/GraphWriter.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40 #include "llvm/Transforms/Utils/Cloning.h"
41 #include "llvm/Transforms/Utils/Local.h"
42
43 #include <cassert>
44 #include <string>
45
46 using namespace llvm;
47
48 #define DEBUG_TYPE "attributor"
49
50 DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",
51 "Determine what attributes are manifested in the IR");
52
53 STATISTIC(NumFnDeleted, "Number of function deleted");
54 STATISTIC(NumFnWithExactDefinition,
55 "Number of functions with exact definitions");
56 STATISTIC(NumFnWithoutExactDefinition,
57 "Number of functions without exact definitions");
58 STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created");
59 STATISTIC(NumAttributesTimedOut,
60 "Number of abstract attributes timed out before fixpoint");
61 STATISTIC(NumAttributesValidFixpoint,
62 "Number of abstract attributes in a valid fixpoint state");
63 STATISTIC(NumAttributesManifested,
64 "Number of abstract attributes manifested in IR");
65 STATISTIC(NumAttributesFixedDueToRequiredDependences,
66 "Number of abstract attributes fixed due to required dependences");
67
68 // TODO: Determine a good default value.
69 //
70 // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
71 // (when run with the first 5 abstract attributes). The results also indicate
72 // that we never reach 32 iterations but always find a fixpoint sooner.
73 //
74 // This will become more evolved once we perform two interleaved fixpoint
75 // iterations: bottom-up and top-down.
76 static cl::opt<unsigned>
77 MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
78 cl::desc("Maximal number of fixpoint iterations."),
79 cl::init(32));
80
81 static cl::opt<unsigned, true> MaxInitializationChainLengthX(
82 "attributor-max-initialization-chain-length", cl::Hidden,
83 cl::desc(
84 "Maximal number of chained initializations (to avoid stack overflows)"),
85 cl::location(MaxInitializationChainLength), cl::init(1024));
86 unsigned llvm::MaxInitializationChainLength;
87
88 static cl::opt<bool> VerifyMaxFixpointIterations(
89 "attributor-max-iterations-verify", cl::Hidden,
90 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
91 cl::init(false));
92
93 static cl::opt<bool> AnnotateDeclarationCallSites(
94 "attributor-annotate-decl-cs", cl::Hidden,
95 cl::desc("Annotate call sites of function declarations."), cl::init(false));
96
97 static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
98 cl::init(true), cl::Hidden);
99
100 static cl::opt<bool>
101 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden,
102 cl::desc("Allow the Attributor to create shallow "
103 "wrappers for non-exact definitions."),
104 cl::init(false));
105
106 static cl::opt<bool>
107 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden,
108 cl::desc("Allow the Attributor to use IP information "
109 "derived from non-exact functions via cloning"),
110 cl::init(false));
111
112 // These options can only used for debug builds.
113 #ifndef NDEBUG
114 static cl::list<std::string>
115 SeedAllowList("attributor-seed-allow-list", cl::Hidden,
116 cl::desc("Comma seperated list of attribute names that are "
117 "allowed to be seeded."),
118 cl::ZeroOrMore, cl::CommaSeparated);
119
120 static cl::list<std::string> FunctionSeedAllowList(
121 "attributor-function-seed-allow-list", cl::Hidden,
122 cl::desc("Comma seperated list of function names that are "
123 "allowed to be seeded."),
124 cl::ZeroOrMore, cl::CommaSeparated);
125 #endif
126
127 static cl::opt<bool>
128 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden,
129 cl::desc("Dump the dependency graph to dot files."),
130 cl::init(false));
131
132 static cl::opt<std::string> DepGraphDotFileNamePrefix(
133 "attributor-depgraph-dot-filename-prefix", cl::Hidden,
134 cl::desc("The prefix used for the CallGraph dot file names."));
135
136 static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
137 cl::desc("View the dependency graph."),
138 cl::init(false));
139
140 static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
141 cl::desc("Print attribute dependencies"),
142 cl::init(false));
143
144 /// Logic operators for the change status enum class.
145 ///
146 ///{
operator |(ChangeStatus L,ChangeStatus R)147 ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) {
148 return L == ChangeStatus::CHANGED ? L : R;
149 }
operator &(ChangeStatus L,ChangeStatus R)150 ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) {
151 return L == ChangeStatus::UNCHANGED ? L : R;
152 }
153 ///}
154
155 /// Return true if \p New is equal or worse than \p Old.
isEqualOrWorse(const Attribute & New,const Attribute & Old)156 static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
157 if (!Old.isIntAttribute())
158 return true;
159
160 return Old.getValueAsInt() >= New.getValueAsInt();
161 }
162
163 /// Return true if the information provided by \p Attr was added to the
164 /// attribute list \p Attrs. This is only the case if it was not already present
165 /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
addIfNotExistent(LLVMContext & Ctx,const Attribute & Attr,AttributeList & Attrs,int AttrIdx)166 static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
167 AttributeList &Attrs, int AttrIdx) {
168
169 if (Attr.isEnumAttribute()) {
170 Attribute::AttrKind Kind = Attr.getKindAsEnum();
171 if (Attrs.hasAttribute(AttrIdx, Kind))
172 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
173 return false;
174 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
175 return true;
176 }
177 if (Attr.isStringAttribute()) {
178 StringRef Kind = Attr.getKindAsString();
179 if (Attrs.hasAttribute(AttrIdx, Kind))
180 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
181 return false;
182 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
183 return true;
184 }
185 if (Attr.isIntAttribute()) {
186 Attribute::AttrKind Kind = Attr.getKindAsEnum();
187 if (Attrs.hasAttribute(AttrIdx, Kind))
188 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
189 return false;
190 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
191 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
192 return true;
193 }
194
195 llvm_unreachable("Expected enum or string attribute!");
196 }
197
getAssociatedArgument() const198 Argument *IRPosition::getAssociatedArgument() const {
199 if (getPositionKind() == IRP_ARGUMENT)
200 return cast<Argument>(&getAnchorValue());
201
202 // Not an Argument and no argument number means this is not a call site
203 // argument, thus we cannot find a callback argument to return.
204 int ArgNo = getCallSiteArgNo();
205 if (ArgNo < 0)
206 return nullptr;
207
208 // Use abstract call sites to make the connection between the call site
209 // values and the ones in callbacks. If a callback was found that makes use
210 // of the underlying call site operand, we want the corresponding callback
211 // callee argument and not the direct callee argument.
212 Optional<Argument *> CBCandidateArg;
213 SmallVector<const Use *, 4> CallbackUses;
214 const auto &CB = cast<CallBase>(getAnchorValue());
215 AbstractCallSite::getCallbackUses(CB, CallbackUses);
216 for (const Use *U : CallbackUses) {
217 AbstractCallSite ACS(U);
218 assert(ACS && ACS.isCallbackCall());
219 if (!ACS.getCalledFunction())
220 continue;
221
222 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
223
224 // Test if the underlying call site operand is argument number u of the
225 // callback callee.
226 if (ACS.getCallArgOperandNo(u) != ArgNo)
227 continue;
228
229 assert(ACS.getCalledFunction()->arg_size() > u &&
230 "ACS mapped into var-args arguments!");
231 if (CBCandidateArg.hasValue()) {
232 CBCandidateArg = nullptr;
233 break;
234 }
235 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
236 }
237 }
238
239 // If we found a unique callback candidate argument, return it.
240 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
241 return CBCandidateArg.getValue();
242
243 // If no callbacks were found, or none used the underlying call site operand
244 // exclusively, use the direct callee argument if available.
245 const Function *Callee = CB.getCalledFunction();
246 if (Callee && Callee->arg_size() > unsigned(ArgNo))
247 return Callee->getArg(ArgNo);
248
249 return nullptr;
250 }
251
update(Attributor & A)252 ChangeStatus AbstractAttribute::update(Attributor &A) {
253 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
254 if (getState().isAtFixpoint())
255 return HasChanged;
256
257 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
258
259 HasChanged = updateImpl(A);
260
261 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
262 << "\n");
263
264 return HasChanged;
265 }
266
267 ChangeStatus
manifestAttrs(Attributor & A,const IRPosition & IRP,const ArrayRef<Attribute> & DeducedAttrs)268 IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
269 const ArrayRef<Attribute> &DeducedAttrs) {
270 Function *ScopeFn = IRP.getAnchorScope();
271 IRPosition::Kind PK = IRP.getPositionKind();
272
273 // In the following some generic code that will manifest attributes in
274 // DeducedAttrs if they improve the current IR. Due to the different
275 // annotation positions we use the underlying AttributeList interface.
276
277 AttributeList Attrs;
278 switch (PK) {
279 case IRPosition::IRP_INVALID:
280 case IRPosition::IRP_FLOAT:
281 return ChangeStatus::UNCHANGED;
282 case IRPosition::IRP_ARGUMENT:
283 case IRPosition::IRP_FUNCTION:
284 case IRPosition::IRP_RETURNED:
285 Attrs = ScopeFn->getAttributes();
286 break;
287 case IRPosition::IRP_CALL_SITE:
288 case IRPosition::IRP_CALL_SITE_RETURNED:
289 case IRPosition::IRP_CALL_SITE_ARGUMENT:
290 Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes();
291 break;
292 }
293
294 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
295 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
296 for (const Attribute &Attr : DeducedAttrs) {
297 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
298 continue;
299
300 HasChanged = ChangeStatus::CHANGED;
301 }
302
303 if (HasChanged == ChangeStatus::UNCHANGED)
304 return HasChanged;
305
306 switch (PK) {
307 case IRPosition::IRP_ARGUMENT:
308 case IRPosition::IRP_FUNCTION:
309 case IRPosition::IRP_RETURNED:
310 ScopeFn->setAttributes(Attrs);
311 break;
312 case IRPosition::IRP_CALL_SITE:
313 case IRPosition::IRP_CALL_SITE_RETURNED:
314 case IRPosition::IRP_CALL_SITE_ARGUMENT:
315 cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs);
316 break;
317 case IRPosition::IRP_INVALID:
318 case IRPosition::IRP_FLOAT:
319 break;
320 }
321
322 return HasChanged;
323 }
324
325 const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey());
326 const IRPosition
327 IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey());
328
SubsumingPositionIterator(const IRPosition & IRP)329 SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
330 IRPositions.emplace_back(IRP);
331
332 // Helper to determine if operand bundles on a call site are benin or
333 // potentially problematic. We handle only llvm.assume for now.
334 auto CanIgnoreOperandBundles = [](const CallBase &CB) {
335 return (isa<IntrinsicInst>(CB) &&
336 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
337 };
338
339 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
340 switch (IRP.getPositionKind()) {
341 case IRPosition::IRP_INVALID:
342 case IRPosition::IRP_FLOAT:
343 case IRPosition::IRP_FUNCTION:
344 return;
345 case IRPosition::IRP_ARGUMENT:
346 case IRPosition::IRP_RETURNED:
347 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
348 return;
349 case IRPosition::IRP_CALL_SITE:
350 assert(CB && "Expected call site!");
351 // TODO: We need to look at the operand bundles similar to the redirection
352 // in CallBase.
353 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
354 if (const Function *Callee = CB->getCalledFunction())
355 IRPositions.emplace_back(IRPosition::function(*Callee));
356 return;
357 case IRPosition::IRP_CALL_SITE_RETURNED:
358 assert(CB && "Expected call site!");
359 // TODO: We need to look at the operand bundles similar to the redirection
360 // in CallBase.
361 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
362 if (const Function *Callee = CB->getCalledFunction()) {
363 IRPositions.emplace_back(IRPosition::returned(*Callee));
364 IRPositions.emplace_back(IRPosition::function(*Callee));
365 for (const Argument &Arg : Callee->args())
366 if (Arg.hasReturnedAttr()) {
367 IRPositions.emplace_back(
368 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
369 IRPositions.emplace_back(
370 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
371 IRPositions.emplace_back(IRPosition::argument(Arg));
372 }
373 }
374 }
375 IRPositions.emplace_back(IRPosition::callsite_function(*CB));
376 return;
377 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
378 assert(CB && "Expected call site!");
379 // TODO: We need to look at the operand bundles similar to the redirection
380 // in CallBase.
381 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
382 const Function *Callee = CB->getCalledFunction();
383 if (Callee) {
384 if (Argument *Arg = IRP.getAssociatedArgument())
385 IRPositions.emplace_back(IRPosition::argument(*Arg));
386 IRPositions.emplace_back(IRPosition::function(*Callee));
387 }
388 }
389 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
390 return;
391 }
392 }
393 }
394
hasAttr(ArrayRef<Attribute::AttrKind> AKs,bool IgnoreSubsumingPositions,Attributor * A) const395 bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
396 bool IgnoreSubsumingPositions, Attributor *A) const {
397 SmallVector<Attribute, 4> Attrs;
398 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
399 for (Attribute::AttrKind AK : AKs)
400 if (EquivIRP.getAttrsFromIRAttr(AK, Attrs))
401 return true;
402 // The first position returned by the SubsumingPositionIterator is
403 // always the position itself. If we ignore subsuming positions we
404 // are done after the first iteration.
405 if (IgnoreSubsumingPositions)
406 break;
407 }
408 if (A)
409 for (Attribute::AttrKind AK : AKs)
410 if (getAttrsFromAssumes(AK, Attrs, *A))
411 return true;
412 return false;
413 }
414
getAttrs(ArrayRef<Attribute::AttrKind> AKs,SmallVectorImpl<Attribute> & Attrs,bool IgnoreSubsumingPositions,Attributor * A) const415 void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
416 SmallVectorImpl<Attribute> &Attrs,
417 bool IgnoreSubsumingPositions, Attributor *A) const {
418 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
419 for (Attribute::AttrKind AK : AKs)
420 EquivIRP.getAttrsFromIRAttr(AK, Attrs);
421 // The first position returned by the SubsumingPositionIterator is
422 // always the position itself. If we ignore subsuming positions we
423 // are done after the first iteration.
424 if (IgnoreSubsumingPositions)
425 break;
426 }
427 if (A)
428 for (Attribute::AttrKind AK : AKs)
429 getAttrsFromAssumes(AK, Attrs, *A);
430 }
431
getAttrsFromIRAttr(Attribute::AttrKind AK,SmallVectorImpl<Attribute> & Attrs) const432 bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
433 SmallVectorImpl<Attribute> &Attrs) const {
434 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
435 return false;
436
437 AttributeList AttrList;
438 if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
439 AttrList = CB->getAttributes();
440 else
441 AttrList = getAssociatedFunction()->getAttributes();
442
443 bool HasAttr = AttrList.hasAttribute(getAttrIdx(), AK);
444 if (HasAttr)
445 Attrs.push_back(AttrList.getAttribute(getAttrIdx(), AK));
446 return HasAttr;
447 }
448
getAttrsFromAssumes(Attribute::AttrKind AK,SmallVectorImpl<Attribute> & Attrs,Attributor & A) const449 bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK,
450 SmallVectorImpl<Attribute> &Attrs,
451 Attributor &A) const {
452 assert(getPositionKind() != IRP_INVALID && "Did expect a valid position!");
453 Value &AssociatedValue = getAssociatedValue();
454
455 const Assume2KnowledgeMap &A2K =
456 A.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
457
458 // Check if we found any potential assume use, if not we don't need to create
459 // explorer iterators.
460 if (A2K.empty())
461 return false;
462
463 LLVMContext &Ctx = AssociatedValue.getContext();
464 unsigned AttrsSize = Attrs.size();
465 MustBeExecutedContextExplorer &Explorer =
466 A.getInfoCache().getMustBeExecutedContextExplorer();
467 auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI());
468 for (auto &It : A2K)
469 if (Explorer.findInContextOf(It.first, EIt, EEnd))
470 Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
471 return AttrsSize != Attrs.size();
472 }
473
verify()474 void IRPosition::verify() {
475 #ifdef EXPENSIVE_CHECKS
476 switch (getPositionKind()) {
477 case IRP_INVALID:
478 assert(!Enc.getOpaqueValue() &&
479 "Expected a nullptr for an invalid position!");
480 return;
481 case IRP_FLOAT:
482 assert((!isa<CallBase>(&getAssociatedValue()) &&
483 !isa<Argument>(&getAssociatedValue())) &&
484 "Expected specialized kind for call base and argument values!");
485 return;
486 case IRP_RETURNED:
487 assert(isa<Function>(getAsValuePtr()) &&
488 "Expected function for a 'returned' position!");
489 assert(getAsValuePtr() == &getAssociatedValue() &&
490 "Associated value mismatch!");
491 return;
492 case IRP_CALL_SITE_RETURNED:
493 assert((isa<CallBase>(getAsValuePtr())) &&
494 "Expected call base for 'call site returned' position!");
495 assert(getAsValuePtr() == &getAssociatedValue() &&
496 "Associated value mismatch!");
497 return;
498 case IRP_CALL_SITE:
499 assert((isa<CallBase>(getAsValuePtr())) &&
500 "Expected call base for 'call site function' position!");
501 assert(getAsValuePtr() == &getAssociatedValue() &&
502 "Associated value mismatch!");
503 return;
504 case IRP_FUNCTION:
505 assert(isa<Function>(getAsValuePtr()) &&
506 "Expected function for a 'function' position!");
507 assert(getAsValuePtr() == &getAssociatedValue() &&
508 "Associated value mismatch!");
509 return;
510 case IRP_ARGUMENT:
511 assert(isa<Argument>(getAsValuePtr()) &&
512 "Expected argument for a 'argument' position!");
513 assert(getAsValuePtr() == &getAssociatedValue() &&
514 "Associated value mismatch!");
515 return;
516 case IRP_CALL_SITE_ARGUMENT: {
517 Use *U = getAsUsePtr();
518 assert(U && "Expected use for a 'call site argument' position!");
519 assert(isa<CallBase>(U->getUser()) &&
520 "Expected call base user for a 'call site argument' position!");
521 assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
522 "Expected call base argument operand for a 'call site argument' "
523 "position");
524 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
525 unsigned(getCallSiteArgNo()) &&
526 "Argument number mismatch!");
527 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
528 return;
529 }
530 }
531 #endif
532 }
533
534 Optional<Constant *>
getAssumedConstant(const Value & V,const AbstractAttribute & AA,bool & UsedAssumedInformation)535 Attributor::getAssumedConstant(const Value &V, const AbstractAttribute &AA,
536 bool &UsedAssumedInformation) {
537 const auto &ValueSimplifyAA = getAAFor<AAValueSimplify>(
538 AA, IRPosition::value(V), /* TrackDependence */ false);
539 Optional<Value *> SimplifiedV =
540 ValueSimplifyAA.getAssumedSimplifiedValue(*this);
541 bool IsKnown = ValueSimplifyAA.isKnown();
542 UsedAssumedInformation |= !IsKnown;
543 if (!SimplifiedV.hasValue()) {
544 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
545 return llvm::None;
546 }
547 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
548 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
549 return llvm::None;
550 }
551 Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue());
552 if (CI && CI->getType() != V.getType()) {
553 // TODO: Check for a save conversion.
554 return nullptr;
555 }
556 if (CI)
557 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
558 return CI;
559 }
560
~Attributor()561 Attributor::~Attributor() {
562 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
563 // thus we cannot delete them. We can, and want to, destruct them though.
564 for (auto &DepAA : DG.SyntheticRoot.Deps) {
565 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
566 AA->~AbstractAttribute();
567 }
568 }
569
isAssumedDead(const AbstractAttribute & AA,const AAIsDead * FnLivenessAA,bool CheckBBLivenessOnly,DepClassTy DepClass)570 bool Attributor::isAssumedDead(const AbstractAttribute &AA,
571 const AAIsDead *FnLivenessAA,
572 bool CheckBBLivenessOnly, DepClassTy DepClass) {
573 const IRPosition &IRP = AA.getIRPosition();
574 if (!Functions.count(IRP.getAnchorScope()))
575 return false;
576 return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass);
577 }
578
isAssumedDead(const Use & U,const AbstractAttribute * QueryingAA,const AAIsDead * FnLivenessAA,bool CheckBBLivenessOnly,DepClassTy DepClass)579 bool Attributor::isAssumedDead(const Use &U,
580 const AbstractAttribute *QueryingAA,
581 const AAIsDead *FnLivenessAA,
582 bool CheckBBLivenessOnly, DepClassTy DepClass) {
583 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
584 if (!UserI)
585 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
586 CheckBBLivenessOnly, DepClass);
587
588 if (auto *CB = dyn_cast<CallBase>(UserI)) {
589 // For call site argument uses we can check if the argument is
590 // unused/dead.
591 if (CB->isArgOperand(&U)) {
592 const IRPosition &CSArgPos =
593 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
594 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
595 CheckBBLivenessOnly, DepClass);
596 }
597 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
598 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
599 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly,
600 DepClass);
601 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
602 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
603 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
604 CheckBBLivenessOnly, DepClass);
605 }
606
607 return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA,
608 CheckBBLivenessOnly, DepClass);
609 }
610
isAssumedDead(const Instruction & I,const AbstractAttribute * QueryingAA,const AAIsDead * FnLivenessAA,bool CheckBBLivenessOnly,DepClassTy DepClass)611 bool Attributor::isAssumedDead(const Instruction &I,
612 const AbstractAttribute *QueryingAA,
613 const AAIsDead *FnLivenessAA,
614 bool CheckBBLivenessOnly, DepClassTy DepClass) {
615 if (!FnLivenessAA)
616 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()),
617 QueryingAA,
618 /* TrackDependence */ false);
619
620 // If we have a context instruction and a liveness AA we use it.
621 if (FnLivenessAA &&
622 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
623 FnLivenessAA->isAssumedDead(&I)) {
624 if (QueryingAA)
625 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
626 return true;
627 }
628
629 if (CheckBBLivenessOnly)
630 return false;
631
632 const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
633 IRPosition::value(I), QueryingAA, /* TrackDependence */ false);
634 // Don't check liveness for AAIsDead.
635 if (QueryingAA == &IsDeadAA)
636 return false;
637
638 if (IsDeadAA.isAssumedDead()) {
639 if (QueryingAA)
640 recordDependence(IsDeadAA, *QueryingAA, DepClass);
641 return true;
642 }
643
644 return false;
645 }
646
isAssumedDead(const IRPosition & IRP,const AbstractAttribute * QueryingAA,const AAIsDead * FnLivenessAA,bool CheckBBLivenessOnly,DepClassTy DepClass)647 bool Attributor::isAssumedDead(const IRPosition &IRP,
648 const AbstractAttribute *QueryingAA,
649 const AAIsDead *FnLivenessAA,
650 bool CheckBBLivenessOnly, DepClassTy DepClass) {
651 Instruction *CtxI = IRP.getCtxI();
652 if (CtxI &&
653 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA,
654 /* CheckBBLivenessOnly */ true,
655 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
656 return true;
657
658 if (CheckBBLivenessOnly)
659 return false;
660
661 // If we haven't succeeded we query the specific liveness info for the IRP.
662 const AAIsDead *IsDeadAA;
663 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
664 IsDeadAA = &getOrCreateAAFor<AAIsDead>(
665 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
666 QueryingAA, /* TrackDependence */ false);
667 else
668 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA,
669 /* TrackDependence */ false);
670 // Don't check liveness for AAIsDead.
671 if (QueryingAA == IsDeadAA)
672 return false;
673
674 if (IsDeadAA->isAssumedDead()) {
675 if (QueryingAA)
676 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
677 return true;
678 }
679
680 return false;
681 }
682
checkForAllUses(function_ref<bool (const Use &,bool &)> Pred,const AbstractAttribute & QueryingAA,const Value & V,DepClassTy LivenessDepClass)683 bool Attributor::checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
684 const AbstractAttribute &QueryingAA,
685 const Value &V, DepClassTy LivenessDepClass) {
686
687 // Check the trivial case first as it catches void values.
688 if (V.use_empty())
689 return true;
690
691 // If the value is replaced by another one, for now a constant, we do not have
692 // uses. Note that this requires users of `checkForAllUses` to not recurse but
693 // instead use the `follow` callback argument to look at transitive users,
694 // however, that should be clear from the presence of the argument.
695 bool UsedAssumedInformation = false;
696 Optional<Constant *> C =
697 getAssumedConstant(V, QueryingAA, UsedAssumedInformation);
698 if (C.hasValue() && C.getValue()) {
699 LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V
700 << " -> " << *C.getValue() << "\n");
701 return true;
702 }
703
704 const IRPosition &IRP = QueryingAA.getIRPosition();
705 SmallVector<const Use *, 16> Worklist;
706 SmallPtrSet<const Use *, 16> Visited;
707
708 for (const Use &U : V.uses())
709 Worklist.push_back(&U);
710
711 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
712 << " initial uses to check\n");
713
714 const Function *ScopeFn = IRP.getAnchorScope();
715 const auto *LivenessAA =
716 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
717 /* TrackDependence */ false)
718 : nullptr;
719
720 while (!Worklist.empty()) {
721 const Use *U = Worklist.pop_back_val();
722 if (!Visited.insert(U).second)
723 continue;
724 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "
725 << *U->getUser() << "\n");
726 if (isAssumedDead(*U, &QueryingAA, LivenessAA,
727 /* CheckBBLivenessOnly */ false, LivenessDepClass)) {
728 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
729 continue;
730 }
731 if (U->getUser()->isDroppable()) {
732 LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n");
733 continue;
734 }
735
736 bool Follow = false;
737 if (!Pred(*U, Follow))
738 return false;
739 if (!Follow)
740 continue;
741 for (const Use &UU : U->getUser()->uses())
742 Worklist.push_back(&UU);
743 }
744
745 return true;
746 }
747
checkForAllCallSites(function_ref<bool (AbstractCallSite)> Pred,const AbstractAttribute & QueryingAA,bool RequireAllCallSites,bool & AllCallSitesKnown)748 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
749 const AbstractAttribute &QueryingAA,
750 bool RequireAllCallSites,
751 bool &AllCallSitesKnown) {
752 // We can try to determine information from
753 // the call sites. However, this is only possible all call sites are known,
754 // hence the function has internal linkage.
755 const IRPosition &IRP = QueryingAA.getIRPosition();
756 const Function *AssociatedFunction = IRP.getAssociatedFunction();
757 if (!AssociatedFunction) {
758 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
759 << "\n");
760 AllCallSitesKnown = false;
761 return false;
762 }
763
764 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
765 &QueryingAA, AllCallSitesKnown);
766 }
767
checkForAllCallSites(function_ref<bool (AbstractCallSite)> Pred,const Function & Fn,bool RequireAllCallSites,const AbstractAttribute * QueryingAA,bool & AllCallSitesKnown)768 bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
769 const Function &Fn,
770 bool RequireAllCallSites,
771 const AbstractAttribute *QueryingAA,
772 bool &AllCallSitesKnown) {
773 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
774 LLVM_DEBUG(
775 dbgs()
776 << "[Attributor] Function " << Fn.getName()
777 << " has no internal linkage, hence not all call sites are known\n");
778 AllCallSitesKnown = false;
779 return false;
780 }
781
782 // If we do not require all call sites we might not see all.
783 AllCallSitesKnown = RequireAllCallSites;
784
785 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
786 for (unsigned u = 0; u < Uses.size(); ++u) {
787 const Use &U = *Uses[u];
788 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "
789 << *U.getUser() << "\n");
790 if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) {
791 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
792 continue;
793 }
794 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
795 if (CE->isCast() && CE->getType()->isPointerTy() &&
796 CE->getType()->getPointerElementType()->isFunctionTy()) {
797 for (const Use &CEU : CE->uses())
798 Uses.push_back(&CEU);
799 continue;
800 }
801 }
802
803 AbstractCallSite ACS(&U);
804 if (!ACS) {
805 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
806 << " has non call site use " << *U.get() << " in "
807 << *U.getUser() << "\n");
808 // BlockAddress users are allowed.
809 if (isa<BlockAddress>(U.getUser()))
810 continue;
811 return false;
812 }
813
814 const Use *EffectiveUse =
815 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
816 if (!ACS.isCallee(EffectiveUse)) {
817 if (!RequireAllCallSites)
818 continue;
819 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
820 << " is an invalid use of " << Fn.getName() << "\n");
821 return false;
822 }
823
824 // Make sure the arguments that can be matched between the call site and the
825 // callee argee on their type. It is unlikely they do not and it doesn't
826 // make sense for all attributes to know/care about this.
827 assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
828 unsigned MinArgsParams =
829 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
830 for (unsigned u = 0; u < MinArgsParams; ++u) {
831 Value *CSArgOp = ACS.getCallArgOperand(u);
832 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
833 LLVM_DEBUG(
834 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
835 << u << "@" << Fn.getName() << ": "
836 << *Fn.getArg(u)->getType() << " vs. "
837 << *ACS.getCallArgOperand(u)->getType() << "\n");
838 return false;
839 }
840 }
841
842 if (Pred(ACS))
843 continue;
844
845 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
846 << *ACS.getInstruction() << "\n");
847 return false;
848 }
849
850 return true;
851 }
852
checkForAllReturnedValuesAndReturnInsts(function_ref<bool (Value &,const SmallSetVector<ReturnInst *,4> &)> Pred,const AbstractAttribute & QueryingAA)853 bool Attributor::checkForAllReturnedValuesAndReturnInsts(
854 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
855 const AbstractAttribute &QueryingAA) {
856
857 const IRPosition &IRP = QueryingAA.getIRPosition();
858 // Since we need to provide return instructions we have to have an exact
859 // definition.
860 const Function *AssociatedFunction = IRP.getAssociatedFunction();
861 if (!AssociatedFunction)
862 return false;
863
864 // If this is a call site query we use the call site specific return values
865 // and liveness information.
866 // TODO: use the function scope once we have call site AAReturnedValues.
867 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
868 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
869 if (!AARetVal.getState().isValidState())
870 return false;
871
872 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
873 }
874
checkForAllReturnedValues(function_ref<bool (Value &)> Pred,const AbstractAttribute & QueryingAA)875 bool Attributor::checkForAllReturnedValues(
876 function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) {
877
878 const IRPosition &IRP = QueryingAA.getIRPosition();
879 const Function *AssociatedFunction = IRP.getAssociatedFunction();
880 if (!AssociatedFunction)
881 return false;
882
883 // TODO: use the function scope once we have call site AAReturnedValues.
884 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
885 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
886 if (!AARetVal.getState().isValidState())
887 return false;
888
889 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
890 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
891 return Pred(RV);
892 });
893 }
894
checkForAllInstructionsImpl(Attributor * A,InformationCache::OpcodeInstMapTy & OpcodeInstMap,function_ref<bool (Instruction &)> Pred,const AbstractAttribute * QueryingAA,const AAIsDead * LivenessAA,const ArrayRef<unsigned> & Opcodes,bool CheckBBLivenessOnly=false)895 static bool checkForAllInstructionsImpl(
896 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
897 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
898 const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes,
899 bool CheckBBLivenessOnly = false) {
900 for (unsigned Opcode : Opcodes) {
901 // Check if we have instructions with this opcode at all first.
902 auto *Insts = OpcodeInstMap.lookup(Opcode);
903 if (!Insts)
904 continue;
905
906 for (Instruction *I : *Insts) {
907 // Skip dead instructions.
908 if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA,
909 CheckBBLivenessOnly))
910 continue;
911
912 if (!Pred(*I))
913 return false;
914 }
915 }
916 return true;
917 }
918
checkForAllInstructions(function_ref<bool (Instruction &)> Pred,const AbstractAttribute & QueryingAA,const ArrayRef<unsigned> & Opcodes,bool CheckBBLivenessOnly)919 bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
920 const AbstractAttribute &QueryingAA,
921 const ArrayRef<unsigned> &Opcodes,
922 bool CheckBBLivenessOnly) {
923
924 const IRPosition &IRP = QueryingAA.getIRPosition();
925 // Since we need to provide instructions we have to have an exact definition.
926 const Function *AssociatedFunction = IRP.getAssociatedFunction();
927 if (!AssociatedFunction)
928 return false;
929
930 // TODO: use the function scope once we have call site AAReturnedValues.
931 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
932 const auto *LivenessAA =
933 CheckBBLivenessOnly ? nullptr
934 : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP,
935 /* TrackDependence */ false));
936
937 auto &OpcodeInstMap =
938 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
939 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
940 LivenessAA, Opcodes, CheckBBLivenessOnly))
941 return false;
942
943 return true;
944 }
945
checkForAllReadWriteInstructions(function_ref<bool (Instruction &)> Pred,AbstractAttribute & QueryingAA)946 bool Attributor::checkForAllReadWriteInstructions(
947 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA) {
948
949 const Function *AssociatedFunction =
950 QueryingAA.getIRPosition().getAssociatedFunction();
951 if (!AssociatedFunction)
952 return false;
953
954 // TODO: use the function scope once we have call site AAReturnedValues.
955 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
956 const auto &LivenessAA =
957 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
958
959 for (Instruction *I :
960 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
961 // Skip dead instructions.
962 if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA))
963 continue;
964
965 if (!Pred(*I))
966 return false;
967 }
968
969 return true;
970 }
971
runTillFixpoint()972 void Attributor::runTillFixpoint() {
973 TimeTraceScope TimeScope("Attributor::runTillFixpoint");
974 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
975 << DG.SyntheticRoot.Deps.size()
976 << " abstract attributes.\n");
977
978 // Now that all abstract attributes are collected and initialized we start
979 // the abstract analysis.
980
981 unsigned IterationCounter = 1;
982
983 SmallVector<AbstractAttribute *, 32> ChangedAAs;
984 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
985 Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end());
986
987 do {
988 // Remember the size to determine new attributes.
989 size_t NumAAs = DG.SyntheticRoot.Deps.size();
990 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
991 << ", Worklist size: " << Worklist.size() << "\n");
992
993 // For invalid AAs we can fix dependent AAs that have a required dependence,
994 // thereby folding long dependence chains in a single step without the need
995 // to run updates.
996 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
997 AbstractAttribute *InvalidAA = InvalidAAs[u];
998
999 // Check the dependences to fast track invalidation.
1000 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
1001 << InvalidAA->Deps.size()
1002 << " required & optional dependences\n");
1003 while (!InvalidAA->Deps.empty()) {
1004 const auto &Dep = InvalidAA->Deps.back();
1005 InvalidAA->Deps.pop_back();
1006 AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer());
1007 if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) {
1008 Worklist.insert(DepAA);
1009 continue;
1010 }
1011 DepAA->getState().indicatePessimisticFixpoint();
1012 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
1013 if (!DepAA->getState().isValidState())
1014 InvalidAAs.insert(DepAA);
1015 else
1016 ChangedAAs.push_back(DepAA);
1017 }
1018 }
1019
1020 // Add all abstract attributes that are potentially dependent on one that
1021 // changed to the work list.
1022 for (AbstractAttribute *ChangedAA : ChangedAAs)
1023 while (!ChangedAA->Deps.empty()) {
1024 Worklist.insert(
1025 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1026 ChangedAA->Deps.pop_back();
1027 }
1028
1029 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
1030 << ", Worklist+Dependent size: " << Worklist.size()
1031 << "\n");
1032
1033 // Reset the changed and invalid set.
1034 ChangedAAs.clear();
1035 InvalidAAs.clear();
1036
1037 // Update all abstract attribute in the work list and record the ones that
1038 // changed.
1039 for (AbstractAttribute *AA : Worklist) {
1040 const auto &AAState = AA->getState();
1041 if (!AAState.isAtFixpoint())
1042 if (updateAA(*AA) == ChangeStatus::CHANGED)
1043 ChangedAAs.push_back(AA);
1044
1045 // Use the InvalidAAs vector to propagate invalid states fast transitively
1046 // without requiring updates.
1047 if (!AAState.isValidState())
1048 InvalidAAs.insert(AA);
1049 }
1050
1051 // Add attributes to the changed set if they have been created in the last
1052 // iteration.
1053 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
1054 DG.SyntheticRoot.end());
1055
1056 // Reset the work list and repopulate with the changed abstract attributes.
1057 // Note that dependent ones are added above.
1058 Worklist.clear();
1059 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
1060
1061 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
1062 VerifyMaxFixpointIterations));
1063
1064 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
1065 << IterationCounter << "/" << MaxFixpointIterations
1066 << " iterations\n");
1067
1068 // Reset abstract arguments not settled in a sound fixpoint by now. This
1069 // happens when we stopped the fixpoint iteration early. Note that only the
1070 // ones marked as "changed" *and* the ones transitively depending on them
1071 // need to be reverted to a pessimistic state. Others might not be in a
1072 // fixpoint state but we can use the optimistic results for them anyway.
1073 SmallPtrSet<AbstractAttribute *, 32> Visited;
1074 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
1075 AbstractAttribute *ChangedAA = ChangedAAs[u];
1076 if (!Visited.insert(ChangedAA).second)
1077 continue;
1078
1079 AbstractState &State = ChangedAA->getState();
1080 if (!State.isAtFixpoint()) {
1081 State.indicatePessimisticFixpoint();
1082
1083 NumAttributesTimedOut++;
1084 }
1085
1086 while (!ChangedAA->Deps.empty()) {
1087 ChangedAAs.push_back(
1088 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1089 ChangedAA->Deps.pop_back();
1090 }
1091 }
1092
1093 LLVM_DEBUG({
1094 if (!Visited.empty())
1095 dbgs() << "\n[Attributor] Finalized " << Visited.size()
1096 << " abstract attributes.\n";
1097 });
1098
1099 if (VerifyMaxFixpointIterations &&
1100 IterationCounter != MaxFixpointIterations) {
1101 errs() << "\n[Attributor] Fixpoint iteration done after: "
1102 << IterationCounter << "/" << MaxFixpointIterations
1103 << " iterations\n";
1104 llvm_unreachable("The fixpoint was not reached with exactly the number of "
1105 "specified iterations!");
1106 }
1107 }
1108
manifestAttributes()1109 ChangeStatus Attributor::manifestAttributes() {
1110 TimeTraceScope TimeScope("Attributor::manifestAttributes");
1111 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
1112
1113 unsigned NumManifested = 0;
1114 unsigned NumAtFixpoint = 0;
1115 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
1116 for (auto &DepAA : DG.SyntheticRoot.Deps) {
1117 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
1118 AbstractState &State = AA->getState();
1119
1120 // If there is not already a fixpoint reached, we can now take the
1121 // optimistic state. This is correct because we enforced a pessimistic one
1122 // on abstract attributes that were transitively dependent on a changed one
1123 // already above.
1124 if (!State.isAtFixpoint())
1125 State.indicateOptimisticFixpoint();
1126
1127 // If the state is invalid, we do not try to manifest it.
1128 if (!State.isValidState())
1129 continue;
1130
1131 // Skip dead code.
1132 if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true))
1133 continue;
1134 // Check if the manifest debug counter that allows skipping manifestation of
1135 // AAs
1136 if (!DebugCounter::shouldExecute(ManifestDBGCounter))
1137 continue;
1138 // Manifest the state and record if we changed the IR.
1139 ChangeStatus LocalChange = AA->manifest(*this);
1140 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
1141 AA->trackStatistics();
1142 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
1143 << "\n");
1144
1145 ManifestChange = ManifestChange | LocalChange;
1146
1147 NumAtFixpoint++;
1148 NumManifested += (LocalChange == ChangeStatus::CHANGED);
1149 }
1150
1151 (void)NumManifested;
1152 (void)NumAtFixpoint;
1153 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
1154 << " arguments while " << NumAtFixpoint
1155 << " were in a valid fixpoint state\n");
1156
1157 NumAttributesManifested += NumManifested;
1158 NumAttributesValidFixpoint += NumAtFixpoint;
1159
1160 (void)NumFinalAAs;
1161 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
1162 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u)
1163 errs() << "Unexpected abstract attribute: "
1164 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1165 << " :: "
1166 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1167 ->getIRPosition()
1168 .getAssociatedValue()
1169 << "\n";
1170 llvm_unreachable("Expected the final number of abstract attributes to "
1171 "remain unchanged!");
1172 }
1173 return ManifestChange;
1174 }
1175
identifyDeadInternalFunctions()1176 void Attributor::identifyDeadInternalFunctions() {
1177 // Identify dead internal functions and delete them. This happens outside
1178 // the other fixpoint analysis as we might treat potentially dead functions
1179 // as live to lower the number of iterations. If they happen to be dead, the
1180 // below fixpoint loop will identify and eliminate them.
1181 SmallVector<Function *, 8> InternalFns;
1182 for (Function *F : Functions)
1183 if (F->hasLocalLinkage())
1184 InternalFns.push_back(F);
1185
1186 SmallPtrSet<Function *, 8> LiveInternalFns;
1187 bool FoundLiveInternal = true;
1188 while (FoundLiveInternal) {
1189 FoundLiveInternal = false;
1190 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
1191 Function *F = InternalFns[u];
1192 if (!F)
1193 continue;
1194
1195 bool AllCallSitesKnown;
1196 if (checkForAllCallSites(
1197 [&](AbstractCallSite ACS) {
1198 Function *Callee = ACS.getInstruction()->getFunction();
1199 return ToBeDeletedFunctions.count(Callee) ||
1200 (Functions.count(Callee) && Callee->hasLocalLinkage() &&
1201 !LiveInternalFns.count(Callee));
1202 },
1203 *F, true, nullptr, AllCallSitesKnown)) {
1204 continue;
1205 }
1206
1207 LiveInternalFns.insert(F);
1208 InternalFns[u] = nullptr;
1209 FoundLiveInternal = true;
1210 }
1211 }
1212
1213 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u)
1214 if (Function *F = InternalFns[u])
1215 ToBeDeletedFunctions.insert(F);
1216 }
1217
cleanupIR()1218 ChangeStatus Attributor::cleanupIR() {
1219 TimeTraceScope TimeScope("Attributor::cleanupIR");
1220 // Delete stuff at the end to avoid invalid references and a nice order.
1221 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
1222 << ToBeDeletedFunctions.size() << " functions and "
1223 << ToBeDeletedBlocks.size() << " blocks and "
1224 << ToBeDeletedInsts.size() << " instructions and "
1225 << ToBeChangedUses.size() << " uses\n");
1226
1227 SmallVector<WeakTrackingVH, 32> DeadInsts;
1228 SmallVector<Instruction *, 32> TerminatorsToFold;
1229
1230 for (auto &It : ToBeChangedUses) {
1231 Use *U = It.first;
1232 Value *NewV = It.second;
1233 Value *OldV = U->get();
1234
1235 // Do not replace uses in returns if the value is a must-tail call we will
1236 // not delete.
1237 if (isa<ReturnInst>(U->getUser()))
1238 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
1239 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
1240 continue;
1241
1242 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
1243 << " instead of " << *OldV << "\n");
1244 U->set(NewV);
1245 // Do not modify call instructions outside the SCC.
1246 if (auto *CB = dyn_cast<CallBase>(OldV))
1247 if (!Functions.count(CB->getCaller()))
1248 continue;
1249 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
1250 CGModifiedFunctions.insert(I->getFunction());
1251 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
1252 isInstructionTriviallyDead(I))
1253 DeadInsts.push_back(I);
1254 }
1255 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
1256 Instruction *UserI = cast<Instruction>(U->getUser());
1257 if (isa<UndefValue>(NewV)) {
1258 ToBeChangedToUnreachableInsts.insert(UserI);
1259 } else {
1260 TerminatorsToFold.push_back(UserI);
1261 }
1262 }
1263 }
1264 for (auto &V : InvokeWithDeadSuccessor)
1265 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
1266 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
1267 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
1268 bool Invoke2CallAllowed =
1269 !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction());
1270 assert((UnwindBBIsDead || NormalBBIsDead) &&
1271 "Invoke does not have dead successors!");
1272 BasicBlock *BB = II->getParent();
1273 BasicBlock *NormalDestBB = II->getNormalDest();
1274 if (UnwindBBIsDead) {
1275 Instruction *NormalNextIP = &NormalDestBB->front();
1276 if (Invoke2CallAllowed) {
1277 changeToCall(II);
1278 NormalNextIP = BB->getTerminator();
1279 }
1280 if (NormalBBIsDead)
1281 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
1282 } else {
1283 assert(NormalBBIsDead && "Broken invariant!");
1284 if (!NormalDestBB->getUniquePredecessor())
1285 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
1286 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
1287 }
1288 }
1289 for (Instruction *I : TerminatorsToFold) {
1290 CGModifiedFunctions.insert(I->getFunction());
1291 ConstantFoldTerminator(I->getParent());
1292 }
1293 for (auto &V : ToBeChangedToUnreachableInsts)
1294 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
1295 CGModifiedFunctions.insert(I->getFunction());
1296 changeToUnreachable(I, /* UseLLVMTrap */ false);
1297 }
1298
1299 for (auto &V : ToBeDeletedInsts) {
1300 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
1301 I->dropDroppableUses();
1302 CGModifiedFunctions.insert(I->getFunction());
1303 if (!I->getType()->isVoidTy())
1304 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1305 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
1306 DeadInsts.push_back(I);
1307 else
1308 I->eraseFromParent();
1309 }
1310 }
1311
1312 LLVM_DEBUG(dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size()
1313 << "\n");
1314
1315 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
1316
1317 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
1318 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
1319 ToBeDeletedBBs.reserve(NumDeadBlocks);
1320 for (BasicBlock *BB : ToBeDeletedBlocks) {
1321 CGModifiedFunctions.insert(BB->getParent());
1322 ToBeDeletedBBs.push_back(BB);
1323 }
1324 // Actually we do not delete the blocks but squash them into a single
1325 // unreachable but untangling branches that jump here is something we need
1326 // to do in a more generic way.
1327 DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
1328 }
1329
1330 identifyDeadInternalFunctions();
1331
1332 // Rewrite the functions as requested during manifest.
1333 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
1334
1335 for (Function *Fn : CGModifiedFunctions)
1336 if (!ToBeDeletedFunctions.count(Fn))
1337 CGUpdater.reanalyzeFunction(*Fn);
1338
1339 for (Function *Fn : ToBeDeletedFunctions) {
1340 if (!Functions.count(Fn))
1341 continue;
1342 CGUpdater.removeFunction(*Fn);
1343 }
1344
1345 if (!ToBeChangedUses.empty())
1346 ManifestChange = ChangeStatus::CHANGED;
1347
1348 if (!ToBeChangedToUnreachableInsts.empty())
1349 ManifestChange = ChangeStatus::CHANGED;
1350
1351 if (!ToBeDeletedFunctions.empty())
1352 ManifestChange = ChangeStatus::CHANGED;
1353
1354 if (!ToBeDeletedBlocks.empty())
1355 ManifestChange = ChangeStatus::CHANGED;
1356
1357 if (!ToBeDeletedInsts.empty())
1358 ManifestChange = ChangeStatus::CHANGED;
1359
1360 if (!InvokeWithDeadSuccessor.empty())
1361 ManifestChange = ChangeStatus::CHANGED;
1362
1363 if (!DeadInsts.empty())
1364 ManifestChange = ChangeStatus::CHANGED;
1365
1366 NumFnDeleted += ToBeDeletedFunctions.size();
1367
1368 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
1369 << " functions after manifest.\n");
1370
1371 #ifdef EXPENSIVE_CHECKS
1372 for (Function *F : Functions) {
1373 if (ToBeDeletedFunctions.count(F))
1374 continue;
1375 assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
1376 }
1377 #endif
1378
1379 return ManifestChange;
1380 }
1381
run()1382 ChangeStatus Attributor::run() {
1383 TimeTraceScope TimeScope("Attributor::run");
1384
1385 Phase = AttributorPhase::UPDATE;
1386 runTillFixpoint();
1387
1388 // dump graphs on demand
1389 if (DumpDepGraph)
1390 DG.dumpGraph();
1391
1392 if (ViewDepGraph)
1393 DG.viewGraph();
1394
1395 if (PrintDependencies)
1396 DG.print();
1397
1398 Phase = AttributorPhase::MANIFEST;
1399 ChangeStatus ManifestChange = manifestAttributes();
1400
1401 Phase = AttributorPhase::CLEANUP;
1402 ChangeStatus CleanupChange = cleanupIR();
1403
1404 return ManifestChange | CleanupChange;
1405 }
1406
updateAA(AbstractAttribute & AA)1407 ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
1408 TimeTraceScope TimeScope(
1409 AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) +
1410 "::updateAA");
1411 assert(Phase == AttributorPhase::UPDATE &&
1412 "We can update AA only in the update stage!");
1413
1414 // Use a new dependence vector for this update.
1415 DependenceVector DV;
1416 DependenceStack.push_back(&DV);
1417
1418 auto &AAState = AA.getState();
1419 ChangeStatus CS = ChangeStatus::UNCHANGED;
1420 if (!isAssumedDead(AA, nullptr, /* CheckBBLivenessOnly */ true))
1421 CS = AA.update(*this);
1422
1423 if (DV.empty()) {
1424 // If the attribute did not query any non-fix information, the state
1425 // will not change and we can indicate that right away.
1426 AAState.indicateOptimisticFixpoint();
1427 }
1428
1429 if (!AAState.isAtFixpoint())
1430 rememberDependences();
1431
1432 // Verify the stack was used properly, that is we pop the dependence vector we
1433 // put there earlier.
1434 DependenceVector *PoppedDV = DependenceStack.pop_back_val();
1435 (void)PoppedDV;
1436 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
1437
1438 return CS;
1439 }
1440
createShallowWrapper(Function & F)1441 void Attributor::createShallowWrapper(Function &F) {
1442 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
1443
1444 Module &M = *F.getParent();
1445 LLVMContext &Ctx = M.getContext();
1446 FunctionType *FnTy = F.getFunctionType();
1447
1448 Function *Wrapper =
1449 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
1450 F.setName(""); // set the inside function anonymous
1451 M.getFunctionList().insert(F.getIterator(), Wrapper);
1452
1453 F.setLinkage(GlobalValue::InternalLinkage);
1454
1455 F.replaceAllUsesWith(Wrapper);
1456 assert(F.use_empty() && "Uses remained after wrapper was created!");
1457
1458 // Move the COMDAT section to the wrapper.
1459 // TODO: Check if we need to keep it for F as well.
1460 Wrapper->setComdat(F.getComdat());
1461 F.setComdat(nullptr);
1462
1463 // Copy all metadata and attributes but keep them on F as well.
1464 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
1465 F.getAllMetadata(MDs);
1466 for (auto MDIt : MDs)
1467 Wrapper->addMetadata(MDIt.first, *MDIt.second);
1468 Wrapper->setAttributes(F.getAttributes());
1469
1470 // Create the call in the wrapper.
1471 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
1472
1473 SmallVector<Value *, 8> Args;
1474 Argument *FArgIt = F.arg_begin();
1475 for (Argument &Arg : Wrapper->args()) {
1476 Args.push_back(&Arg);
1477 Arg.setName((FArgIt++)->getName());
1478 }
1479
1480 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
1481 CI->setTailCall(true);
1482 CI->addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1483 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
1484
1485 NumFnShallowWrappersCreated++;
1486 }
1487
1488 /// Make another copy of the function \p F such that the copied version has
1489 /// internal linkage afterwards and can be analysed. Then we replace all uses
1490 /// of the original function to the copied one
1491 ///
1492 /// Only non-exactly defined functions that have `linkonce_odr` or `weak_odr`
1493 /// linkage can be internalized because these linkages guarantee that other
1494 /// definitions with the same name have the same semantics as this one
1495 ///
internalizeFunction(Function & F)1496 static Function *internalizeFunction(Function &F) {
1497 assert(AllowDeepWrapper && "Cannot create a copy if not allowed.");
1498 assert(!F.isDeclaration() && !F.hasExactDefinition() &&
1499 !GlobalValue::isInterposableLinkage(F.getLinkage()) &&
1500 "Trying to internalize function which cannot be internalized.");
1501
1502 Module &M = *F.getParent();
1503 FunctionType *FnTy = F.getFunctionType();
1504
1505 // create a copy of the current function
1506 Function *Copied = Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(),
1507 F.getName() + ".internalized");
1508 ValueToValueMapTy VMap;
1509 auto *NewFArgIt = Copied->arg_begin();
1510 for (auto &Arg : F.args()) {
1511 auto ArgName = Arg.getName();
1512 NewFArgIt->setName(ArgName);
1513 VMap[&Arg] = &(*NewFArgIt++);
1514 }
1515 SmallVector<ReturnInst *, 8> Returns;
1516
1517 // Copy the body of the original function to the new one
1518 CloneFunctionInto(Copied, &F, VMap, /* ModuleLevelChanges */ false, Returns);
1519
1520 // Set the linakage and visibility late as CloneFunctionInto has some implicit
1521 // requirements.
1522 Copied->setVisibility(GlobalValue::DefaultVisibility);
1523 Copied->setLinkage(GlobalValue::PrivateLinkage);
1524
1525 // Copy metadata
1526 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
1527 F.getAllMetadata(MDs);
1528 for (auto MDIt : MDs)
1529 Copied->addMetadata(MDIt.first, *MDIt.second);
1530
1531 M.getFunctionList().insert(F.getIterator(), Copied);
1532 F.replaceAllUsesWith(Copied);
1533 Copied->setDSOLocal(true);
1534
1535 return Copied;
1536 }
1537
isValidFunctionSignatureRewrite(Argument & Arg,ArrayRef<Type * > ReplacementTypes)1538 bool Attributor::isValidFunctionSignatureRewrite(
1539 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
1540
1541 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
1542 // Forbid the call site to cast the function return type. If we need to
1543 // rewrite these functions we need to re-create a cast for the new call site
1544 // (if the old had uses).
1545 if (!ACS.getCalledFunction() ||
1546 ACS.getInstruction()->getType() !=
1547 ACS.getCalledFunction()->getReturnType())
1548 return false;
1549 // Forbid must-tail calls for now.
1550 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
1551 };
1552
1553 Function *Fn = Arg.getParent();
1554 // Avoid var-arg functions for now.
1555 if (Fn->isVarArg()) {
1556 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
1557 return false;
1558 }
1559
1560 // Avoid functions with complicated argument passing semantics.
1561 AttributeList FnAttributeList = Fn->getAttributes();
1562 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
1563 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
1564 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
1565 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
1566 LLVM_DEBUG(
1567 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
1568 return false;
1569 }
1570
1571 // Avoid callbacks for now.
1572 bool AllCallSitesKnown;
1573 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
1574 AllCallSitesKnown)) {
1575 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
1576 return false;
1577 }
1578
1579 auto InstPred = [](Instruction &I) {
1580 if (auto *CI = dyn_cast<CallInst>(&I))
1581 return !CI->isMustTailCall();
1582 return true;
1583 };
1584
1585 // Forbid must-tail calls for now.
1586 // TODO:
1587 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
1588 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
1589 nullptr, {Instruction::Call})) {
1590 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
1591 return false;
1592 }
1593
1594 return true;
1595 }
1596
registerFunctionSignatureRewrite(Argument & Arg,ArrayRef<Type * > ReplacementTypes,ArgumentReplacementInfo::CalleeRepairCBTy && CalleeRepairCB,ArgumentReplacementInfo::ACSRepairCBTy && ACSRepairCB)1597 bool Attributor::registerFunctionSignatureRewrite(
1598 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
1599 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
1600 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
1601 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
1602 << Arg.getParent()->getName() << " with "
1603 << ReplacementTypes.size() << " replacements\n");
1604 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
1605 "Cannot register an invalid rewrite");
1606
1607 Function *Fn = Arg.getParent();
1608 SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
1609 ArgumentReplacementMap[Fn];
1610 if (ARIs.empty())
1611 ARIs.resize(Fn->arg_size());
1612
1613 // If we have a replacement already with less than or equal new arguments,
1614 // ignore this request.
1615 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
1616 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
1617 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
1618 return false;
1619 }
1620
1621 // If we have a replacement already but we like the new one better, delete
1622 // the old.
1623 ARI.reset();
1624
1625 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
1626 << Arg.getParent()->getName() << " with "
1627 << ReplacementTypes.size() << " replacements\n");
1628
1629 // Remember the replacement.
1630 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
1631 std::move(CalleeRepairCB),
1632 std::move(ACSRepairCB)));
1633
1634 return true;
1635 }
1636
shouldSeedAttribute(AbstractAttribute & AA)1637 bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
1638 bool Result = true;
1639 #ifndef NDEBUG
1640 if (SeedAllowList.size() != 0)
1641 Result =
1642 std::count(SeedAllowList.begin(), SeedAllowList.end(), AA.getName());
1643 Function *Fn = AA.getAnchorScope();
1644 if (FunctionSeedAllowList.size() != 0 && Fn)
1645 Result &= std::count(FunctionSeedAllowList.begin(),
1646 FunctionSeedAllowList.end(), Fn->getName());
1647 #endif
1648 return Result;
1649 }
1650
rewriteFunctionSignatures(SmallPtrSetImpl<Function * > & ModifiedFns)1651 ChangeStatus Attributor::rewriteFunctionSignatures(
1652 SmallPtrSetImpl<Function *> &ModifiedFns) {
1653 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1654
1655 for (auto &It : ArgumentReplacementMap) {
1656 Function *OldFn = It.getFirst();
1657
1658 // Deleted functions do not require rewrites.
1659 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
1660 continue;
1661
1662 const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
1663 It.getSecond();
1664 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
1665
1666 SmallVector<Type *, 16> NewArgumentTypes;
1667 SmallVector<AttributeSet, 16> NewArgumentAttributes;
1668
1669 // Collect replacement argument types and copy over existing attributes.
1670 AttributeList OldFnAttributeList = OldFn->getAttributes();
1671 for (Argument &Arg : OldFn->args()) {
1672 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
1673 ARIs[Arg.getArgNo()]) {
1674 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
1675 ARI->ReplacementTypes.end());
1676 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
1677 AttributeSet());
1678 } else {
1679 NewArgumentTypes.push_back(Arg.getType());
1680 NewArgumentAttributes.push_back(
1681 OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
1682 }
1683 }
1684
1685 FunctionType *OldFnTy = OldFn->getFunctionType();
1686 Type *RetTy = OldFnTy->getReturnType();
1687
1688 // Construct the new function type using the new arguments types.
1689 FunctionType *NewFnTy =
1690 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
1691
1692 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
1693 << "' from " << *OldFn->getFunctionType() << " to "
1694 << *NewFnTy << "\n");
1695
1696 // Create the new function body and insert it into the module.
1697 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
1698 OldFn->getAddressSpace(), "");
1699 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
1700 NewFn->takeName(OldFn);
1701 NewFn->copyAttributesFrom(OldFn);
1702
1703 // Patch the pointer to LLVM function in debug info descriptor.
1704 NewFn->setSubprogram(OldFn->getSubprogram());
1705 OldFn->setSubprogram(nullptr);
1706
1707 // Recompute the parameter attributes list based on the new arguments for
1708 // the function.
1709 LLVMContext &Ctx = OldFn->getContext();
1710 NewFn->setAttributes(AttributeList::get(
1711 Ctx, OldFnAttributeList.getFnAttributes(),
1712 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
1713
1714 // Since we have now created the new function, splice the body of the old
1715 // function right into the new function, leaving the old rotting hulk of the
1716 // function empty.
1717 NewFn->getBasicBlockList().splice(NewFn->begin(),
1718 OldFn->getBasicBlockList());
1719
1720 // Fixup block addresses to reference new function.
1721 SmallVector<BlockAddress *, 8u> BlockAddresses;
1722 for (User *U : OldFn->users())
1723 if (auto *BA = dyn_cast<BlockAddress>(U))
1724 BlockAddresses.push_back(BA);
1725 for (auto *BA : BlockAddresses)
1726 BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock()));
1727
1728 // Set of all "call-like" instructions that invoke the old function mapped
1729 // to their new replacements.
1730 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
1731
1732 // Callback to create a new "call-like" instruction for a given one.
1733 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
1734 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
1735 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
1736
1737 // Collect the new argument operands for the replacement call site.
1738 SmallVector<Value *, 16> NewArgOperands;
1739 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
1740 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
1741 unsigned NewFirstArgNum = NewArgOperands.size();
1742 (void)NewFirstArgNum; // only used inside assert.
1743 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
1744 ARIs[OldArgNum]) {
1745 if (ARI->ACSRepairCB)
1746 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
1747 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
1748 NewArgOperands.size() &&
1749 "ACS repair callback did not provide as many operand as new "
1750 "types were registered!");
1751 // TODO: Exose the attribute set to the ACS repair callback
1752 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
1753 AttributeSet());
1754 } else {
1755 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
1756 NewArgOperandAttributes.push_back(
1757 OldCallAttributeList.getParamAttributes(OldArgNum));
1758 }
1759 }
1760
1761 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
1762 "Mismatch # argument operands vs. # argument operand attributes!");
1763 assert(NewArgOperands.size() == NewFn->arg_size() &&
1764 "Mismatch # argument operands vs. # function arguments!");
1765
1766 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
1767 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
1768
1769 // Create a new call or invoke instruction to replace the old one.
1770 CallBase *NewCB;
1771 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
1772 NewCB =
1773 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
1774 NewArgOperands, OperandBundleDefs, "", OldCB);
1775 } else {
1776 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
1777 "", OldCB);
1778 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
1779 NewCB = NewCI;
1780 }
1781
1782 // Copy over various properties and the new attributes.
1783 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
1784 NewCB->setCallingConv(OldCB->getCallingConv());
1785 NewCB->takeName(OldCB);
1786 NewCB->setAttributes(AttributeList::get(
1787 Ctx, OldCallAttributeList.getFnAttributes(),
1788 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
1789
1790 CallSitePairs.push_back({OldCB, NewCB});
1791 return true;
1792 };
1793
1794 // Use the CallSiteReplacementCreator to create replacement call sites.
1795 bool AllCallSitesKnown;
1796 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
1797 true, nullptr, AllCallSitesKnown);
1798 (void)Success;
1799 assert(Success && "Assumed call site replacement to succeed!");
1800
1801 // Rewire the arguments.
1802 Argument *OldFnArgIt = OldFn->arg_begin();
1803 Argument *NewFnArgIt = NewFn->arg_begin();
1804 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
1805 ++OldArgNum, ++OldFnArgIt) {
1806 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
1807 ARIs[OldArgNum]) {
1808 if (ARI->CalleeRepairCB)
1809 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
1810 NewFnArgIt += ARI->ReplacementTypes.size();
1811 } else {
1812 NewFnArgIt->takeName(&*OldFnArgIt);
1813 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
1814 ++NewFnArgIt;
1815 }
1816 }
1817
1818 // Eliminate the instructions *after* we visited all of them.
1819 for (auto &CallSitePair : CallSitePairs) {
1820 CallBase &OldCB = *CallSitePair.first;
1821 CallBase &NewCB = *CallSitePair.second;
1822 assert(OldCB.getType() == NewCB.getType() &&
1823 "Cannot handle call sites with different types!");
1824 ModifiedFns.insert(OldCB.getFunction());
1825 CGUpdater.replaceCallSite(OldCB, NewCB);
1826 OldCB.replaceAllUsesWith(&NewCB);
1827 OldCB.eraseFromParent();
1828 }
1829
1830 // Replace the function in the call graph (if any).
1831 CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
1832
1833 // If the old function was modified and needed to be reanalyzed, the new one
1834 // does now.
1835 if (ModifiedFns.erase(OldFn))
1836 ModifiedFns.insert(NewFn);
1837
1838 Changed = ChangeStatus::CHANGED;
1839 }
1840
1841 return Changed;
1842 }
1843
initializeInformationCache(const Function & CF,FunctionInfo & FI)1844 void InformationCache::initializeInformationCache(const Function &CF,
1845 FunctionInfo &FI) {
1846 // As we do not modify the function here we can remove the const
1847 // withouth breaking implicit assumptions. At the end of the day, we could
1848 // initialize the cache eagerly which would look the same to the users.
1849 Function &F = const_cast<Function &>(CF);
1850
1851 // Walk all instructions to find interesting instructions that might be
1852 // queried by abstract attributes during their initialization or update.
1853 // This has to happen before we create attributes.
1854
1855 for (Instruction &I : instructions(&F)) {
1856 bool IsInterestingOpcode = false;
1857
1858 // To allow easy access to all instructions in a function with a given
1859 // opcode we store them in the InfoCache. As not all opcodes are interesting
1860 // to concrete attributes we only cache the ones that are as identified in
1861 // the following switch.
1862 // Note: There are no concrete attributes now so this is initially empty.
1863 switch (I.getOpcode()) {
1864 default:
1865 assert(!isa<CallBase>(&I) &&
1866 "New call base instruction type needs to be known in the "
1867 "Attributor.");
1868 break;
1869 case Instruction::Call:
1870 // Calls are interesting on their own, additionally:
1871 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
1872 // For `must-tail` calls we remember the caller and callee.
1873 if (IntrinsicInst *Assume = dyn_cast<IntrinsicInst>(&I)) {
1874 if (Assume->getIntrinsicID() == Intrinsic::assume)
1875 fillMapFromAssume(*Assume, KnowledgeMap);
1876 } else if (cast<CallInst>(I).isMustTailCall()) {
1877 FI.ContainsMustTailCall = true;
1878 if (const Function *Callee = cast<CallInst>(I).getCalledFunction())
1879 getFunctionInfo(*Callee).CalledViaMustTail = true;
1880 }
1881 LLVM_FALLTHROUGH;
1882 case Instruction::CallBr:
1883 case Instruction::Invoke:
1884 case Instruction::CleanupRet:
1885 case Instruction::CatchSwitch:
1886 case Instruction::AtomicRMW:
1887 case Instruction::AtomicCmpXchg:
1888 case Instruction::Br:
1889 case Instruction::Resume:
1890 case Instruction::Ret:
1891 case Instruction::Load:
1892 // The alignment of a pointer is interesting for loads.
1893 case Instruction::Store:
1894 // The alignment of a pointer is interesting for stores.
1895 IsInterestingOpcode = true;
1896 }
1897 if (IsInterestingOpcode) {
1898 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
1899 if (!Insts)
1900 Insts = new (Allocator) InstructionVectorTy();
1901 Insts->push_back(&I);
1902 }
1903 if (I.mayReadOrWriteMemory())
1904 FI.RWInsts.push_back(&I);
1905 }
1906
1907 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
1908 isInlineViable(F).isSuccess())
1909 InlineableFunctions.insert(&F);
1910 }
1911
getAAResultsForFunction(const Function & F)1912 AAResults *InformationCache::getAAResultsForFunction(const Function &F) {
1913 return AG.getAnalysis<AAManager>(F);
1914 }
1915
~FunctionInfo()1916 InformationCache::FunctionInfo::~FunctionInfo() {
1917 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
1918 // manually destroy them.
1919 for (auto &It : OpcodeInstMap)
1920 It.getSecond()->~InstructionVectorTy();
1921 }
1922
recordDependence(const AbstractAttribute & FromAA,const AbstractAttribute & ToAA,DepClassTy DepClass)1923 void Attributor::recordDependence(const AbstractAttribute &FromAA,
1924 const AbstractAttribute &ToAA,
1925 DepClassTy DepClass) {
1926 // If we are outside of an update, thus before the actual fixpoint iteration
1927 // started (= when we create AAs), we do not track dependences because we will
1928 // put all AAs into the initial worklist anyway.
1929 if (DependenceStack.empty())
1930 return;
1931 if (FromAA.getState().isAtFixpoint())
1932 return;
1933 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
1934 }
1935
rememberDependences()1936 void Attributor::rememberDependences() {
1937 assert(!DependenceStack.empty() && "No dependences to remember!");
1938
1939 for (DepInfo &DI : *DependenceStack.back()) {
1940 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
1941 DepAAs.push_back(AbstractAttribute::DepTy(
1942 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
1943 }
1944 }
1945
identifyDefaultAbstractAttributes(Function & F)1946 void Attributor::identifyDefaultAbstractAttributes(Function &F) {
1947 if (!VisitedFunctions.insert(&F).second)
1948 return;
1949 if (F.isDeclaration())
1950 return;
1951
1952 // In non-module runs we need to look at the call sites of a function to
1953 // determine if it is part of a must-tail call edge. This will influence what
1954 // attributes we can derive.
1955 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
1956 if (!isModulePass() && !FI.CalledViaMustTail) {
1957 for (const Use &U : F.uses())
1958 if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
1959 if (CB->isCallee(&U) && CB->isMustTailCall())
1960 FI.CalledViaMustTail = true;
1961 }
1962
1963 IRPosition FPos = IRPosition::function(F);
1964
1965 // Check for dead BasicBlocks in every function.
1966 // We need dead instruction detection because we do not want to deal with
1967 // broken IR in which SSA rules do not apply.
1968 getOrCreateAAFor<AAIsDead>(FPos);
1969
1970 // Every function might be "will-return".
1971 getOrCreateAAFor<AAWillReturn>(FPos);
1972
1973 // Every function might contain instructions that cause "undefined behavior".
1974 getOrCreateAAFor<AAUndefinedBehavior>(FPos);
1975
1976 // Every function can be nounwind.
1977 getOrCreateAAFor<AANoUnwind>(FPos);
1978
1979 // Every function might be marked "nosync"
1980 getOrCreateAAFor<AANoSync>(FPos);
1981
1982 // Every function might be "no-free".
1983 getOrCreateAAFor<AANoFree>(FPos);
1984
1985 // Every function might be "no-return".
1986 getOrCreateAAFor<AANoReturn>(FPos);
1987
1988 // Every function might be "no-recurse".
1989 getOrCreateAAFor<AANoRecurse>(FPos);
1990
1991 // Every function might be "readnone/readonly/writeonly/...".
1992 getOrCreateAAFor<AAMemoryBehavior>(FPos);
1993
1994 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
1995 getOrCreateAAFor<AAMemoryLocation>(FPos);
1996
1997 // Every function might be applicable for Heap-To-Stack conversion.
1998 if (EnableHeapToStack)
1999 getOrCreateAAFor<AAHeapToStack>(FPos);
2000
2001 // Return attributes are only appropriate if the return type is non void.
2002 Type *ReturnType = F.getReturnType();
2003 if (!ReturnType->isVoidTy()) {
2004 // Argument attribute "returned" --- Create only one per function even
2005 // though it is an argument attribute.
2006 getOrCreateAAFor<AAReturnedValues>(FPos);
2007
2008 IRPosition RetPos = IRPosition::returned(F);
2009
2010 // Every returned value might be dead.
2011 getOrCreateAAFor<AAIsDead>(RetPos);
2012
2013 // Every function might be simplified.
2014 getOrCreateAAFor<AAValueSimplify>(RetPos);
2015
2016 // Every returned value might be marked noundef.
2017 getOrCreateAAFor<AANoUndef>(RetPos);
2018
2019 if (ReturnType->isPointerTy()) {
2020
2021 // Every function with pointer return type might be marked align.
2022 getOrCreateAAFor<AAAlign>(RetPos);
2023
2024 // Every function with pointer return type might be marked nonnull.
2025 getOrCreateAAFor<AANonNull>(RetPos);
2026
2027 // Every function with pointer return type might be marked noalias.
2028 getOrCreateAAFor<AANoAlias>(RetPos);
2029
2030 // Every function with pointer return type might be marked
2031 // dereferenceable.
2032 getOrCreateAAFor<AADereferenceable>(RetPos);
2033 }
2034 }
2035
2036 for (Argument &Arg : F.args()) {
2037 IRPosition ArgPos = IRPosition::argument(Arg);
2038
2039 // Every argument might be simplified.
2040 getOrCreateAAFor<AAValueSimplify>(ArgPos);
2041
2042 // Every argument might be dead.
2043 getOrCreateAAFor<AAIsDead>(ArgPos);
2044
2045 // Every argument might be marked noundef.
2046 getOrCreateAAFor<AANoUndef>(ArgPos);
2047
2048 if (Arg.getType()->isPointerTy()) {
2049 // Every argument with pointer type might be marked nonnull.
2050 getOrCreateAAFor<AANonNull>(ArgPos);
2051
2052 // Every argument with pointer type might be marked noalias.
2053 getOrCreateAAFor<AANoAlias>(ArgPos);
2054
2055 // Every argument with pointer type might be marked dereferenceable.
2056 getOrCreateAAFor<AADereferenceable>(ArgPos);
2057
2058 // Every argument with pointer type might be marked align.
2059 getOrCreateAAFor<AAAlign>(ArgPos);
2060
2061 // Every argument with pointer type might be marked nocapture.
2062 getOrCreateAAFor<AANoCapture>(ArgPos);
2063
2064 // Every argument with pointer type might be marked
2065 // "readnone/readonly/writeonly/..."
2066 getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
2067
2068 // Every argument with pointer type might be marked nofree.
2069 getOrCreateAAFor<AANoFree>(ArgPos);
2070
2071 // Every argument with pointer type might be privatizable (or promotable)
2072 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
2073 }
2074 }
2075
2076 auto CallSitePred = [&](Instruction &I) -> bool {
2077 auto &CB = cast<CallBase>(I);
2078 IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2079
2080 // Call sites might be dead if they do not have side effects and no live
2081 // users. The return value might be dead if there are no live users.
2082 getOrCreateAAFor<AAIsDead>(CBRetPos);
2083
2084 Function *Callee = CB.getCalledFunction();
2085 // TODO: Even if the callee is not known now we might be able to simplify
2086 // the call/callee.
2087 if (!Callee)
2088 return true;
2089
2090 // Skip declarations except if annotations on their call sites were
2091 // explicitly requested.
2092 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
2093 !Callee->hasMetadata(LLVMContext::MD_callback))
2094 return true;
2095
2096 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
2097
2098 IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2099
2100 // Call site return integer values might be limited by a constant range.
2101 if (Callee->getReturnType()->isIntegerTy())
2102 getOrCreateAAFor<AAValueConstantRange>(CBRetPos);
2103 }
2104
2105 for (int I = 0, E = CB.getNumArgOperands(); I < E; ++I) {
2106
2107 IRPosition CBArgPos = IRPosition::callsite_argument(CB, I);
2108
2109 // Every call site argument might be dead.
2110 getOrCreateAAFor<AAIsDead>(CBArgPos);
2111
2112 // Call site argument might be simplified.
2113 getOrCreateAAFor<AAValueSimplify>(CBArgPos);
2114
2115 // Every call site argument might be marked "noundef".
2116 getOrCreateAAFor<AANoUndef>(CBArgPos);
2117
2118 if (!CB.getArgOperand(I)->getType()->isPointerTy())
2119 continue;
2120
2121 // Call site argument attribute "non-null".
2122 getOrCreateAAFor<AANonNull>(CBArgPos);
2123
2124 // Call site argument attribute "nocapture".
2125 getOrCreateAAFor<AANoCapture>(CBArgPos);
2126
2127 // Call site argument attribute "no-alias".
2128 getOrCreateAAFor<AANoAlias>(CBArgPos);
2129
2130 // Call site argument attribute "dereferenceable".
2131 getOrCreateAAFor<AADereferenceable>(CBArgPos);
2132
2133 // Call site argument attribute "align".
2134 getOrCreateAAFor<AAAlign>(CBArgPos);
2135
2136 // Call site argument attribute
2137 // "readnone/readonly/writeonly/..."
2138 getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);
2139
2140 // Call site argument attribute "nofree".
2141 getOrCreateAAFor<AANoFree>(CBArgPos);
2142 }
2143 return true;
2144 };
2145
2146 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
2147 bool Success;
2148 Success = checkForAllInstructionsImpl(
2149 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
2150 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2151 (unsigned)Instruction::Call});
2152 (void)Success;
2153 assert(Success && "Expected the check call to be successful!");
2154
2155 auto LoadStorePred = [&](Instruction &I) -> bool {
2156 if (isa<LoadInst>(I))
2157 getOrCreateAAFor<AAAlign>(
2158 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
2159 else
2160 getOrCreateAAFor<AAAlign>(
2161 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
2162 return true;
2163 };
2164 Success = checkForAllInstructionsImpl(
2165 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
2166 {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
2167 (void)Success;
2168 assert(Success && "Expected the check call to be successful!");
2169 }
2170
2171 /// Helpers to ease debugging through output streams and print calls.
2172 ///
2173 ///{
operator <<(raw_ostream & OS,ChangeStatus S)2174 raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
2175 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
2176 }
2177
operator <<(raw_ostream & OS,IRPosition::Kind AP)2178 raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
2179 switch (AP) {
2180 case IRPosition::IRP_INVALID:
2181 return OS << "inv";
2182 case IRPosition::IRP_FLOAT:
2183 return OS << "flt";
2184 case IRPosition::IRP_RETURNED:
2185 return OS << "fn_ret";
2186 case IRPosition::IRP_CALL_SITE_RETURNED:
2187 return OS << "cs_ret";
2188 case IRPosition::IRP_FUNCTION:
2189 return OS << "fn";
2190 case IRPosition::IRP_CALL_SITE:
2191 return OS << "cs";
2192 case IRPosition::IRP_ARGUMENT:
2193 return OS << "arg";
2194 case IRPosition::IRP_CALL_SITE_ARGUMENT:
2195 return OS << "cs_arg";
2196 }
2197 llvm_unreachable("Unknown attribute position!");
2198 }
2199
operator <<(raw_ostream & OS,const IRPosition & Pos)2200 raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
2201 const Value &AV = Pos.getAssociatedValue();
2202 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
2203 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo()
2204 << "]}";
2205 }
2206
operator <<(raw_ostream & OS,const IntegerRangeState & S)2207 raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
2208 OS << "range-state(" << S.getBitWidth() << ")<";
2209 S.getKnown().print(OS);
2210 OS << " / ";
2211 S.getAssumed().print(OS);
2212 OS << ">";
2213
2214 return OS << static_cast<const AbstractState &>(S);
2215 }
2216
operator <<(raw_ostream & OS,const AbstractState & S)2217 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
2218 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
2219 }
2220
operator <<(raw_ostream & OS,const AbstractAttribute & AA)2221 raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
2222 AA.print(OS);
2223 return OS;
2224 }
2225
operator <<(raw_ostream & OS,const PotentialConstantIntValuesState & S)2226 raw_ostream &llvm::operator<<(raw_ostream &OS,
2227 const PotentialConstantIntValuesState &S) {
2228 OS << "set-state(< {";
2229 if (!S.isValidState())
2230 OS << "full-set";
2231 else {
2232 for (auto &it : S.getAssumedSet())
2233 OS << it << ", ";
2234 if (S.undefIsContained())
2235 OS << "undef ";
2236 }
2237 OS << "} >)";
2238
2239 return OS;
2240 }
2241
print(raw_ostream & OS) const2242 void AbstractAttribute::print(raw_ostream &OS) const {
2243 OS << "[";
2244 OS << getName();
2245 OS << "] for CtxI ";
2246
2247 if (auto *I = getCtxI()) {
2248 OS << "'";
2249 I->print(OS);
2250 OS << "'";
2251 } else
2252 OS << "<<null inst>>";
2253
2254 OS << " at position " << getIRPosition() << " with state " << getAsStr()
2255 << '\n';
2256 }
2257
printWithDeps(raw_ostream & OS) const2258 void AbstractAttribute::printWithDeps(raw_ostream &OS) const {
2259 print(OS);
2260
2261 for (const auto &DepAA : Deps) {
2262 auto *AA = DepAA.getPointer();
2263 OS << " updates ";
2264 AA->print(OS);
2265 }
2266
2267 OS << '\n';
2268 }
2269 ///}
2270
2271 /// ----------------------------------------------------------------------------
2272 /// Pass (Manager) Boilerplate
2273 /// ----------------------------------------------------------------------------
2274
runAttributorOnFunctions(InformationCache & InfoCache,SetVector<Function * > & Functions,AnalysisGetter & AG,CallGraphUpdater & CGUpdater)2275 static bool runAttributorOnFunctions(InformationCache &InfoCache,
2276 SetVector<Function *> &Functions,
2277 AnalysisGetter &AG,
2278 CallGraphUpdater &CGUpdater) {
2279 if (Functions.empty())
2280 return false;
2281
2282 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
2283 << " functions.\n");
2284
2285 // Create an Attributor and initially empty information cache that is filled
2286 // while we identify default attribute opportunities.
2287 Attributor A(Functions, InfoCache, CGUpdater);
2288
2289 // Create shallow wrappers for all functions that are not IPO amendable
2290 if (AllowShallowWrappers)
2291 for (Function *F : Functions)
2292 if (!A.isFunctionIPOAmendable(*F))
2293 Attributor::createShallowWrapper(*F);
2294
2295 // Internalize non-exact functions
2296 // TODO: for now we eagerly internalize functions without calculating the
2297 // cost, we need a cost interface to determine whether internalizing
2298 // a function is "benefitial"
2299 if (AllowDeepWrapper) {
2300 unsigned FunSize = Functions.size();
2301 for (unsigned u = 0; u < FunSize; u++) {
2302 Function *F = Functions[u];
2303 if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() &&
2304 !GlobalValue::isInterposableLinkage(F->getLinkage())) {
2305 Function *NewF = internalizeFunction(*F);
2306 Functions.insert(NewF);
2307
2308 // Update call graph
2309 CGUpdater.replaceFunctionWith(*F, *NewF);
2310 for (const Use &U : NewF->uses())
2311 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
2312 auto *CallerF = CB->getCaller();
2313 CGUpdater.reanalyzeFunction(*CallerF);
2314 }
2315 }
2316 }
2317 }
2318
2319 for (Function *F : Functions) {
2320 if (F->hasExactDefinition())
2321 NumFnWithExactDefinition++;
2322 else
2323 NumFnWithoutExactDefinition++;
2324
2325 // We look at internal functions only on-demand but if any use is not a
2326 // direct call or outside the current set of analyzed functions, we have
2327 // to do it eagerly.
2328 if (F->hasLocalLinkage()) {
2329 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
2330 const auto *CB = dyn_cast<CallBase>(U.getUser());
2331 return CB && CB->isCallee(&U) &&
2332 Functions.count(const_cast<Function *>(CB->getCaller()));
2333 }))
2334 continue;
2335 }
2336
2337 // Populate the Attributor with abstract attribute opportunities in the
2338 // function and the information cache with IR information.
2339 A.identifyDefaultAbstractAttributes(*F);
2340 }
2341
2342 ChangeStatus Changed = A.run();
2343
2344 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
2345 << " functions, result: " << Changed << ".\n");
2346 return Changed == ChangeStatus::CHANGED;
2347 }
2348
viewGraph()2349 void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
2350
dumpGraph()2351 void AADepGraph::dumpGraph() {
2352 static std::atomic<int> CallTimes;
2353 std::string Prefix;
2354
2355 if (!DepGraphDotFileNamePrefix.empty())
2356 Prefix = DepGraphDotFileNamePrefix;
2357 else
2358 Prefix = "dep_graph";
2359 std::string Filename =
2360 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
2361
2362 outs() << "Dependency graph dump to " << Filename << ".\n";
2363
2364 std::error_code EC;
2365
2366 raw_fd_ostream File(Filename, EC, sys::fs::OF_Text);
2367 if (!EC)
2368 llvm::WriteGraph(File, this);
2369
2370 CallTimes++;
2371 }
2372
print()2373 void AADepGraph::print() {
2374 for (auto DepAA : SyntheticRoot.Deps)
2375 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
2376 }
2377
run(Module & M,ModuleAnalysisManager & AM)2378 PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
2379 FunctionAnalysisManager &FAM =
2380 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
2381 AnalysisGetter AG(FAM);
2382
2383 SetVector<Function *> Functions;
2384 for (Function &F : M)
2385 Functions.insert(&F);
2386
2387 CallGraphUpdater CGUpdater;
2388 BumpPtrAllocator Allocator;
2389 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
2390 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
2391 // FIXME: Think about passes we will preserve and add them here.
2392 return PreservedAnalyses::none();
2393 }
2394 return PreservedAnalyses::all();
2395 }
2396
run(LazyCallGraph::SCC & C,CGSCCAnalysisManager & AM,LazyCallGraph & CG,CGSCCUpdateResult & UR)2397 PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
2398 CGSCCAnalysisManager &AM,
2399 LazyCallGraph &CG,
2400 CGSCCUpdateResult &UR) {
2401 FunctionAnalysisManager &FAM =
2402 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2403 AnalysisGetter AG(FAM);
2404
2405 SetVector<Function *> Functions;
2406 for (LazyCallGraph::Node &N : C)
2407 Functions.insert(&N.getFunction());
2408
2409 if (Functions.empty())
2410 return PreservedAnalyses::all();
2411
2412 Module &M = *Functions.back()->getParent();
2413 CallGraphUpdater CGUpdater;
2414 CGUpdater.initialize(CG, C, AM, UR);
2415 BumpPtrAllocator Allocator;
2416 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
2417 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
2418 // FIXME: Think about passes we will preserve and add them here.
2419 PreservedAnalyses PA;
2420 PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
2421 return PA;
2422 }
2423 return PreservedAnalyses::all();
2424 }
2425
2426 namespace llvm {
2427
2428 template <> struct GraphTraits<AADepGraphNode *> {
2429 using NodeRef = AADepGraphNode *;
2430 using DepTy = PointerIntPair<AADepGraphNode *, 1>;
2431 using EdgeRef = PointerIntPair<AADepGraphNode *, 1>;
2432
getEntryNodellvm::GraphTraits2433 static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; }
DepGetValllvm::GraphTraits2434 static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); }
2435
2436 using ChildIteratorType =
2437 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2438 using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator;
2439
child_beginllvm::GraphTraits2440 static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); }
2441
child_endllvm::GraphTraits2442 static ChildIteratorType child_end(NodeRef N) { return N->child_end(); }
2443 };
2444
2445 template <>
2446 struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> {
getEntryNodellvm::GraphTraits2447 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
2448
2449 using nodes_iterator =
2450 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2451
nodes_beginllvm::GraphTraits2452 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
2453
nodes_endllvm::GraphTraits2454 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
2455 };
2456
2457 template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits2458 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
2459
getNodeLabelllvm::DOTGraphTraits2460 static std::string getNodeLabel(const AADepGraphNode *Node,
2461 const AADepGraph *DG) {
2462 std::string AAString = "";
2463 raw_string_ostream O(AAString);
2464 Node->print(O);
2465 return AAString;
2466 }
2467 };
2468
2469 } // end namespace llvm
2470
2471 namespace {
2472
2473 struct AttributorLegacyPass : public ModulePass {
2474 static char ID;
2475
AttributorLegacyPass__anonff3353ac0a11::AttributorLegacyPass2476 AttributorLegacyPass() : ModulePass(ID) {
2477 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
2478 }
2479
runOnModule__anonff3353ac0a11::AttributorLegacyPass2480 bool runOnModule(Module &M) override {
2481 if (skipModule(M))
2482 return false;
2483
2484 AnalysisGetter AG;
2485 SetVector<Function *> Functions;
2486 for (Function &F : M)
2487 Functions.insert(&F);
2488
2489 CallGraphUpdater CGUpdater;
2490 BumpPtrAllocator Allocator;
2491 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
2492 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
2493 }
2494
getAnalysisUsage__anonff3353ac0a11::AttributorLegacyPass2495 void getAnalysisUsage(AnalysisUsage &AU) const override {
2496 // FIXME: Think about passes we will preserve and add them here.
2497 AU.addRequired<TargetLibraryInfoWrapperPass>();
2498 }
2499 };
2500
2501 struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
2502 static char ID;
2503
AttributorCGSCCLegacyPass__anonff3353ac0a11::AttributorCGSCCLegacyPass2504 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
2505 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
2506 }
2507
runOnSCC__anonff3353ac0a11::AttributorCGSCCLegacyPass2508 bool runOnSCC(CallGraphSCC &SCC) override {
2509 if (skipSCC(SCC))
2510 return false;
2511
2512 SetVector<Function *> Functions;
2513 for (CallGraphNode *CGN : SCC)
2514 if (Function *Fn = CGN->getFunction())
2515 if (!Fn->isDeclaration())
2516 Functions.insert(Fn);
2517
2518 if (Functions.empty())
2519 return false;
2520
2521 AnalysisGetter AG;
2522 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
2523 CallGraphUpdater CGUpdater;
2524 CGUpdater.initialize(CG, SCC);
2525 Module &M = *Functions.back()->getParent();
2526 BumpPtrAllocator Allocator;
2527 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
2528 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
2529 }
2530
getAnalysisUsage__anonff3353ac0a11::AttributorCGSCCLegacyPass2531 void getAnalysisUsage(AnalysisUsage &AU) const override {
2532 // FIXME: Think about passes we will preserve and add them here.
2533 AU.addRequired<TargetLibraryInfoWrapperPass>();
2534 CallGraphSCCPass::getAnalysisUsage(AU);
2535 }
2536 };
2537
2538 } // end anonymous namespace
2539
createAttributorLegacyPass()2540 Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
createAttributorCGSCCLegacyPass()2541 Pass *llvm::createAttributorCGSCCLegacyPass() {
2542 return new AttributorCGSCCLegacyPass();
2543 }
2544
2545 char AttributorLegacyPass::ID = 0;
2546 char AttributorCGSCCLegacyPass::ID = 0;
2547
2548 INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
2549 "Deduce and propagate attributes", false, false)
2550 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2551 INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
2552 "Deduce and propagate attributes", false, false)
2553 INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
2554 "Deduce and propagate attributes (CGSCC pass)", false,
2555 false)
2556 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2557 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
2558 INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
2559 "Deduce and propagate attributes (CGSCC pass)", false,
2560 false)
2561