1 //==- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation --==//
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
7 //===----------------------------------------------------------------------===//
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.
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,
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
24 //===----------------------------------------------------------------------===//
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Analysis/BasicAliasAnalysis.h"
29 #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
30 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
31 #include "llvm/Analysis/CaptureTracking.h"
32 #include "llvm/Analysis/GlobalsModRef.h"
33 #include "llvm/Analysis/MemoryLocation.h"
34 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
35 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
36 #include "llvm/Analysis/ScopedNoAliasAA.h"
37 #include "llvm/Analysis/TargetLibraryInfo.h"
38 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
39 #include "llvm/Analysis/ValueTracking.h"
40 #include "llvm/IR/Argument.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/InitializePasses.h"
49 #include "llvm/Pass.h"
50 #include "llvm/Support/AtomicOrdering.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
58 #define DEBUG_TYPE "aa"
62 STATISTIC(NumNoAlias
, "Number of NoAlias results");
63 STATISTIC(NumMayAlias
, "Number of MayAlias results");
64 STATISTIC(NumMustAlias
, "Number of MustAlias results");
67 /// Allow disabling BasicAA from the AA results. This is particularly useful
68 /// when testing to isolate a single AA implementation.
69 cl::opt
<bool> DisableBasicAA("disable-basic-aa", cl::Hidden
, cl::init(false));
73 /// Print a trace of alias analysis queries and their results.
74 static cl::opt
<bool> EnableAATrace("aa-trace", cl::Hidden
, cl::init(false));
76 static const bool EnableAATrace
= false;
79 AAResults::AAResults(AAResults
&&Arg
)
80 : TLI(Arg
.TLI
), AAs(std::move(Arg
.AAs
)), AADeps(std::move(Arg
.AADeps
)) {
82 AA
->setAAResults(this);
85 AAResults::~AAResults() {
86 // FIXME; It would be nice to at least clear out the pointers back to this
87 // aggregation here, but we end up with non-nesting lifetimes in the legacy
88 // pass manager that prevent this from working. In the legacy pass manager
89 // we'll end up with dangling references here in some cases.
92 AA
->setAAResults(nullptr);
96 bool AAResults::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
97 FunctionAnalysisManager::Invalidator
&Inv
) {
98 // AAResults preserves the AAManager by default, due to the stateless nature
99 // of AliasAnalysis. There is no need to check whether it has been preserved
100 // explicitly. Check if any module dependency was invalidated and caused the
101 // AAManager to be invalidated. Invalidate ourselves in that case.
102 auto PAC
= PA
.getChecker
<AAManager
>();
103 if (!PAC
.preservedWhenStateless())
106 // Check if any of the function dependencies were invalidated, and invalidate
107 // ourselves in that case.
108 for (AnalysisKey
*ID
: AADeps
)
109 if (Inv
.invalidate(ID
, F
, PA
))
112 // Everything we depend on is still fine, so are we. Nothing to invalidate.
116 //===----------------------------------------------------------------------===//
117 // Default chaining methods
118 //===----------------------------------------------------------------------===//
120 AliasResult
AAResults::alias(const MemoryLocation
&LocA
,
121 const MemoryLocation
&LocB
) {
123 return alias(LocA
, LocB
, AAQIP
);
126 AliasResult
AAResults::alias(const MemoryLocation
&LocA
,
127 const MemoryLocation
&LocB
, AAQueryInfo
&AAQI
) {
128 AliasResult Result
= AliasResult::MayAlias
;
131 for (unsigned I
= 0; I
< AAQI
.Depth
; ++I
)
133 dbgs() << "Start " << *LocA
.Ptr
<< " @ " << LocA
.Size
<< ", "
134 << *LocB
.Ptr
<< " @ " << LocB
.Size
<< "\n";
138 for (const auto &AA
: AAs
) {
139 Result
= AA
->alias(LocA
, LocB
, AAQI
);
140 if (Result
!= AliasResult::MayAlias
)
146 for (unsigned I
= 0; I
< AAQI
.Depth
; ++I
)
148 dbgs() << "End " << *LocA
.Ptr
<< " @ " << LocA
.Size
<< ", "
149 << *LocB
.Ptr
<< " @ " << LocB
.Size
<< " = " << Result
<< "\n";
152 if (AAQI
.Depth
== 0) {
153 if (Result
== AliasResult::NoAlias
)
155 else if (Result
== AliasResult::MustAlias
)
163 bool AAResults::pointsToConstantMemory(const MemoryLocation
&Loc
,
166 return pointsToConstantMemory(Loc
, AAQIP
, OrLocal
);
169 bool AAResults::pointsToConstantMemory(const MemoryLocation
&Loc
,
170 AAQueryInfo
&AAQI
, bool OrLocal
) {
171 for (const auto &AA
: AAs
)
172 if (AA
->pointsToConstantMemory(Loc
, AAQI
, OrLocal
))
178 ModRefInfo
AAResults::getArgModRefInfo(const CallBase
*Call
, unsigned ArgIdx
) {
179 ModRefInfo Result
= ModRefInfo::ModRef
;
181 for (const auto &AA
: AAs
) {
182 Result
= intersectModRef(Result
, AA
->getArgModRefInfo(Call
, ArgIdx
));
184 // Early-exit the moment we reach the bottom of the lattice.
185 if (isNoModRef(Result
))
186 return ModRefInfo::NoModRef
;
192 ModRefInfo
AAResults::getModRefInfo(Instruction
*I
, const CallBase
*Call2
) {
194 return getModRefInfo(I
, Call2
, AAQIP
);
197 ModRefInfo
AAResults::getModRefInfo(Instruction
*I
, const CallBase
*Call2
,
199 // We may have two calls.
200 if (const auto *Call1
= dyn_cast
<CallBase
>(I
)) {
201 // Check if the two calls modify the same memory.
202 return getModRefInfo(Call1
, Call2
, AAQI
);
203 } else if (I
->isFenceLike()) {
204 // If this is a fence, just return ModRef.
205 return ModRefInfo::ModRef
;
207 // Otherwise, check if the call modifies or references the
208 // location this memory access defines. The best we can say
209 // is that if the call references what this instruction
210 // defines, it must be clobbered by this location.
211 const MemoryLocation DefLoc
= MemoryLocation::get(I
);
212 ModRefInfo MR
= getModRefInfo(Call2
, DefLoc
, AAQI
);
213 if (isModOrRefSet(MR
))
214 return setModAndRef(MR
);
216 return ModRefInfo::NoModRef
;
219 ModRefInfo
AAResults::getModRefInfo(const CallBase
*Call
,
220 const MemoryLocation
&Loc
) {
222 return getModRefInfo(Call
, Loc
, AAQIP
);
225 ModRefInfo
AAResults::getModRefInfo(const CallBase
*Call
,
226 const MemoryLocation
&Loc
,
228 ModRefInfo Result
= ModRefInfo::ModRef
;
230 for (const auto &AA
: AAs
) {
231 Result
= intersectModRef(Result
, AA
->getModRefInfo(Call
, Loc
, AAQI
));
233 // Early-exit the moment we reach the bottom of the lattice.
234 if (isNoModRef(Result
))
235 return ModRefInfo::NoModRef
;
238 // Try to refine the mod-ref info further using other API entry points to the
239 // aggregate set of AA results.
240 auto MRB
= getModRefBehavior(Call
);
241 if (onlyAccessesInaccessibleMem(MRB
))
242 return ModRefInfo::NoModRef
;
244 if (onlyReadsMemory(MRB
))
245 Result
= clearMod(Result
);
246 else if (doesNotReadMemory(MRB
))
247 Result
= clearRef(Result
);
249 if (onlyAccessesArgPointees(MRB
) || onlyAccessesInaccessibleOrArgMem(MRB
)) {
250 bool IsMustAlias
= true;
251 ModRefInfo AllArgsMask
= ModRefInfo::NoModRef
;
252 if (doesAccessArgPointees(MRB
)) {
253 for (auto AI
= Call
->arg_begin(), AE
= Call
->arg_end(); AI
!= AE
; ++AI
) {
254 const Value
*Arg
= *AI
;
255 if (!Arg
->getType()->isPointerTy())
257 unsigned ArgIdx
= std::distance(Call
->arg_begin(), AI
);
258 MemoryLocation ArgLoc
=
259 MemoryLocation::getForArgument(Call
, ArgIdx
, TLI
);
260 AliasResult ArgAlias
= alias(ArgLoc
, Loc
, AAQI
);
261 if (ArgAlias
!= AliasResult::NoAlias
) {
262 ModRefInfo ArgMask
= getArgModRefInfo(Call
, ArgIdx
);
263 AllArgsMask
= unionModRef(AllArgsMask
, ArgMask
);
265 // Conservatively clear IsMustAlias unless only MustAlias is found.
266 IsMustAlias
&= (ArgAlias
== AliasResult::MustAlias
);
269 // Return NoModRef if no alias found with any argument.
270 if (isNoModRef(AllArgsMask
))
271 return ModRefInfo::NoModRef
;
272 // Logical & between other AA analyses and argument analysis.
273 Result
= intersectModRef(Result
, AllArgsMask
);
274 // If only MustAlias found above, set Must bit.
275 Result
= IsMustAlias
? setMust(Result
) : clearMust(Result
);
278 // If Loc is a constant memory location, the call definitely could not
279 // modify the memory location.
280 if (isModSet(Result
) && pointsToConstantMemory(Loc
, AAQI
, /*OrLocal*/ false))
281 Result
= clearMod(Result
);
286 ModRefInfo
AAResults::getModRefInfo(const CallBase
*Call1
,
287 const CallBase
*Call2
) {
289 return getModRefInfo(Call1
, Call2
, AAQIP
);
292 ModRefInfo
AAResults::getModRefInfo(const CallBase
*Call1
,
293 const CallBase
*Call2
, AAQueryInfo
&AAQI
) {
294 ModRefInfo Result
= ModRefInfo::ModRef
;
296 for (const auto &AA
: AAs
) {
297 Result
= intersectModRef(Result
, AA
->getModRefInfo(Call1
, Call2
, AAQI
));
299 // Early-exit the moment we reach the bottom of the lattice.
300 if (isNoModRef(Result
))
301 return ModRefInfo::NoModRef
;
304 // Try to refine the mod-ref info further using other API entry points to the
305 // aggregate set of AA results.
307 // If Call1 or Call2 are readnone, they don't interact.
308 auto Call1B
= getModRefBehavior(Call1
);
309 if (Call1B
== FMRB_DoesNotAccessMemory
)
310 return ModRefInfo::NoModRef
;
312 auto Call2B
= getModRefBehavior(Call2
);
313 if (Call2B
== FMRB_DoesNotAccessMemory
)
314 return ModRefInfo::NoModRef
;
316 // If they both only read from memory, there is no dependence.
317 if (onlyReadsMemory(Call1B
) && onlyReadsMemory(Call2B
))
318 return ModRefInfo::NoModRef
;
320 // If Call1 only reads memory, the only dependence on Call2 can be
321 // from Call1 reading memory written by Call2.
322 if (onlyReadsMemory(Call1B
))
323 Result
= clearMod(Result
);
324 else if (doesNotReadMemory(Call1B
))
325 Result
= clearRef(Result
);
327 // If Call2 only access memory through arguments, accumulate the mod/ref
328 // information from Call1's references to the memory referenced by
329 // Call2's arguments.
330 if (onlyAccessesArgPointees(Call2B
)) {
331 if (!doesAccessArgPointees(Call2B
))
332 return ModRefInfo::NoModRef
;
333 ModRefInfo R
= ModRefInfo::NoModRef
;
334 bool IsMustAlias
= true;
335 for (auto I
= Call2
->arg_begin(), E
= Call2
->arg_end(); I
!= E
; ++I
) {
336 const Value
*Arg
= *I
;
337 if (!Arg
->getType()->isPointerTy())
339 unsigned Call2ArgIdx
= std::distance(Call2
->arg_begin(), I
);
341 MemoryLocation::getForArgument(Call2
, Call2ArgIdx
, TLI
);
343 // ArgModRefC2 indicates what Call2 might do to Call2ArgLoc, and the
344 // dependence of Call1 on that location is the inverse:
345 // - If Call2 modifies location, dependence exists if Call1 reads or
347 // - If Call2 only reads location, dependence exists if Call1 writes.
348 ModRefInfo ArgModRefC2
= getArgModRefInfo(Call2
, Call2ArgIdx
);
349 ModRefInfo ArgMask
= ModRefInfo::NoModRef
;
350 if (isModSet(ArgModRefC2
))
351 ArgMask
= ModRefInfo::ModRef
;
352 else if (isRefSet(ArgModRefC2
))
353 ArgMask
= ModRefInfo::Mod
;
355 // ModRefC1 indicates what Call1 might do to Call2ArgLoc, and we use
356 // above ArgMask to update dependence info.
357 ModRefInfo ModRefC1
= getModRefInfo(Call1
, Call2ArgLoc
, AAQI
);
358 ArgMask
= intersectModRef(ArgMask
, ModRefC1
);
360 // Conservatively clear IsMustAlias unless only MustAlias is found.
361 IsMustAlias
&= isMustSet(ModRefC1
);
363 R
= intersectModRef(unionModRef(R
, ArgMask
), Result
);
365 // On early exit, not all args were checked, cannot set Must.
373 return ModRefInfo::NoModRef
;
375 // If MustAlias found above, set Must bit.
376 return IsMustAlias
? setMust(R
) : clearMust(R
);
379 // If Call1 only accesses memory through arguments, check if Call2 references
380 // any of the memory referenced by Call1's arguments. If not, return NoModRef.
381 if (onlyAccessesArgPointees(Call1B
)) {
382 if (!doesAccessArgPointees(Call1B
))
383 return ModRefInfo::NoModRef
;
384 ModRefInfo R
= ModRefInfo::NoModRef
;
385 bool IsMustAlias
= true;
386 for (auto I
= Call1
->arg_begin(), E
= Call1
->arg_end(); I
!= E
; ++I
) {
387 const Value
*Arg
= *I
;
388 if (!Arg
->getType()->isPointerTy())
390 unsigned Call1ArgIdx
= std::distance(Call1
->arg_begin(), I
);
392 MemoryLocation::getForArgument(Call1
, Call1ArgIdx
, TLI
);
394 // ArgModRefC1 indicates what Call1 might do to Call1ArgLoc; if Call1
395 // might Mod Call1ArgLoc, then we care about either a Mod or a Ref by
396 // Call2. If Call1 might Ref, then we care only about a Mod by Call2.
397 ModRefInfo ArgModRefC1
= getArgModRefInfo(Call1
, Call1ArgIdx
);
398 ModRefInfo ModRefC2
= getModRefInfo(Call2
, Call1ArgLoc
, AAQI
);
399 if ((isModSet(ArgModRefC1
) && isModOrRefSet(ModRefC2
)) ||
400 (isRefSet(ArgModRefC1
) && isModSet(ModRefC2
)))
401 R
= intersectModRef(unionModRef(R
, ArgModRefC1
), Result
);
403 // Conservatively clear IsMustAlias unless only MustAlias is found.
404 IsMustAlias
&= isMustSet(ModRefC2
);
407 // On early exit, not all args were checked, cannot set Must.
415 return ModRefInfo::NoModRef
;
417 // If MustAlias found above, set Must bit.
418 return IsMustAlias
? setMust(R
) : clearMust(R
);
424 FunctionModRefBehavior
AAResults::getModRefBehavior(const CallBase
*Call
) {
425 FunctionModRefBehavior Result
= FMRB_UnknownModRefBehavior
;
427 for (const auto &AA
: AAs
) {
428 Result
= FunctionModRefBehavior(Result
& AA
->getModRefBehavior(Call
));
430 // Early-exit the moment we reach the bottom of the lattice.
431 if (Result
== FMRB_DoesNotAccessMemory
)
438 FunctionModRefBehavior
AAResults::getModRefBehavior(const Function
*F
) {
439 FunctionModRefBehavior Result
= FMRB_UnknownModRefBehavior
;
441 for (const auto &AA
: AAs
) {
442 Result
= FunctionModRefBehavior(Result
& AA
->getModRefBehavior(F
));
444 // Early-exit the moment we reach the bottom of the lattice.
445 if (Result
== FMRB_DoesNotAccessMemory
)
452 raw_ostream
&llvm::operator<<(raw_ostream
&OS
, AliasResult AR
) {
454 case AliasResult::NoAlias
:
457 case AliasResult::MustAlias
:
460 case AliasResult::MayAlias
:
463 case AliasResult::PartialAlias
:
464 OS
<< "PartialAlias";
466 OS
<< " (off " << AR
.getOffset() << ")";
472 //===----------------------------------------------------------------------===//
473 // Helper method implementation
474 //===----------------------------------------------------------------------===//
476 ModRefInfo
AAResults::getModRefInfo(const LoadInst
*L
,
477 const MemoryLocation
&Loc
) {
479 return getModRefInfo(L
, Loc
, AAQIP
);
481 ModRefInfo
AAResults::getModRefInfo(const LoadInst
*L
,
482 const MemoryLocation
&Loc
,
484 // Be conservative in the face of atomic.
485 if (isStrongerThan(L
->getOrdering(), AtomicOrdering::Unordered
))
486 return ModRefInfo::ModRef
;
488 // If the load address doesn't alias the given address, it doesn't read
489 // or write the specified memory.
491 AliasResult AR
= alias(MemoryLocation::get(L
), Loc
, AAQI
);
492 if (AR
== AliasResult::NoAlias
)
493 return ModRefInfo::NoModRef
;
494 if (AR
== AliasResult::MustAlias
)
495 return ModRefInfo::MustRef
;
497 // Otherwise, a load just reads.
498 return ModRefInfo::Ref
;
501 ModRefInfo
AAResults::getModRefInfo(const StoreInst
*S
,
502 const MemoryLocation
&Loc
) {
504 return getModRefInfo(S
, Loc
, AAQIP
);
506 ModRefInfo
AAResults::getModRefInfo(const StoreInst
*S
,
507 const MemoryLocation
&Loc
,
509 // Be conservative in the face of atomic.
510 if (isStrongerThan(S
->getOrdering(), AtomicOrdering::Unordered
))
511 return ModRefInfo::ModRef
;
514 AliasResult AR
= alias(MemoryLocation::get(S
), Loc
, AAQI
);
515 // If the store address cannot alias the pointer in question, then the
516 // specified memory cannot be modified by the store.
517 if (AR
== AliasResult::NoAlias
)
518 return ModRefInfo::NoModRef
;
520 // If the pointer is a pointer to constant memory, then it could not have
521 // been modified by this store.
522 if (pointsToConstantMemory(Loc
, AAQI
))
523 return ModRefInfo::NoModRef
;
525 // If the store address aliases the pointer as must alias, set Must.
526 if (AR
== AliasResult::MustAlias
)
527 return ModRefInfo::MustMod
;
530 // Otherwise, a store just writes.
531 return ModRefInfo::Mod
;
534 ModRefInfo
AAResults::getModRefInfo(const FenceInst
*S
, const MemoryLocation
&Loc
) {
536 return getModRefInfo(S
, Loc
, AAQIP
);
539 ModRefInfo
AAResults::getModRefInfo(const FenceInst
*S
,
540 const MemoryLocation
&Loc
,
542 // If we know that the location is a constant memory location, the fence
543 // cannot modify this location.
544 if (Loc
.Ptr
&& pointsToConstantMemory(Loc
, AAQI
))
545 return ModRefInfo::Ref
;
546 return ModRefInfo::ModRef
;
549 ModRefInfo
AAResults::getModRefInfo(const VAArgInst
*V
,
550 const MemoryLocation
&Loc
) {
552 return getModRefInfo(V
, Loc
, AAQIP
);
555 ModRefInfo
AAResults::getModRefInfo(const VAArgInst
*V
,
556 const MemoryLocation
&Loc
,
559 AliasResult AR
= alias(MemoryLocation::get(V
), Loc
, AAQI
);
560 // If the va_arg address cannot alias the pointer in question, then the
561 // specified memory cannot be accessed by the va_arg.
562 if (AR
== AliasResult::NoAlias
)
563 return ModRefInfo::NoModRef
;
565 // If the pointer is a pointer to constant memory, then it could not have
566 // been modified by this va_arg.
567 if (pointsToConstantMemory(Loc
, AAQI
))
568 return ModRefInfo::NoModRef
;
570 // If the va_arg aliases the pointer as must alias, set Must.
571 if (AR
== AliasResult::MustAlias
)
572 return ModRefInfo::MustModRef
;
575 // Otherwise, a va_arg reads and writes.
576 return ModRefInfo::ModRef
;
579 ModRefInfo
AAResults::getModRefInfo(const CatchPadInst
*CatchPad
,
580 const MemoryLocation
&Loc
) {
582 return getModRefInfo(CatchPad
, Loc
, AAQIP
);
585 ModRefInfo
AAResults::getModRefInfo(const CatchPadInst
*CatchPad
,
586 const MemoryLocation
&Loc
,
589 // If the pointer is a pointer to constant memory,
590 // then it could not have been modified by this catchpad.
591 if (pointsToConstantMemory(Loc
, AAQI
))
592 return ModRefInfo::NoModRef
;
595 // Otherwise, a catchpad reads and writes.
596 return ModRefInfo::ModRef
;
599 ModRefInfo
AAResults::getModRefInfo(const CatchReturnInst
*CatchRet
,
600 const MemoryLocation
&Loc
) {
602 return getModRefInfo(CatchRet
, Loc
, AAQIP
);
605 ModRefInfo
AAResults::getModRefInfo(const CatchReturnInst
*CatchRet
,
606 const MemoryLocation
&Loc
,
609 // If the pointer is a pointer to constant memory,
610 // then it could not have been modified by this catchpad.
611 if (pointsToConstantMemory(Loc
, AAQI
))
612 return ModRefInfo::NoModRef
;
615 // Otherwise, a catchret reads and writes.
616 return ModRefInfo::ModRef
;
619 ModRefInfo
AAResults::getModRefInfo(const AtomicCmpXchgInst
*CX
,
620 const MemoryLocation
&Loc
) {
622 return getModRefInfo(CX
, Loc
, AAQIP
);
625 ModRefInfo
AAResults::getModRefInfo(const AtomicCmpXchgInst
*CX
,
626 const MemoryLocation
&Loc
,
628 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
629 if (isStrongerThanMonotonic(CX
->getSuccessOrdering()))
630 return ModRefInfo::ModRef
;
633 AliasResult AR
= alias(MemoryLocation::get(CX
), Loc
, AAQI
);
634 // If the cmpxchg address does not alias the location, it does not access
636 if (AR
== AliasResult::NoAlias
)
637 return ModRefInfo::NoModRef
;
639 // If the cmpxchg address aliases the pointer as must alias, set Must.
640 if (AR
== AliasResult::MustAlias
)
641 return ModRefInfo::MustModRef
;
644 return ModRefInfo::ModRef
;
647 ModRefInfo
AAResults::getModRefInfo(const AtomicRMWInst
*RMW
,
648 const MemoryLocation
&Loc
) {
650 return getModRefInfo(RMW
, Loc
, AAQIP
);
653 ModRefInfo
AAResults::getModRefInfo(const AtomicRMWInst
*RMW
,
654 const MemoryLocation
&Loc
,
656 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
657 if (isStrongerThanMonotonic(RMW
->getOrdering()))
658 return ModRefInfo::ModRef
;
661 AliasResult AR
= alias(MemoryLocation::get(RMW
), Loc
, AAQI
);
662 // If the atomicrmw address does not alias the location, it does not access
664 if (AR
== AliasResult::NoAlias
)
665 return ModRefInfo::NoModRef
;
667 // If the atomicrmw address aliases the pointer as must alias, set Must.
668 if (AR
== AliasResult::MustAlias
)
669 return ModRefInfo::MustModRef
;
672 return ModRefInfo::ModRef
;
675 ModRefInfo
AAResults::getModRefInfo(const Instruction
*I
,
676 const Optional
<MemoryLocation
> &OptLoc
,
677 AAQueryInfo
&AAQIP
) {
678 if (OptLoc
== None
) {
679 if (const auto *Call
= dyn_cast
<CallBase
>(I
)) {
680 return createModRefInfo(getModRefBehavior(Call
));
684 const MemoryLocation
&Loc
= OptLoc
.getValueOr(MemoryLocation());
686 switch (I
->getOpcode()) {
687 case Instruction::VAArg
:
688 return getModRefInfo((const VAArgInst
*)I
, Loc
, AAQIP
);
689 case Instruction::Load
:
690 return getModRefInfo((const LoadInst
*)I
, Loc
, AAQIP
);
691 case Instruction::Store
:
692 return getModRefInfo((const StoreInst
*)I
, Loc
, AAQIP
);
693 case Instruction::Fence
:
694 return getModRefInfo((const FenceInst
*)I
, Loc
, AAQIP
);
695 case Instruction::AtomicCmpXchg
:
696 return getModRefInfo((const AtomicCmpXchgInst
*)I
, Loc
, AAQIP
);
697 case Instruction::AtomicRMW
:
698 return getModRefInfo((const AtomicRMWInst
*)I
, Loc
, AAQIP
);
699 case Instruction::Call
:
700 return getModRefInfo((const CallInst
*)I
, Loc
, AAQIP
);
701 case Instruction::Invoke
:
702 return getModRefInfo((const InvokeInst
*)I
, Loc
, AAQIP
);
703 case Instruction::CatchPad
:
704 return getModRefInfo((const CatchPadInst
*)I
, Loc
, AAQIP
);
705 case Instruction::CatchRet
:
706 return getModRefInfo((const CatchReturnInst
*)I
, Loc
, AAQIP
);
708 return ModRefInfo::NoModRef
;
712 /// Return information about whether a particular call site modifies
713 /// or reads the specified memory location \p MemLoc before instruction \p I
715 /// FIXME: this is really just shoring-up a deficiency in alias analysis.
716 /// BasicAA isn't willing to spend linear time determining whether an alloca
717 /// was captured before or after this particular call, while we are. However,
718 /// with a smarter AA in place, this test is just wasting compile time.
719 ModRefInfo
AAResults::callCapturesBefore(const Instruction
*I
,
720 const MemoryLocation
&MemLoc
,
724 return ModRefInfo::ModRef
;
726 const Value
*Object
= getUnderlyingObject(MemLoc
.Ptr
);
727 if (!isIdentifiedFunctionLocal(Object
))
728 return ModRefInfo::ModRef
;
730 const auto *Call
= dyn_cast
<CallBase
>(I
);
731 if (!Call
|| Call
== Object
)
732 return ModRefInfo::ModRef
;
734 if (PointerMayBeCapturedBefore(Object
, /* ReturnCaptures */ true,
735 /* StoreCaptures */ true, I
, DT
,
736 /* include Object */ true))
737 return ModRefInfo::ModRef
;
740 ModRefInfo R
= ModRefInfo::NoModRef
;
741 bool IsMustAlias
= true;
742 // Set flag only if no May found and all operands processed.
743 for (auto CI
= Call
->data_operands_begin(), CE
= Call
->data_operands_end();
744 CI
!= CE
; ++CI
, ++ArgNo
) {
745 // Only look at the no-capture or byval pointer arguments. If this
746 // pointer were passed to arguments that were neither of these, then it
747 // couldn't be no-capture.
748 if (!(*CI
)->getType()->isPointerTy() ||
749 (!Call
->doesNotCapture(ArgNo
) && ArgNo
< Call
->getNumArgOperands() &&
750 !Call
->isByValArgument(ArgNo
)))
753 AliasResult AR
= alias(
754 MemoryLocation::getBeforeOrAfter(*CI
),
755 MemoryLocation::getBeforeOrAfter(Object
), AAQI
);
756 // If this is a no-capture pointer argument, see if we can tell that it
757 // is impossible to alias the pointer we're checking. If not, we have to
758 // assume that the call could touch the pointer, even though it doesn't
760 if (AR
!= AliasResult::MustAlias
)
762 if (AR
== AliasResult::NoAlias
)
764 if (Call
->doesNotAccessMemory(ArgNo
))
766 if (Call
->onlyReadsMemory(ArgNo
)) {
770 // Not returning MustModRef since we have not seen all the arguments.
771 return ModRefInfo::ModRef
;
773 return IsMustAlias
? setMust(R
) : clearMust(R
);
776 /// canBasicBlockModify - Return true if it is possible for execution of the
777 /// specified basic block to modify the location Loc.
779 bool AAResults::canBasicBlockModify(const BasicBlock
&BB
,
780 const MemoryLocation
&Loc
) {
781 return canInstructionRangeModRef(BB
.front(), BB
.back(), Loc
, ModRefInfo::Mod
);
784 /// canInstructionRangeModRef - Return true if it is possible for the
785 /// execution of the specified instructions to mod\ref (according to the
786 /// mode) the location Loc. The instructions to consider are all
787 /// of the instructions in the range of [I1,I2] INCLUSIVE.
788 /// I1 and I2 must be in the same basic block.
789 bool AAResults::canInstructionRangeModRef(const Instruction
&I1
,
790 const Instruction
&I2
,
791 const MemoryLocation
&Loc
,
792 const ModRefInfo Mode
) {
793 assert(I1
.getParent() == I2
.getParent() &&
794 "Instructions not in same basic block!");
795 BasicBlock::const_iterator I
= I1
.getIterator();
796 BasicBlock::const_iterator E
= I2
.getIterator();
797 ++E
; // Convert from inclusive to exclusive range.
799 for (; I
!= E
; ++I
) // Check every instruction in range
800 if (isModOrRefSet(intersectModRef(getModRefInfo(&*I
, Loc
), Mode
)))
805 // Provide a definition for the root virtual destructor.
806 AAResults::Concept::~Concept() = default;
808 // Provide a definition for the static object used to identify passes.
809 AnalysisKey
AAManager::Key
;
814 } // end anonymous namespace
816 ExternalAAWrapperPass::ExternalAAWrapperPass() : ImmutablePass(ID
) {
817 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
820 ExternalAAWrapperPass::ExternalAAWrapperPass(CallbackT CB
)
821 : ImmutablePass(ID
), CB(std::move(CB
)) {
822 initializeExternalAAWrapperPassPass(*PassRegistry::getPassRegistry());
825 char ExternalAAWrapperPass::ID
= 0;
827 INITIALIZE_PASS(ExternalAAWrapperPass
, "external-aa", "External Alias Analysis",
831 llvm::createExternalAAWrapperPass(ExternalAAWrapperPass::CallbackT Callback
) {
832 return new ExternalAAWrapperPass(std::move(Callback
));
835 AAResultsWrapperPass::AAResultsWrapperPass() : FunctionPass(ID
) {
836 initializeAAResultsWrapperPassPass(*PassRegistry::getPassRegistry());
839 char AAResultsWrapperPass::ID
= 0;
841 INITIALIZE_PASS_BEGIN(AAResultsWrapperPass
, "aa",
842 "Function Alias Analysis Results", false, true)
843 INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass
)
844 INITIALIZE_PASS_DEPENDENCY(CFLAndersAAWrapperPass
)
845 INITIALIZE_PASS_DEPENDENCY(CFLSteensAAWrapperPass
)
846 INITIALIZE_PASS_DEPENDENCY(ExternalAAWrapperPass
)
847 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass
)
848 INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass
)
849 INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass
)
850 INITIALIZE_PASS_DEPENDENCY(ScopedNoAliasAAWrapperPass
)
851 INITIALIZE_PASS_DEPENDENCY(TypeBasedAAWrapperPass
)
852 INITIALIZE_PASS_END(AAResultsWrapperPass
, "aa",
853 "Function Alias Analysis Results", false, true)
855 FunctionPass
*llvm::createAAResultsWrapperPass() {
856 return new AAResultsWrapperPass();
859 /// Run the wrapper pass to rebuild an aggregation over known AA passes.
861 /// This is the legacy pass manager's interface to the new-style AA results
862 /// aggregation object. Because this is somewhat shoe-horned into the legacy
863 /// pass manager, we hard code all the specific alias analyses available into
864 /// it. While the particular set enabled is configured via commandline flags,
865 /// adding a new alias analysis to LLVM will require adding support for it to
867 bool AAResultsWrapperPass::runOnFunction(Function
&F
) {
868 // NB! This *must* be reset before adding new AA results to the new
869 // AAResults object because in the legacy pass manager, each instance
870 // of these will refer to the *same* immutable analyses, registering and
871 // unregistering themselves with them. We need to carefully tear down the
872 // previous object first, in this case replacing it with an empty one, before
873 // registering new results.
875 new AAResults(getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI(F
)));
877 // BasicAA is always available for function analyses. Also, we add it first
878 // so that it can trump TBAA results when it proves MustAlias.
879 // FIXME: TBAA should have an explicit mode to support this and then we
880 // should reconsider the ordering here.
882 AAR
->addAAResult(getAnalysis
<BasicAAWrapperPass
>().getResult());
884 // Populate the results with the currently available AAs.
885 if (auto *WrapperPass
= getAnalysisIfAvailable
<ScopedNoAliasAAWrapperPass
>())
886 AAR
->addAAResult(WrapperPass
->getResult());
887 if (auto *WrapperPass
= getAnalysisIfAvailable
<TypeBasedAAWrapperPass
>())
888 AAR
->addAAResult(WrapperPass
->getResult());
889 if (auto *WrapperPass
=
890 getAnalysisIfAvailable
<objcarc::ObjCARCAAWrapperPass
>())
891 AAR
->addAAResult(WrapperPass
->getResult());
892 if (auto *WrapperPass
= getAnalysisIfAvailable
<GlobalsAAWrapperPass
>())
893 AAR
->addAAResult(WrapperPass
->getResult());
894 if (auto *WrapperPass
= getAnalysisIfAvailable
<SCEVAAWrapperPass
>())
895 AAR
->addAAResult(WrapperPass
->getResult());
896 if (auto *WrapperPass
= getAnalysisIfAvailable
<CFLAndersAAWrapperPass
>())
897 AAR
->addAAResult(WrapperPass
->getResult());
898 if (auto *WrapperPass
= getAnalysisIfAvailable
<CFLSteensAAWrapperPass
>())
899 AAR
->addAAResult(WrapperPass
->getResult());
901 // If available, run an external AA providing callback over the results as
903 if (auto *WrapperPass
= getAnalysisIfAvailable
<ExternalAAWrapperPass
>())
905 WrapperPass
->CB(*this, F
, *AAR
);
907 // Analyses don't mutate the IR, so return false.
911 void AAResultsWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
912 AU
.setPreservesAll();
913 AU
.addRequiredTransitive
<BasicAAWrapperPass
>();
914 AU
.addRequiredTransitive
<TargetLibraryInfoWrapperPass
>();
916 // We also need to mark all the alias analysis passes we will potentially
917 // probe in runOnFunction as used here to ensure the legacy pass manager
918 // preserves them. This hard coding of lists of alias analyses is specific to
919 // the legacy pass manager.
920 AU
.addUsedIfAvailable
<ScopedNoAliasAAWrapperPass
>();
921 AU
.addUsedIfAvailable
<TypeBasedAAWrapperPass
>();
922 AU
.addUsedIfAvailable
<objcarc::ObjCARCAAWrapperPass
>();
923 AU
.addUsedIfAvailable
<GlobalsAAWrapperPass
>();
924 AU
.addUsedIfAvailable
<SCEVAAWrapperPass
>();
925 AU
.addUsedIfAvailable
<CFLAndersAAWrapperPass
>();
926 AU
.addUsedIfAvailable
<CFLSteensAAWrapperPass
>();
927 AU
.addUsedIfAvailable
<ExternalAAWrapperPass
>();
930 AAManager::Result
AAManager::run(Function
&F
, FunctionAnalysisManager
&AM
) {
931 Result
R(AM
.getResult
<TargetLibraryAnalysis
>(F
));
932 for (auto &Getter
: ResultGetters
)
937 AAResults
llvm::createLegacyPMAAResults(Pass
&P
, Function
&F
,
938 BasicAAResult
&BAR
) {
939 AAResults
AAR(P
.getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI(F
));
941 // Add in our explicitly constructed BasicAA results.
943 AAR
.addAAResult(BAR
);
945 // Populate the results with the other currently available AAs.
946 if (auto *WrapperPass
=
947 P
.getAnalysisIfAvailable
<ScopedNoAliasAAWrapperPass
>())
948 AAR
.addAAResult(WrapperPass
->getResult());
949 if (auto *WrapperPass
= P
.getAnalysisIfAvailable
<TypeBasedAAWrapperPass
>())
950 AAR
.addAAResult(WrapperPass
->getResult());
951 if (auto *WrapperPass
=
952 P
.getAnalysisIfAvailable
<objcarc::ObjCARCAAWrapperPass
>())
953 AAR
.addAAResult(WrapperPass
->getResult());
954 if (auto *WrapperPass
= P
.getAnalysisIfAvailable
<GlobalsAAWrapperPass
>())
955 AAR
.addAAResult(WrapperPass
->getResult());
956 if (auto *WrapperPass
= P
.getAnalysisIfAvailable
<CFLAndersAAWrapperPass
>())
957 AAR
.addAAResult(WrapperPass
->getResult());
958 if (auto *WrapperPass
= P
.getAnalysisIfAvailable
<CFLSteensAAWrapperPass
>())
959 AAR
.addAAResult(WrapperPass
->getResult());
960 if (auto *WrapperPass
= P
.getAnalysisIfAvailable
<ExternalAAWrapperPass
>())
962 WrapperPass
->CB(P
, F
, AAR
);
967 bool llvm::isNoAliasCall(const Value
*V
) {
968 if (const auto *Call
= dyn_cast
<CallBase
>(V
))
969 return Call
->hasRetAttr(Attribute::NoAlias
);
973 static bool isNoAliasOrByValArgument(const Value
*V
) {
974 if (const Argument
*A
= dyn_cast
<Argument
>(V
))
975 return A
->hasNoAliasAttr() || A
->hasByValAttr();
979 bool llvm::isIdentifiedObject(const Value
*V
) {
980 if (isa
<AllocaInst
>(V
))
982 if (isa
<GlobalValue
>(V
) && !isa
<GlobalAlias
>(V
))
984 if (isNoAliasCall(V
))
986 if (isNoAliasOrByValArgument(V
))
991 bool llvm::isIdentifiedFunctionLocal(const Value
*V
) {
992 return isa
<AllocaInst
>(V
) || isNoAliasCall(V
) || isNoAliasOrByValArgument(V
);
995 void llvm::getAAResultsAnalysisUsage(AnalysisUsage
&AU
) {
996 // This function needs to be in sync with llvm::createLegacyPMAAResults -- if
997 // more alias analyses are added to llvm::createLegacyPMAAResults, they need
998 // to be added here also.
999 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
1000 AU
.addUsedIfAvailable
<ScopedNoAliasAAWrapperPass
>();
1001 AU
.addUsedIfAvailable
<TypeBasedAAWrapperPass
>();
1002 AU
.addUsedIfAvailable
<objcarc::ObjCARCAAWrapperPass
>();
1003 AU
.addUsedIfAvailable
<GlobalsAAWrapperPass
>();
1004 AU
.addUsedIfAvailable
<CFLAndersAAWrapperPass
>();
1005 AU
.addUsedIfAvailable
<CFLSteensAAWrapperPass
>();
1006 AU
.addUsedIfAvailable
<ExternalAAWrapperPass
>();