1//===- AliasSetTracker.cpp - Alias Sets Tracker 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 AliasSetTracker and AliasSet classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/AliasSetTracker.h"
14#include "llvm/ADT/SetVector.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/Analysis/AliasAnalysis.h"
17#include "llvm/Analysis/GuardUtils.h"
18#include "llvm/Analysis/MemoryLocation.h"
19#include "llvm/Config/llvm-config.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/InstIterator.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/IntrinsicInst.h"
24#include "llvm/IR/PassManager.h"
25#include "llvm/IR/PatternMatch.h"
26#include "llvm/IR/Value.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/AtomicOrdering.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
37static cl::opt<unsigned> SaturationThreshold(
38 "alias-set-saturation-threshold", cl::Hidden, cl::init(Val: 250),
39 cl::desc("The maximum total number of memory locations alias "
40 "sets may contain before degradation"));
41
42/// mergeSetIn - Merge the specified alias set into this alias set.
43void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST,
44 BatchAAResults &BatchAA) {
45 assert(!AS.Forward && "Alias set is already forwarding!");
46 assert(!Forward && "This set is a forwarding set!!");
47
48 // Update the alias and access types of this set...
49 Access |= AS.Access;
50 Alias |= AS.Alias;
51
52 if (Alias == SetMustAlias) {
53 // Check that these two merged sets really are must aliases. If we cannot
54 // find a must-alias pair between them, this set becomes a may alias.
55 if (!any_of(Range&: MemoryLocs, P: [&](const MemoryLocation &MemLoc) {
56 return any_of(Range&: AS.MemoryLocs, P: [&](const MemoryLocation &ASMemLoc) {
57 return BatchAA.isMustAlias(LocA: MemLoc, LocB: ASMemLoc);
58 });
59 }))
60 Alias = SetMayAlias;
61 }
62
63 // Merge the list of constituent memory locations...
64 if (MemoryLocs.empty()) {
65 std::swap(LHS&: MemoryLocs, RHS&: AS.MemoryLocs);
66 } else {
67 append_range(C&: MemoryLocs, R&: AS.MemoryLocs);
68 AS.MemoryLocs.clear();
69 }
70
71 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
72 if (UnknownInsts.empty()) { // Merge call sites...
73 if (ASHadUnknownInsts) {
74 std::swap(x&: UnknownInsts, y&: AS.UnknownInsts);
75 addRef();
76 }
77 } else if (ASHadUnknownInsts) {
78 llvm::append_range(C&: UnknownInsts, R&: AS.UnknownInsts);
79 AS.UnknownInsts.clear();
80 }
81
82 AS.Forward = this; // Forward across AS now...
83 addRef(); // AS is now pointing to us...
84
85 if (ASHadUnknownInsts)
86 AS.dropRef(AST);
87}
88
89void AliasSetTracker::removeAliasSet(AliasSet *AS) {
90 if (AliasSet *Fwd = AS->Forward) {
91 Fwd->dropRef(AST&: *this);
92 AS->Forward = nullptr;
93 } else // Update TotalAliasSetSize only if not forwarding.
94 TotalAliasSetSize -= AS->size();
95
96 AliasSets.erase(IT: AS);
97 // If we've removed the saturated alias set, set saturated marker back to
98 // nullptr and ensure this tracker is empty.
99 if (AS == AliasAnyAS) {
100 AliasAnyAS = nullptr;
101 assert(AliasSets.empty() && "Tracker not empty");
102 }
103}
104
105void AliasSet::removeFromTracker(AliasSetTracker &AST) {
106 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
107 AST.removeAliasSet(AS: this);
108}
109
110void AliasSet::addMemoryLocation(AliasSetTracker &AST,
111 const MemoryLocation &MemLoc,
112 bool KnownMustAlias) {
113 if (isMustAlias() && !KnownMustAlias) {
114 // If we cannot find a must-alias with any of the existing MemoryLocs, we
115 // must downgrade to may-alias.
116 if (!any_of(Range&: MemoryLocs, P: [&](const MemoryLocation &ASMemLoc) {
117 return AST.getAliasAnalysis().isMustAlias(LocA: MemLoc, LocB: ASMemLoc);
118 }))
119 Alias = SetMayAlias;
120 }
121
122 // Add it to the end of the list...
123 MemoryLocs.push_back(Elt: MemLoc);
124
125 AST.TotalAliasSetSize++;
126}
127
128void AliasSet::addUnknownInst(Instruction *I, BatchAAResults &AA) {
129 if (UnknownInsts.empty())
130 addRef();
131 UnknownInsts.emplace_back(args&: I);
132
133 // Guards are marked as modifying memory for control flow modelling purposes,
134 // but don't actually modify any specific memory location.
135 using namespace PatternMatch;
136 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(U: I) &&
137 !(I->use_empty() && match(V: I, P: m_Intrinsic<Intrinsic::invariant_start>()));
138 if (!MayWriteMemory) {
139 Alias = SetMayAlias;
140 Access |= ModRefInfo::Ref;
141 return;
142 }
143
144 // FIXME: This should use mod/ref information to make this not suck so bad
145 Alias = SetMayAlias;
146 Access = ModRefInfo::ModRef;
147}
148
149/// aliasesMemoryLocation - If the specified memory location "may" (or must)
150/// alias one of the members in the set return the appropriate AliasResult.
151/// Otherwise return NoAlias.
152///
153AliasResult AliasSet::aliasesMemoryLocation(const MemoryLocation &MemLoc,
154 BatchAAResults &AA) const {
155 if (AliasAny)
156 return AliasResult::MayAlias;
157
158 // Check all of the memory locations in the set...
159 for (const auto &ASMemLoc : MemoryLocs) {
160 AliasResult AR = AA.alias(LocA: MemLoc, LocB: ASMemLoc);
161 if (AR != AliasResult::NoAlias)
162 return AR;
163 }
164
165 // Check the unknown instructions...
166 for (Instruction *Inst : UnknownInsts)
167 if (isModOrRefSet(MRI: AA.getModRefInfo(I: Inst, OptLoc: MemLoc)))
168 return AliasResult::MayAlias;
169
170 return AliasResult::NoAlias;
171}
172
173ModRefInfo AliasSet::aliasesUnknownInst(const Instruction *Inst,
174 BatchAAResults &AA) const {
175
176 if (AliasAny)
177 return ModRefInfo::ModRef;
178
179 if (!Inst->mayReadOrWriteMemory())
180 return ModRefInfo::NoModRef;
181
182 for (Instruction *UnknownInst : UnknownInsts) {
183 const auto *C1 = dyn_cast<CallBase>(Val: UnknownInst);
184 const auto *C2 = dyn_cast<CallBase>(Val: Inst);
185 if (!C1 || !C2 || isModOrRefSet(MRI: AA.getModRefInfo(I: C1, Call2: C2)) ||
186 isModOrRefSet(MRI: AA.getModRefInfo(I: C2, Call2: C1))) {
187 // TODO: Could be more precise, but not really useful right now.
188 return ModRefInfo::ModRef;
189 }
190 }
191
192 ModRefInfo MR = ModRefInfo::NoModRef;
193 for (const auto &ASMemLoc : MemoryLocs) {
194 MR |= AA.getModRefInfo(I: Inst, OptLoc: ASMemLoc);
195 if (isModAndRefSet(MRI: MR))
196 return MR;
197 }
198
199 return MR;
200}
201
202AliasSet::PointerVector AliasSet::getPointers() const {
203 SmallSetVector<const Value *, 8> Pointers;
204 for (const MemoryLocation &MemLoc : MemoryLocs)
205 Pointers.insert(X: MemLoc.Ptr);
206 return Pointers.takeVector();
207}
208
209void AliasSetTracker::clear() {
210 PointerMap.clear();
211 AliasSets.clear();
212}
213
214/// mergeAliasSetsForMemoryLocation - Given a memory location, merge all alias
215/// sets that may alias it. Return the unified set, or nullptr if no aliasing
216/// set was found. A known existing alias set for the pointer value of the
217/// memory location can be passed in (or nullptr if not available). MustAliasAll
218/// is updated to true/false if the memory location is found to MustAlias all
219/// the sets it merged.
220AliasSet *AliasSetTracker::mergeAliasSetsForMemoryLocation(
221 const MemoryLocation &MemLoc, AliasSet *PtrAS, bool &MustAliasAll) {
222 AliasSet *FoundSet = nullptr;
223 MustAliasAll = true;
224 for (AliasSet &AS : llvm::make_early_inc_range(Range&: *this)) {
225 if (AS.Forward)
226 continue;
227
228 // An alias set that already contains a memory location with the same
229 // pointer value is directly assumed to MustAlias; we bypass the AA query in
230 // this case.
231 // Note: it is not guaranteed that AA would always provide the same result;
232 // a known exception are undef pointer values, where alias(undef, undef) is
233 // NoAlias, while we treat it as MustAlias.
234 if (&AS != PtrAS) {
235 AliasResult AR = AS.aliasesMemoryLocation(MemLoc, AA);
236 if (AR == AliasResult::NoAlias)
237 continue;
238
239 if (AR != AliasResult::MustAlias)
240 MustAliasAll = false;
241 }
242
243 if (!FoundSet) {
244 // If this is the first alias set ptr can go into, remember it.
245 FoundSet = &AS;
246 } else {
247 // Otherwise, we must merge the sets.
248 FoundSet->mergeSetIn(AS, AST&: *this, BatchAA&: AA);
249 }
250 }
251
252 return FoundSet;
253}
254
255AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
256 AliasSet *FoundSet = nullptr;
257 for (AliasSet &AS : llvm::make_early_inc_range(Range&: *this)) {
258 if (AS.Forward || !isModOrRefSet(MRI: AS.aliasesUnknownInst(Inst, AA)))
259 continue;
260 if (!FoundSet) {
261 // If this is the first alias set ptr can go into, remember it.
262 FoundSet = &AS;
263 } else {
264 // Otherwise, we must merge the sets.
265 FoundSet->mergeSetIn(AS, AST&: *this, BatchAA&: AA);
266 }
267 }
268 return FoundSet;
269}
270
271AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
272 // The alias sets are indexed with a map from the memory locations' pointer
273 // values. If the memory location is already registered, we can find it in the
274 // alias set associated with its pointer.
275 AliasSet *&MapEntry = PointerMap[MemLoc.Ptr];
276 if (MapEntry) {
277 collapseForwardingIn(AS&: MapEntry);
278 if (is_contained(Range&: MapEntry->MemoryLocs, Element: MemLoc))
279 return *MapEntry;
280 }
281
282 AliasSet *AS;
283 bool MustAliasAll = false;
284 if (AliasAnyAS) {
285 // At this point, the AST is saturated, so we only have one active alias
286 // set. That means we already know which alias set we want to return, and
287 // just need to add the memory location to that set to keep the data
288 // structure consistent.
289 // This, of course, means that we will never need a merge here.
290 AS = AliasAnyAS;
291 } else if (AliasSet *AliasAS = mergeAliasSetsForMemoryLocation(
292 MemLoc, PtrAS: MapEntry, MustAliasAll)) {
293 // Add it to the alias set it aliases.
294 AS = AliasAS;
295 } else {
296 // Otherwise create a new alias set to hold the new memory location.
297 AliasSets.push_back(val: AS = new AliasSet());
298 MustAliasAll = true;
299 }
300
301 // Register memory location in selected alias set.
302 AS->addMemoryLocation(AST&: *this, MemLoc, KnownMustAlias: MustAliasAll);
303 // Register selected alias set in pointer map (or ensure it is consistent with
304 // earlier map entry after taking into account new merging).
305 if (MapEntry) {
306 collapseForwardingIn(AS&: MapEntry);
307 assert(MapEntry == AS && "Memory locations with same pointer value cannot "
308 "be in different alias sets");
309 } else {
310 AS->addRef();
311 MapEntry = AS;
312 }
313 return *AS;
314}
315
316void AliasSetTracker::add(const MemoryLocation &Loc) {
317 addMemoryLocation(Loc, MR: ModRefInfo::NoModRef);
318}
319
320void AliasSetTracker::add(LoadInst *LI) {
321 if (isStrongerThanMonotonic(AO: LI->getOrdering()))
322 return addUnknown(I: LI);
323 addMemoryLocation(Loc: MemoryLocation::get(LI), MR: ModRefInfo::Ref);
324}
325
326void AliasSetTracker::add(StoreInst *SI) {
327 if (isStrongerThanMonotonic(AO: SI->getOrdering()))
328 return addUnknown(I: SI);
329 addMemoryLocation(Loc: MemoryLocation::get(SI), MR: ModRefInfo::Mod);
330}
331
332void AliasSetTracker::addWithoutAATags(StoreInst *SI) {
333 assert(!isStrongerThanMonotonic(SI->getOrdering()) &&
334 "Can't handle release stores here");
335 addMemoryLocation(Loc: MemoryLocation::get(SI).getWithoutAATags(),
336 MR: ModRefInfo::Mod);
337}
338
339void AliasSetTracker::add(VAArgInst *VAAI) {
340 addMemoryLocation(Loc: MemoryLocation::get(VI: VAAI), MR: ModRefInfo::ModRef);
341}
342
343void AliasSetTracker::add(AnyMemSetInst *MSI) {
344 addMemoryLocation(Loc: MemoryLocation::getForDest(MI: MSI), MR: ModRefInfo::Mod);
345}
346
347void AliasSetTracker::add(AnyMemTransferInst *MTI) {
348 addMemoryLocation(Loc: MemoryLocation::getForDest(MI: MTI), MR: ModRefInfo::Mod);
349 addMemoryLocation(Loc: MemoryLocation::getForSource(MTI), MR: ModRefInfo::Ref);
350}
351
352void AliasSetTracker::addUnknown(Instruction *Inst) {
353 if (auto *II = dyn_cast<IntrinsicInst>(Val: Inst)) {
354 // These intrinsics will show up as affecting memory, but they are just
355 // markers.
356 switch (II->getIntrinsicID()) {
357 default:
358 break;
359 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
360 case Intrinsic::allow_runtime_check:
361 case Intrinsic::allow_ubsan_check:
362 case Intrinsic::assume:
363 case Intrinsic::experimental_noalias_scope_decl:
364 case Intrinsic::sideeffect:
365 case Intrinsic::pseudoprobe:
366 return;
367 }
368 }
369 if (!Inst->mayReadOrWriteMemory())
370 return; // doesn't alias anything
371
372 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
373 AS->addUnknownInst(I: Inst, AA);
374 return;
375 }
376 AliasSets.push_back(val: new AliasSet());
377 AliasSets.back().addUnknownInst(I: Inst, AA);
378}
379
380void AliasSetTracker::add(Instruction *I) {
381 // Dispatch to one of the other add methods.
382 if (LoadInst *LI = dyn_cast<LoadInst>(Val: I))
383 return add(LI);
384 if (StoreInst *SI = dyn_cast<StoreInst>(Val: I))
385 return add(SI);
386 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(Val: I))
387 return add(VAAI);
388 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(Val: I))
389 return add(MSI);
390 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(Val: I))
391 return add(MTI);
392
393 // Handle all calls with known mod/ref sets genericall
394 if (auto *Call = dyn_cast<CallBase>(Val: I))
395 if (Call->onlyAccessesArgMemory()) {
396 ModRefInfo CallMask = AA.getMemoryEffects(Call).getModRef();
397
398 // Some intrinsics are marked as modifying memory for control flow
399 // modelling purposes, but don't actually modify any specific memory
400 // location.
401 using namespace PatternMatch;
402 if (Call->use_empty() &&
403 match(V: Call, P: m_Intrinsic<Intrinsic::invariant_start>()))
404 CallMask &= ModRefInfo::Ref;
405
406 for (auto IdxArgPair : enumerate(First: Call->args())) {
407 int ArgIdx = IdxArgPair.index();
408 const Value *Arg = IdxArgPair.value();
409 if (!Arg->getType()->isPointerTy())
410 continue;
411 MemoryLocation ArgLoc =
412 MemoryLocation::getForArgument(Call, ArgIdx, TLI: nullptr);
413 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
414 ArgMask &= CallMask;
415 if (!isNoModRef(MRI: ArgMask))
416 addMemoryLocation(Loc: ArgLoc, MR: ArgMask);
417 }
418 return;
419 }
420
421 return addUnknown(Inst: I);
422}
423
424void AliasSetTracker::add(BasicBlock &BB) {
425 for (auto &I : BB)
426 add(I: &I);
427}
428
429void AliasSetTracker::add(const AliasSetTracker &AST) {
430 assert(&AA == &AST.AA &&
431 "Merging AliasSetTracker objects with different Alias Analyses!");
432
433 // Loop over all of the alias sets in AST, adding the members contained
434 // therein into the current alias sets. This can cause alias sets to be
435 // merged together in the current AST.
436 for (const AliasSet &AS : AST) {
437 if (AS.Forward)
438 continue; // Ignore forwarding alias sets
439
440 // If there are any call sites in the alias set, add them to this AST.
441 for (Instruction *Inst : AS.UnknownInsts)
442 add(I: Inst);
443
444 // Loop over all of the memory locations in this alias set.
445 for (const MemoryLocation &ASMemLoc : AS.MemoryLocs)
446 addMemoryLocation(Loc: ASMemLoc, MR: AS.Access);
447 }
448}
449
450AliasSet &AliasSetTracker::mergeAllAliasSets() {
451 assert(!AliasAnyAS && (TotalAliasSetSize > SaturationThreshold) &&
452 "Full merge should happen once, when the saturation threshold is "
453 "reached");
454
455 // Collect all alias sets, so that we can drop references with impunity
456 // without worrying about iterator invalidation.
457 std::vector<AliasSet *> ASVector;
458 ASVector.reserve(n: SaturationThreshold);
459 for (AliasSet &AS : *this)
460 ASVector.push_back(x: &AS);
461
462 // Copy all instructions and memory locations into a new set, and forward all
463 // other sets to it.
464 AliasSets.push_back(val: new AliasSet());
465 AliasAnyAS = &AliasSets.back();
466 AliasAnyAS->Alias = AliasSet::SetMayAlias;
467 AliasAnyAS->Access = ModRefInfo::ModRef;
468 AliasAnyAS->AliasAny = true;
469
470 for (auto *Cur : ASVector) {
471 // If Cur was already forwarding, just forward to the new AS instead.
472 AliasSet *FwdTo = Cur->Forward;
473 if (FwdTo) {
474 Cur->Forward = AliasAnyAS;
475 AliasAnyAS->addRef();
476 FwdTo->dropRef(AST&: *this);
477 continue;
478 }
479
480 // Otherwise, perform the actual merge.
481 AliasAnyAS->mergeSetIn(AS&: *Cur, AST&: *this, BatchAA&: AA);
482 }
483
484 return *AliasAnyAS;
485}
486
487AliasSet &AliasSetTracker::addMemoryLocation(MemoryLocation Loc,
488 ModRefInfo MR) {
489 AliasSet &AS = getAliasSetFor(MemLoc: Loc);
490 AS.Access |= MR;
491
492 if (!AliasAnyAS && (TotalAliasSetSize > SaturationThreshold)) {
493 // The AST is now saturated. From here on, we conservatively consider all
494 // elements to alias each-other.
495 return mergeAllAliasSets();
496 }
497
498 return AS;
499}
500
501//===----------------------------------------------------------------------===//
502// AliasSet/AliasSetTracker Printing Support
503//===----------------------------------------------------------------------===//
504
505void AliasSet::print(raw_ostream &OS) const {
506 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
507 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
508 switch (Access) {
509 case ModRefInfo::NoModRef:
510 OS << "No access ";
511 break;
512 case ModRefInfo::Ref:
513 OS << "Ref ";
514 break;
515 case ModRefInfo::Mod:
516 OS << "Mod ";
517 break;
518 case ModRefInfo::ModRef:
519 OS << "Mod/Ref ";
520 break;
521 }
522 if (Forward)
523 OS << " forwarding to " << (void*)Forward;
524
525 if (!MemoryLocs.empty()) {
526 ListSeparator LS;
527 OS << "Memory locations: ";
528 for (const MemoryLocation &MemLoc : MemoryLocs) {
529 OS << LS;
530 MemLoc.Ptr->printAsOperand(O&: OS << "(");
531 if (MemLoc.Size == LocationSize::afterPointer())
532 OS << ", unknown after)";
533 else if (MemLoc.Size == LocationSize::beforeOrAfterPointer())
534 OS << ", unknown before-or-after)";
535 else
536 OS << ", " << MemLoc.Size << ")";
537 }
538 }
539 if (!UnknownInsts.empty()) {
540 ListSeparator LS;
541 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
542 for (Instruction *I : UnknownInsts) {
543 OS << LS;
544 if (I->hasName())
545 I->printAsOperand(O&: OS);
546 else
547 I->print(O&: OS);
548 }
549 }
550 OS << "\n";
551}
552
553void AliasSetTracker::print(raw_ostream &OS) const {
554 OS << "Alias Set Tracker: " << AliasSets.size();
555 if (AliasAnyAS)
556 OS << " (Saturated)";
557 OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
558 for (const AliasSet &AS : *this)
559 AS.print(OS);
560 OS << "\n";
561}
562
563#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
564LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
565LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
566#endif
567
568//===----------------------------------------------------------------------===//
569// AliasSetPrinter Pass
570//===----------------------------------------------------------------------===//
571
572AliasSetsPrinterPass::AliasSetsPrinterPass(raw_ostream &OS) : OS(OS) {}
573
574PreservedAnalyses AliasSetsPrinterPass::run(Function &F,
575 FunctionAnalysisManager &AM) {
576 auto &AA = AM.getResult<AAManager>(IR&: F);
577 BatchAAResults BatchAA(AA);
578 AliasSetTracker Tracker(BatchAA);
579 OS << "Alias sets for function '" << F.getName() << "':\n";
580 for (Instruction &I : instructions(F))
581 Tracker.add(I: &I);
582 Tracker.print(OS);
583 return PreservedAnalyses::all();
584}
585