LLVM 24.0.0git
AliasAnalysis.cpp
Go to the documentation of this file.
1//==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==//
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 the generic AliasAnalysis interface which is used as the
10// common interface used by all clients and implementations of alias analysis.
11//
12// This file also implements the default version of the AliasAnalysis interface
13// that is to be used when no other implementation is specified. This does some
14// simple tests that detect obvious cases: two different global pointers cannot
15// alias, a global cannot alias a malloc, two different mallocs cannot alias,
16// etc.
17//
18// This alias analysis implementation really isn't very good for anything, but
19// it is very fast, and makes a nice clean default implementation. Because it
20// handles lots of little corner cases, other, more complex, alias analysis
21// implementations may choose to rely on this pass to resolve these simple and
22// easy cases.
23//
24//===----------------------------------------------------------------------===//
25
27#include "AnalysisOptions.h"
28#include "llvm/ADT/Statistic.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Type.h"
44#include "llvm/IR/Value.h"
46#include "llvm/Pass.h"
49#include <cassert>
50#include <functional>
51#include <iterator>
52
53#define DEBUG_TYPE "aa"
54
55using namespace llvm;
56
57STATISTIC(NumNoAlias, "Number of NoAlias results");
58STATISTIC(NumMayAlias, "Number of MayAlias results");
59STATISTIC(NumMustAlias, "Number of MustAlias results");
60
61AAResults::AAResults(const TargetLibraryInfo &TLI) : TLI(TLI) {}
62
64 : TLI(Arg.TLI), AAs(std::move(Arg.AAs)), AADeps(std::move(Arg.AADeps)) {}
65
66AAResults::~AAResults() = default;
67
69 FunctionAnalysisManager::Invalidator &Inv) {
70 // AAResults preserves the AAManager by default, due to the stateless nature
71 // of AliasAnalysis. There is no need to check whether it has been preserved
72 // explicitly. Check if any module dependency was invalidated and caused the
73 // AAManager to be invalidated. Invalidate ourselves in that case.
74 auto PAC = PA.getChecker<AAManager>();
75 if (!PAC.preservedWhenStateless())
76 return true;
77
78 // Check if any of the function dependencies were invalidated, and invalidate
79 // ourselves in that case.
80 for (AnalysisKey *ID : AADeps)
81 if (Inv.invalidate(ID, F, PA))
82 return true;
83
84 // Everything we depend on is still fine, so are we. Nothing to invalidate.
85 return false;
86}
87
88//===----------------------------------------------------------------------===//
89// Default chaining methods
90//===----------------------------------------------------------------------===//
91
93 const MemoryLocation &LocB) {
94 SimpleAAQueryInfo AAQIP(*this);
95 return alias(LocA, LocB, AAQIP, nullptr);
96}
97
99 const MemoryLocation &LocB, AAQueryInfo &AAQI,
100 const Instruction *CtxI) {
101 assert(LocA.Ptr->getType()->isPointerTy() &&
102 LocB.Ptr->getType()->isPointerTy() &&
103 "Can only call alias() on pointers");
105
106#ifndef NDEBUG
107 const bool EnableAATrace =
108 getAnalysisOptions(LocA.Ptr->getContext()).aa_trace;
109#else
110 constexpr bool EnableAATrace = false;
111#endif
112 if (EnableAATrace) {
113 for (unsigned I = 0; I < AAQI.Depth; ++I)
114 dbgs() << " ";
115 dbgs() << "Start " << *LocA.Ptr << " @ " << LocA.Size << ", "
116 << *LocB.Ptr << " @ " << LocB.Size << "\n";
117 }
118
119 AAQI.Depth++;
120 for (const auto &AA : AAs) {
121 Result = AA->alias(LocA, LocB, AAQI, CtxI);
122 if (Result != AliasResult::MayAlias)
123 break;
124 }
125 AAQI.Depth--;
126
127 if (EnableAATrace) {
128 for (unsigned I = 0; I < AAQI.Depth; ++I)
129 dbgs() << " ";
130 dbgs() << "End " << *LocA.Ptr << " @ " << LocA.Size << ", "
131 << *LocB.Ptr << " @ " << LocB.Size << " = " << Result << "\n";
132 }
133
134 if (AAQI.Depth == 0) {
135 if (Result == AliasResult::NoAlias)
136 ++NumNoAlias;
137 else if (Result == AliasResult::MustAlias)
138 ++NumMustAlias;
139 else
140 ++NumMayAlias;
141 }
142 return Result;
143}
144
146 const Instruction *CtxI) {
148
149 for (const auto &AA : AAs) {
150 Result = AA->aliasErrno(Loc, CtxI);
151 if (Result != AliasResult::MayAlias)
152 break;
153 }
154
155 return Result;
156}
157
159 bool IgnoreLocals) {
160 SimpleAAQueryInfo AAQIP(*this);
161 return getModRefInfoMask(Loc, AAQIP, IgnoreLocals);
162}
163
165 AAQueryInfo &AAQI, bool IgnoreLocals) {
167
168 for (const auto &AA : AAs) {
169 Result &= AA->getModRefInfoMask(Loc, AAQI, IgnoreLocals);
170
171 // Early-exit the moment we reach the bottom of the lattice.
172 if (isNoModRef(Result))
174 }
175
176 return Result;
177}
178
181
182 for (const auto &AA : AAs) {
183 Result &= AA->getArgModRefInfo(Call, ArgIdx);
184
185 // Early-exit the moment we reach the bottom of the lattice.
186 if (isNoModRef(Result))
188 }
189
190 return Result;
191}
192
194 const CallBase *Call2) {
195 SimpleAAQueryInfo AAQIP(*this);
196 return getModRefInfo(I, Call2, AAQIP);
197}
198
200 AAQueryInfo &AAQI) {
201 // We may have two calls.
202 if (const auto *Call1 = dyn_cast<CallBase>(I)) {
203 // Check if the two calls modify the same memory.
204 return getModRefInfo(Call1, Call2, AAQI);
205 }
206 // If this is a fence, just return ModRef.
207 if (I->isFenceLike())
208 return ModRefInfo::ModRef;
209 // Otherwise, check if the call modifies or references the
210 // location this memory access defines. The best we can say
211 // is that if the call references what this instruction
212 // defines, it must be clobbered by this location.
213 const MemoryLocation DefLoc = MemoryLocation::get(I);
214 ModRefInfo MR = getModRefInfo(Call2, DefLoc, AAQI);
215 if (isModOrRefSet(MR))
216 return ModRefInfo::ModRef;
218}
219
221 const MemoryLocation &Loc,
222 AAQueryInfo &AAQI) {
224
225 for (const auto &AA : AAs) {
226 Result &= AA->getModRefInfo(Call, Loc, AAQI);
227
228 // Early-exit the moment we reach the bottom of the lattice.
229 if (isNoModRef(Result))
231 }
232
233 // Apply the ModRef mask. This ensures that if Loc is a constant memory
234 // location, we take into account the fact that the call definitely could not
235 // modify the memory location.
236 if (!isNoModRef(Result))
237 Result &= getModRefInfoMask(Loc);
238
239 return Result;
240}
241
244 const MemoryEffects CallDef) {
245
247 auto addModRefInfoForLoc = [&](IRMemLocation L) {
248 ModRefInfo UseMR = CallUse.getModRef(L);
249 if (UseMR == ModRefInfo::NoModRef)
250 return;
251 ModRefInfo DefMR = CallDef.getModRef(L);
252 if (DefMR == ModRefInfo::NoModRef)
253 return;
254 if (DefMR == ModRefInfo::Ref && DefMR == UseMR)
255 return;
256 Result |= UseMR;
257 };
258
259 addModRefInfoForLoc(IRMemLocation::InaccessibleMem);
261 addModRefInfoForLoc(Loc);
262 return Result;
263}
264
266 const CallBase *Call2, AAQueryInfo &AAQI) {
268
269 for (const auto &AA : AAs) {
270 Result &= AA->getModRefInfo(Call1, Call2, AAQI);
271
272 // Early-exit the moment we reach the bottom of the lattice.
273 if (isNoModRef(Result))
275 }
276
277 // Try to refine the mod-ref info further using other API entry points to the
278 // aggregate set of AA results.
279
280 // If Call1 or Call2 are readnone, they don't interact.
281 auto Call1B = getMemoryEffects(Call1, AAQI);
282 if (Call1B.doesNotAccessMemory())
284
285 auto Call2B = getMemoryEffects(Call2, AAQI);
286 if (Call2B.doesNotAccessMemory())
288
289 // If they both only read from memory, there is no dependence.
290 if (Call1B.onlyReadsMemory() && Call2B.onlyReadsMemory())
292
293 // If Call1 only reads memory, the only dependence on Call2 can be
294 // from Call1 reading memory written by Call2.
295 if (Call1B.onlyReadsMemory())
296 Result &= ModRefInfo::Ref;
297 else if (Call1B.onlyWritesMemory())
298 Result &= ModRefInfo::Mod;
299
300 // If Call2 only access memory through arguments, accumulate the mod/ref
301 // information from Call1's references to the memory referenced by
302 // Call2's arguments.
303 if (Call2B.onlyAccessesArgPointees()) {
304 if (!Call2B.doesAccessArgPointees())
307 for (auto I = Call2->arg_begin(), E = Call2->arg_end(); I != E; ++I) {
308 const Value *Arg = *I;
309 if (!Arg->getType()->isPointerTy())
310 continue;
311 unsigned Call2ArgIdx = std::distance(Call2->arg_begin(), I);
312 auto Call2ArgLoc =
313 MemoryLocation::getForArgument(Call2, Call2ArgIdx, TLI);
314
315 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
316 // dependence of Call1 on that location is the inverse:
317 // - If Call2 modifies location, dependence exists if Call1 reads or
318 // writes.
319 // - If Call2 only reads location, dependence exists if Call1 writes.
320 ModRefInfo ArgModRefC2 = getArgModRefInfo(Call2, Call2ArgIdx);
322 if (isModSet(ArgModRefC2))
323 ArgMask = ModRefInfo::ModRef;
324 else if (isRefSet(ArgModRefC2))
325 ArgMask = ModRefInfo::Mod;
326
327 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
328 // above ArgMask to update dependence info.
329 ArgMask &= getModRefInfo(Call1, Call2ArgLoc, AAQI);
330
331 R = (R | ArgMask) & Result;
332 if (R == Result)
333 break;
334 }
335
336 return R;
337 }
338
339 // If Call1 only accesses memory through arguments, check if Call2 references
340 // any of the memory referenced by Call1's arguments. If not, return NoModRef.
341 if (Call1B.onlyAccessesArgPointees()) {
342 if (!Call1B.doesAccessArgPointees())
345 for (auto I = Call1->arg_begin(), E = Call1->arg_end(); I != E; ++I) {
346 const Value *Arg = *I;
347 if (!Arg->getType()->isPointerTy())
348 continue;
349 unsigned Call1ArgIdx = std::distance(Call1->arg_begin(), I);
350 auto Call1ArgLoc =
351 MemoryLocation::getForArgument(Call1, Call1ArgIdx, TLI);
352
353 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
354 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
355 // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
356 ModRefInfo ArgModRefC1 = getArgModRefInfo(Call1, Call1ArgIdx);
357 ModRefInfo ModRefC2 = getModRefInfo(Call2, Call1ArgLoc, AAQI);
358 if ((isModSet(ArgModRefC1) && isModOrRefSet(ModRefC2)) ||
359 (isRefSet(ArgModRefC1) && isModSet(ModRefC2)))
360 R = (R | ArgModRefC1) & Result;
361
362 if (R == Result)
363 break;
364 }
365
366 return R;
367 }
368
369 // If only Inaccessible and Target Memory Location have set ModRefInfo
370 // then check the relation between the same locations.
371 if (Call1B.onlyAccessesInaccessibleOrTargetMem() &&
372 Call2B.onlyAccessesInaccessibleOrTargetMem())
373 return getModRefInfoInaccessibleAndTargetMemLoc(Call1B, Call2B);
374
375 return Result;
376}
377
379 const Instruction *I2) {
380 SimpleAAQueryInfo AAQIP(*this);
381 return getModRefInfo(I1, I2, AAQIP);
382}
383
385 const Instruction *I2, AAQueryInfo &AAQI) {
386 // Early-exit if either instruction does not read or write memory.
387 if (!I1->mayReadOrWriteMemory() || !I2->mayReadOrWriteMemory())
389
390 if (const auto *Call2 = dyn_cast<CallBase>(I2))
391 return getModRefInfo(I1, Call2, AAQI);
392
393 // FIXME: We can have a more precise result.
396}
397
399 AAQueryInfo &AAQI) {
401
402 for (const auto &AA : AAs) {
403 Result &= AA->getMemoryEffects(Call, AAQI);
404
405 // Early-exit the moment we reach the bottom of the lattice.
406 if (Result.doesNotAccessMemory())
407 return Result;
408 }
409
410 return Result;
411}
412
417
420
421 for (const auto &AA : AAs) {
422 Result &= AA->getMemoryEffects(F);
423
424 // Early-exit the moment we reach the bottom of the lattice.
425 if (Result.doesNotAccessMemory())
426 return Result;
427 }
428
429 return Result;
430}
431
433 switch (AR) {
435 OS << "NoAlias";
436 break;
438 OS << "MustAlias";
439 break;
441 OS << "MayAlias";
442 break;
444 OS << "PartialAlias";
445 if (AR.hasOffset())
446 OS << " (off " << AR.getOffset() << ")";
447 break;
448 }
449 return OS;
450}
451
452//===----------------------------------------------------------------------===//
453// Helper method implementation
454//===----------------------------------------------------------------------===//
455
457 AAQueryInfo &AAQI) {
458 if (!Loc.Ptr)
459 return ModRefInfo::ModRef;
460
461 // If the location is *never* captured, it cannot be affected by
462 // synchronizing operations. However, we cannot ignore locations that are
463 // only captured after the operation, as the synchronization may still have
464 // an effect if the object is only captured *later*. As such, set I to null
465 // and ReturnCaptures to true here.
466 const Value *Obj = getUnderlyingObject(Loc.Ptr);
468 Obj, /*I=*/nullptr, /*OrAt=*/true, /*ReturnCaptures=*/true);
469 if (capturesNothing(CC))
471
472 // If only read provenance was captured, other threads may only read the
473 // object.
474 ModRefInfo MR =
476
477 // If Loc is a constant memory location, the synchronization operation
478 // definitely could not modify it.
479 return MR & AA->getModRefInfoMask(Loc);
480}
481
483 const MemoryLocation &Loc,
484 AAQueryInfo &AAQI) {
485 // If the load address doesn't alias the given address, it doesn't read
486 // or write the specified memory.
487 if (Loc.Ptr) {
488 AliasResult AR = alias(MemoryLocation::get(L), Loc, AAQI, L);
489 if (AR == AliasResult::NoAlias) {
490 // Synchronization effects may affect locations that do not alias.
491 if (isStrongerThanMonotonic(L->getOrdering()))
492 return getSyncEffects(this, Loc, AAQI);
494 }
495 }
496
497 // Preserve the ordering requirement.
498 if (isStrongerThanUnordered(L->getOrdering()))
499 return ModRefInfo::ModRef;
500
501 // Otherwise, a load just reads.
502 return ModRefInfo::Ref;
503}
504
506 const MemoryLocation &Loc,
507 AAQueryInfo &AAQI) {
508 if (Loc.Ptr) {
509 AliasResult AR = alias(MemoryLocation::get(S), Loc, AAQI, S);
510 // If the store address cannot alias the pointer in question, then the
511 // specified memory cannot be modified by the store.
512 if (AR == AliasResult::NoAlias) {
513 // Synchronization effects may affect locations that do not alias.
515 return getSyncEffects(this, Loc, AAQI);
517 }
518
519 // Examine the ModRef mask. If Mod isn't present, then return NoModRef.
520 // This ensures that if Loc is a constant memory location, we take into
521 // account the fact that the store definitely could not modify the memory
522 // location.
525 }
526
527 // Preserve the ordering requirement.
529 return ModRefInfo::ModRef;
530
531 // Otherwise, a store just writes.
532 return ModRefInfo::Mod;
533}
534
536 const MemoryLocation &Loc,
537 AAQueryInfo &AAQI) {
538 if (Loc.Ptr) {
540
541 for (const auto &AA : AAs) {
542 Result &= AA->getModRefInfo(F, Loc, AAQI);
543
544 if (isNoModRef(Result))
546 }
547
548 return Result & getSyncEffects(this, Loc, AAQI);
549 }
550
551 return ModRefInfo::ModRef;
552}
553
555 const MemoryLocation &Loc,
556 AAQueryInfo &AAQI) {
557 if (Loc.Ptr) {
558 AliasResult AR = alias(MemoryLocation::get(V), Loc, AAQI, V);
559 // If the va_arg address cannot alias the pointer in question, then the
560 // specified memory cannot be accessed by the va_arg.
561 if (AR == AliasResult::NoAlias)
563
564 // If the pointer is a pointer to invariant memory, then it could not have
565 // been modified by this va_arg.
566 return getModRefInfoMask(Loc, AAQI);
567 }
568
569 // Otherwise, a va_arg reads and writes.
570 return ModRefInfo::ModRef;
571}
572
574 const MemoryLocation &Loc,
575 AAQueryInfo &AAQI) {
576 if (Loc.Ptr) {
577 // If the pointer is a pointer to invariant memory,
578 // then it could not have been modified by this catchpad.
579 return getModRefInfoMask(Loc, AAQI);
580 }
581
582 // Otherwise, a catchpad reads and writes.
583 return ModRefInfo::ModRef;
584}
585
587 const MemoryLocation &Loc,
588 AAQueryInfo &AAQI) {
589 if (Loc.Ptr) {
590 // If the pointer is a pointer to invariant memory,
591 // then it could not have been modified by this catchpad.
592 return getModRefInfoMask(Loc, AAQI);
593 }
594
595 // Otherwise, a catchret reads and writes.
596 return ModRefInfo::ModRef;
597}
598
600 const MemoryLocation &Loc,
601 AAQueryInfo &AAQI) {
602 if (Loc.Ptr) {
603 AliasResult AR = alias(MemoryLocation::get(CX), Loc, AAQI, CX);
604 // If the cmpxchg address does not alias the location, it does not access
605 // it.
606 if (AR == AliasResult::NoAlias) {
607 // Synchronization effects may affect locations that do not alias.
609 return getSyncEffects(this, Loc, AAQI);
611 }
612 }
613
614 return ModRefInfo::ModRef;
615}
616
618 const MemoryLocation &Loc,
619 AAQueryInfo &AAQI) {
620 if (Loc.Ptr) {
621 AliasResult AR = alias(MemoryLocation::get(RMW), Loc, AAQI, RMW);
622 // If the atomicrmw address does not alias the location, it does not access
623 // it.
624 if (AR == AliasResult::NoAlias) {
625 // Synchronization effects may affect locations that do not alias.
627 return getSyncEffects(this, Loc, AAQI);
629 }
630 }
631
632 return ModRefInfo::ModRef;
633}
634
636 const std::optional<MemoryLocation> &OptLoc,
637 AAQueryInfo &AAQIP) {
638 if (OptLoc == std::nullopt) {
639 if (const auto *Call = dyn_cast<CallBase>(I))
640 return getMemoryEffects(Call, AAQIP).getModRef();
641 }
642
643 const MemoryLocation &Loc = OptLoc.value_or(MemoryLocation());
644
645 switch (I->getOpcode()) {
646 case Instruction::VAArg:
647 return getModRefInfo((const VAArgInst *)I, Loc, AAQIP);
648 case Instruction::Load:
649 return getModRefInfo((const LoadInst *)I, Loc, AAQIP);
650 case Instruction::Store:
651 return getModRefInfo((const StoreInst *)I, Loc, AAQIP);
652 case Instruction::Fence:
653 return getModRefInfo((const FenceInst *)I, Loc, AAQIP);
654 case Instruction::AtomicCmpXchg:
655 return getModRefInfo((const AtomicCmpXchgInst *)I, Loc, AAQIP);
656 case Instruction::AtomicRMW:
657 return getModRefInfo((const AtomicRMWInst *)I, Loc, AAQIP);
658 case Instruction::Call:
659 case Instruction::CallBr:
660 case Instruction::Invoke:
661 return getModRefInfo((const CallBase *)I, Loc, AAQIP);
662 case Instruction::CatchPad:
663 return getModRefInfo((const CatchPadInst *)I, Loc, AAQIP);
664 case Instruction::CatchRet:
665 return getModRefInfo((const CatchReturnInst *)I, Loc, AAQIP);
666 default:
667 assert(!I->mayReadOrWriteMemory() &&
668 "Unhandled memory access instruction!");
670 }
671}
672
673/// Return information about whether a particular call site modifies
674/// or reads the specified memory location \p MemLoc before instruction \p I
675/// in a BasicBlock.
676/// FIXME: this is really just shoring-up a deficiency in alias analysis.
677/// BasicAA isn't willing to spend linear time determining whether an alloca
678/// was captured before or after this particular call, while we are. However,
679/// with a smarter AA in place, this test is just wasting compile time.
681 const MemoryLocation &MemLoc,
682 DominatorTree *DT,
683 AAQueryInfo &AAQI) {
684 if (!DT)
685 return ModRefInfo::ModRef;
686
687 const Value *Object = getUnderlyingObject(MemLoc.Ptr);
688 if (!isIdentifiedFunctionLocal(Object))
689 return ModRefInfo::ModRef;
690
691 const auto *Call = dyn_cast<CallBase>(I);
692 if (!Call || Call == Object)
693 return ModRefInfo::ModRef;
694
696 Object, /* ReturnCaptures */ true, I, DT,
697 /* include Object */ true, CaptureComponents::Provenance)))
698 return ModRefInfo::ModRef;
699
700 unsigned ArgNo = 0;
702 // Set flag only if no May found and all operands processed.
703 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
704 CI != CE; ++CI, ++ArgNo) {
705 // Only look at the no-capture or byval pointer arguments. If this
706 // pointer were passed to arguments that were neither of these, then it
707 // couldn't be no-capture.
708 if (!(*CI)->getType()->isPointerTy())
709 continue;
710
711 // Make sure we still check captures(ret: address, provenance) and
712 // captures(address) arguments, as these wouldn't be treated as a capture
713 // at the call-site.
714 CaptureInfo Captures = Call->getCaptureInfo(ArgNo);
716 continue;
717
718 AliasResult AR =
721 // If this is a no-capture pointer argument, see if we can tell that it
722 // is impossible to alias the pointer we're checking. If not, we have to
723 // assume that the call could touch the pointer, even though it doesn't
724 // escape.
725 if (AR == AliasResult::NoAlias)
726 continue;
727 if (Call->doesNotAccessMemory(ArgNo))
728 continue;
729 if (Call->onlyReadsMemory(ArgNo)) {
730 R = ModRefInfo::Ref;
731 continue;
732 }
733 return ModRefInfo::ModRef;
734 }
735 return R;
736}
737
738/// canBasicBlockModify - Return true if it is possible for execution of the
739/// specified basic block to modify the location Loc.
740///
745
746/// canInstructionRangeModRef - Return true if it is possible for the
747/// execution of the specified instructions to mod\ref (according to the
748/// mode) the location Loc. The instructions to consider are all
749/// of the instructions in the range of [I1,I2] INCLUSIVE.
750/// I1 and I2 must be in the same basic block.
752 const Instruction &I2,
753 const MemoryLocation &Loc,
754 const ModRefInfo Mode) {
755 assert(I1.getParent() == I2.getParent() &&
756 "Instructions not in same basic block!");
757 BasicBlock::const_iterator I = I1.getIterator();
759 ++E; // Convert from inclusive to exclusive range.
760
761 for (; I != E; ++I) // Check every instruction in range
762 if (isModOrRefSet(getModRefInfo(&*I, Loc) & Mode))
763 return true;
764 return false;
765}
766
767// Provide a definition for the root virtual destructor.
769
770// Provide a definition for the static object used to identify passes.
771AnalysisKey AAManager::Key;
772
774
777
779
780INITIALIZE_PASS(ExternalAAWrapperPass, "external-aa", "External Alias Analysis",
781 false, true)
782
785 bool RunEarly) {
786 return new ExternalAAWrapperPass(std::move(Callback), RunEarly);
787}
788
790
792
794 "Function Alias Analysis Results", false, true)
802 "Function Alias Analysis Results", false, true)
803
804/// Run the wrapper pass to rebuild an aggregation over known AA passes.
805///
806/// This is the legacy pass manager's interface to the new-style AA results
807/// aggregation object. Because this is somewhat shoe-horned into the legacy
808/// pass manager, we hard code all the specific alias analyses available into
809/// it. While the particular set enabled is configured via commandline flags,
810/// adding a new alias analysis to LLVM will require adding support for it to
811/// this list.
813 // NB! This *must* be reset before adding new AA results to the new
814 // AAResults object because in the legacy pass manager, each instance
815 // of these will refer to the *same* immutable analyses, registering and
816 // unregistering themselves with them. We need to carefully tear down the
817 // previous object first, in this case replacing it with an empty one, before
818 // registering new results.
819 AAR.reset(
821
822 // Add any target-specific alias analyses that should be run early.
823 auto *ExtWrapperPass = getAnalysisIfAvailable<ExternalAAWrapperPass>();
824 if (ExtWrapperPass && ExtWrapperPass->RunEarly && ExtWrapperPass->CB) {
825 LLVM_DEBUG(dbgs() << "AAResults register Early ExternalAA: "
826 << ExtWrapperPass->getPassName() << "\n");
827 ExtWrapperPass->CB(*this, F, *AAR);
828 }
829
830 // BasicAA is always available for function analyses. Also, we add it first
831 // so that it can trump TBAA results when it proves MustAlias.
832 // FIXME: TBAA should have an explicit mode to support this and then we
833 // should reconsider the ordering here.
834 if (!getAnalysisOptions(F).disable_basic_aa) {
835 LLVM_DEBUG(dbgs() << "AAResults register BasicAA\n");
836 AAR->addAAResult(getAnalysis<BasicAAWrapperPass>().getResult());
837 }
838
839 // Populate the results with the currently available AAs.
840 if (auto *WrapperPass =
842 LLVM_DEBUG(dbgs() << "AAResults register ScopedNoAliasAA\n");
843 AAR->addAAResult(WrapperPass->getResult());
844 }
845 if (auto *WrapperPass = getAnalysisIfAvailable<TypeBasedAAWrapperPass>()) {
846 LLVM_DEBUG(dbgs() << "AAResults register TypeBasedAA\n");
847 AAR->addAAResult(WrapperPass->getResult());
848 }
849 if (auto *WrapperPass = getAnalysisIfAvailable<GlobalsAAWrapperPass>()) {
850 LLVM_DEBUG(dbgs() << "AAResults register GlobalsAA\n");
851 AAR->addAAResult(WrapperPass->getResult());
852 }
853 if (auto *WrapperPass = getAnalysisIfAvailable<SCEVAAWrapperPass>()) {
854 LLVM_DEBUG(dbgs() << "AAResults register SCEVAA\n");
855 AAR->addAAResult(WrapperPass->getResult());
856 }
857
858 // If available, run an external AA providing callback over the results as
859 // well.
860 if (ExtWrapperPass && !ExtWrapperPass->RunEarly && ExtWrapperPass->CB) {
861 LLVM_DEBUG(dbgs() << "AAResults register Late ExternalAA: "
862 << ExtWrapperPass->getPassName() << "\n");
863 ExtWrapperPass->CB(*this, F, *AAR);
864 }
865
866 // Analyses don't mutate the IR, so return false.
867 return false;
868}
869
871 AU.setPreservesAll();
874
875 // We also need to mark all the alias analysis passes we will potentially
876 // probe in runOnFunction as used here to ensure the legacy pass manager
877 // preserves them. This hard coding of lists of alias analyses is specific to
878 // the legacy pass manager.
884}
885
888 for (auto &Getter : ResultGetters)
889 (*Getter)(F, AM, R);
890 return R;
891}
892
894 if (const auto *Call = dyn_cast<CallBase>(V))
895 return Call->hasRetAttr(Attribute::NoAlias);
896 return false;
897}
898
899static bool isNoAliasOrByValArgument(const Value *V) {
900 if (const Argument *A = dyn_cast<Argument>(V))
901 return A->hasNoAliasAttr() || A->hasByValAttr();
902 return false;
903}
904
906 if (isa<AllocaInst>(V))
907 return true;
909 return true;
910 if (isNoAliasCall(V))
911 return true;
913 return true;
914 return false;
915}
916
920
922 // TODO: We can handle other cases here
923 // 1) For GC languages, arguments to functions are often required to be
924 // base pointers.
925 // 2) Result of allocation routines are often base pointers. Leverage TLI.
926 return (isa<AllocaInst>(V) || isa<GlobalVariable>(V));
927}
928
930 if (auto *CB = dyn_cast<CallBase>(V)) {
932 CB, /*MustPreserveOffset=*/false))
933 return false;
934
935 // The return value of a function with a captures(ret: address, provenance)
936 // attribute is not necessarily an escape source. The return value may
937 // alias with a non-escaping object.
938 return !CB->hasArgumentWithAdditionalReturnCaptureComponents();
939 }
940
941 // The load case works because isNotCapturedBefore considers all
942 // stores to be escapes (it passes true for the StoreCaptures argument
943 // to PointerMayBeCaptured).
944 if (isa<LoadInst>(V))
945 return true;
946
947 // The inttoptr case works because isNotCapturedBefore considers all
948 // means of converting or equating a pointer to an int (ptrtoint, ptr store
949 // which could be followed by an integer load, ptr<->int compare) as
950 // escaping, and objects located at well-known addresses via platform-specific
951 // means cannot be considered non-escaping local objects.
952 if (isa<IntToPtrInst>(V))
953 return true;
954
955 // Capture tracking considers insertions into aggregates and vectors as
956 // captures. As such, extractions from aggregates and vectors are escape
957 // sources.
959 return true;
960
961 // Same for inttoptr constant expressions.
962 if (auto *CE = dyn_cast<ConstantExpr>(V))
963 if (CE->getOpcode() == Instruction::IntToPtr)
964 return true;
965
966 return false;
967}
968
970 bool &RequiresNoCaptureBeforeUnwind) {
971 RequiresNoCaptureBeforeUnwind = false;
972
973 // Alloca goes out of scope on unwind.
974 if (isa<AllocaInst>(Object))
975 return true;
976
977 // Byval goes out of scope on unwind.
978 if (auto *A = dyn_cast<Argument>(Object))
979 return A->hasByValAttr() || A->hasAttribute(Attribute::DeadOnUnwind);
980
981 // A noalias return is not accessible from any other code. If the pointer
982 // does not escape prior to the unwind, then the caller cannot access the
983 // memory either.
984 if (isNoAliasCall(Object)) {
985 RequiresNoCaptureBeforeUnwind = true;
986 return true;
987 }
988
989 return false;
990}
991
992// We don't consider globals as writable: While the physical memory is writable,
993// we may not have provenance to perform the write.
994bool llvm::isWritableObject(const Value *Object,
995 bool &ExplicitlyDereferenceableOnly) {
996 ExplicitlyDereferenceableOnly = false;
997
998 // TODO: Alloca might not be writable after its lifetime ends.
999 // See https://github.com/llvm/llvm-project/issues/51838.
1000 if (isa<AllocaInst>(Object))
1001 return true;
1002
1003 if (auto *A = dyn_cast<Argument>(Object)) {
1004 // Also require noalias, otherwise writability at function entry cannot be
1005 // generalized to writability at other program points, even if the pointer
1006 // does not escape.
1007 if (A->hasAttribute(Attribute::Writable) && A->hasNoAliasAttr()) {
1008 ExplicitlyDereferenceableOnly = true;
1009 return true;
1010 }
1011
1012 return A->hasByValAttr();
1013 }
1014
1015 // TODO: Noalias shouldn't imply writability, this should check for an
1016 // allocator function instead.
1017 return isNoAliasCall(Object);
1018}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ModRefInfo getModRefInfoInaccessibleAndTargetMemLoc(const MemoryEffects CallUse, const MemoryEffects CallDef)
static bool isNoAliasOrByValArgument(const Value *V)
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This is the interface for a simple mod/ref and alias analysis over globals.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
RISCVCC::CondCode CC
This is the interface for a SCEV-based alias analysis.
This is the interface for a metadata-based scoped no-alias analysis.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
This is the interface for a metadata-based TBAA.
A manager for alias analyses.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
This class stores info we want to provide to or retain within an alias query.
unsigned Depth
Query depth used to distinguish recursive queries.
CaptureAnalysis * CA
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
bool runOnFunction(Function &F) override
Run the wrapper pass to rebuild an aggregation over known AA passes.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Instruction *CtxI)
LLVM_ABI AAResults(const TargetLibraryInfo &TLI)
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call)
Return the behavior of the given call site.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
LLVM_ABI ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the ModRef info associated with a pointer argument of a call.
LLVM_ABI ModRefInfo callCapturesBefore(const Instruction *I, const MemoryLocation &MemLoc, DominatorTree *DT, AAQueryInfo &AAQIP)
Return information about whether a particular call site modifies or reads the specified memory locati...
LLVM_ABI bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, const MemoryLocation &Loc, const ModRefInfo Mode)
Check if it is possible for the execution of the specified instructions to mod(according to the mode)...
LLVM_ABI ~AAResults()
LLVM_ABI bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc)
Check if it is possible for execution of the specified basic block to modify the location Loc.
The possible results of an alias query.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
constexpr int32_t getOffset() const
constexpr bool hasOffset() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
An instruction that atomically checks whether a specified value is in a memory location,...
AtomicOrdering getMergedOrdering() const
Returns a single ordering which is at least as strong as both the success and failure orderings for t...
an instruction that atomically reads a memory location, combines it with another value,...
AtomicOrdering getOrdering() const
Returns the ordering constraint of this rmw instruction.
Legacy wrapper pass to provide the BasicAAResult object.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Instruction & back() const
Definition BasicBlock.h:465
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
const Instruction & front() const
Definition BasicBlock.h:463
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Represents which components of the pointer may be captured in which location.
Definition ModRef.h:414
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
Definition ModRef.h:446
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
An instruction for ordering other memory operations.
FunctionPass(char &pid)
Definition Pass.h:314
Legacy wrapper pass to provide the GlobalsAAResult object.
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:283
ImmutablePass(char &pid)
Definition Pass.h:285
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
An instruction for reading from memory.
ModRefInfo getModRef(Location Loc) const
Get ModRefInfo for the given Location.
Definition ModRef.h:219
static MemoryEffectsBase unknown()
Definition ModRef.h:123
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
const Value * Ptr
The address of the start of the location.
static LLVM_ABI std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
static LLVM_ABI MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
Legacy wrapper pass to provide the SCEVAAResult object.
Legacy wrapper pass to provide the ScopedNoAliasAAResult object.
AAQueryInfo that uses SimpleCaptureAnalysis.
An instruction for storing to memory.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this store instruction.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
Legacy wrapper pass to provide the TypeBasedAAResult object.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
Abstract Attribute helper functions.
Definition Attributor.h:164
This is an optimization pass for GlobalISel generic memory operations.
bool capturesReadProvenanceOnly(CaptureComponents CC)
Definition ModRef.h:391
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isStrongerThanMonotonic(AtomicOrdering AO)
LLVM_ABI bool isBaseOfObject(const Value *V)
Return true if we know V to the base address of the corresponding memory object.
bool isStrongerThanUnordered(AtomicOrdering AO)
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI ModRefInfo getSyncEffects(AAResults *AA, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Get ModRefInfo for a synchronizing operation, such as a fence or stronger than monotonic atomic load/...
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isModOrRefSet(const ModRefInfo MRI)
Definition ModRef.h:43
LLVM_ABI bool isNotVisibleOnUnwind(const Value *Object, bool &RequiresNoCaptureBeforeUnwind)
Return true if Object memory is not visible after an unwind, in the sense that program semantics cann...
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:365
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
IRMemLocation
The locations at which a function might access memory.
Definition ModRef.h:60
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
Definition ModRef.h:64
LLVM_ABI ImmutablePass * createExternalAAWrapperPass(std::function< void(Pass &, Function &, AAResults &)> Callback, bool RunEarly=false)
A wrapper pass around a callback which can be used to populate the AAResults in the AAResultsWrapperP...
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveOffset, bool MustPreserveProvenance=false)
launder.invariant.group and similar intrinsics return a pointer that aliases their argument,...
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
LLVM_ABI bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
LLVM_ABI bool isEscapeSource(const Value *V)
Returns true if the pointer is one which would have been considered an escape by isNotCapturedBefore.
bool capturesAnything(CaptureComponents CC)
Definition ModRef.h:379
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:375
bool isNoModRef(const ModRefInfo MRI)
Definition ModRef.h:40
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
bool capturesAnyProvenance(CaptureComponents CC)
Definition ModRef.h:400
bool isRefSet(const ModRefInfo MRI)
Definition ModRef.h:52
LLVM_ABI bool isWritableObject(const Value *Object, bool &ExplicitlyDereferenceableOnly)
Return true if the Object is writable, in the sense that any location based on this pointer that can ...
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
virtual CaptureComponents getCapturesBefore(const Value *Object, const Instruction *I, bool OrAt, bool ReturnCaptures)=0
Return how Object may be captured before instruction I, considering only provenance captures.
A wrapper pass for external alias analyses.
std::function< void(Pass &, Function &, AAResults &)> CallbackT
static LLVM_ABI char ID
bool RunEarly
Flag indicating whether this external AA should run before Basic AA.