[ARM] Generate 8.1-m CSINC, CSNEG and CSINV instructions.
[llvm-core.git] / lib / Analysis / AliasSetTracker.cpp
bloba6e5b9fab558d09b6fd3d3da0530443e72ca3c5f
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/AliasSetTracker.h"
14 #include "llvm/Analysis/AliasAnalysis.h"
15 #include "llvm/Analysis/GuardUtils.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/MemoryLocation.h"
18 #include "llvm/Analysis/MemorySSA.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/Instruction.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/PatternMatch.h"
29 #include "llvm/IR/Value.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/AtomicOrdering.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Compiler.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <vector>
42 using namespace llvm;
44 static cl::opt<unsigned>
45 SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
46 cl::init(250),
47 cl::desc("The maximum number of pointers may-alias "
48 "sets may contain before degradation"));
50 /// mergeSetIn - Merge the specified alias set into this alias set.
51 ///
52 void AliasSet::mergeSetIn(AliasSet &AS, AliasSetTracker &AST) {
53 assert(!AS.Forward && "Alias set is already forwarding!");
54 assert(!Forward && "This set is a forwarding set!!");
56 bool WasMustAlias = (Alias == SetMustAlias);
57 // Update the alias and access types of this set...
58 Access |= AS.Access;
59 Alias |= AS.Alias;
61 if (Alias == SetMustAlias) {
62 // Check that these two merged sets really are must aliases. Since both
63 // used to be must-alias sets, we can just check any pointer from each set
64 // for aliasing.
65 AliasAnalysis &AA = AST.getAliasAnalysis();
66 PointerRec *L = getSomePointer();
67 PointerRec *R = AS.getSomePointer();
69 // If the pointers are not a must-alias pair, this set becomes a may alias.
70 if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
71 MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
72 MustAlias)
73 Alias = SetMayAlias;
76 if (Alias == SetMayAlias) {
77 if (WasMustAlias)
78 AST.TotalMayAliasSetSize += size();
79 if (AS.Alias == SetMustAlias)
80 AST.TotalMayAliasSetSize += AS.size();
83 bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
84 if (UnknownInsts.empty()) { // Merge call sites...
85 if (ASHadUnknownInsts) {
86 std::swap(UnknownInsts, AS.UnknownInsts);
87 addRef();
89 } else if (ASHadUnknownInsts) {
90 UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
91 AS.UnknownInsts.clear();
94 AS.Forward = this; // Forward across AS now...
95 addRef(); // AS is now pointing to us...
97 // Merge the list of constituent pointers...
98 if (AS.PtrList) {
99 SetSize += AS.size();
100 AS.SetSize = 0;
101 *PtrListEnd = AS.PtrList;
102 AS.PtrList->setPrevInList(PtrListEnd);
103 PtrListEnd = AS.PtrListEnd;
105 AS.PtrList = nullptr;
106 AS.PtrListEnd = &AS.PtrList;
107 assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
109 if (ASHadUnknownInsts)
110 AS.dropRef(AST);
113 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
114 if (AliasSet *Fwd = AS->Forward) {
115 Fwd->dropRef(*this);
116 AS->Forward = nullptr;
117 } else // Update TotalMayAliasSetSize only if not forwarding.
118 if (AS->Alias == AliasSet::SetMayAlias)
119 TotalMayAliasSetSize -= AS->size();
121 AliasSets.erase(AS);
124 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
125 assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
126 AST.removeAliasSet(this);
129 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
130 LocationSize Size, const AAMDNodes &AAInfo,
131 bool KnownMustAlias, bool SkipSizeUpdate) {
132 assert(!Entry.hasAliasSet() && "Entry already in set!");
134 // Check to see if we have to downgrade to _may_ alias.
135 if (isMustAlias())
136 if (PointerRec *P = getSomePointer()) {
137 if (!KnownMustAlias) {
138 AliasAnalysis &AA = AST.getAliasAnalysis();
139 AliasResult Result = AA.alias(
140 MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
141 MemoryLocation(Entry.getValue(), Size, AAInfo));
142 if (Result != MustAlias) {
143 Alias = SetMayAlias;
144 AST.TotalMayAliasSetSize += size();
146 assert(Result != NoAlias && "Cannot be part of must set!");
147 } else if (!SkipSizeUpdate)
148 P->updateSizeAndAAInfo(Size, AAInfo);
151 Entry.setAliasSet(this);
152 Entry.updateSizeAndAAInfo(Size, AAInfo);
154 // Add it to the end of the list...
155 ++SetSize;
156 assert(*PtrListEnd == nullptr && "End of list is not null?");
157 *PtrListEnd = &Entry;
158 PtrListEnd = Entry.setPrevInList(PtrListEnd);
159 assert(*PtrListEnd == nullptr && "End of list is not null?");
160 // Entry points to alias set.
161 addRef();
163 if (Alias == SetMayAlias)
164 AST.TotalMayAliasSetSize++;
167 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
168 if (UnknownInsts.empty())
169 addRef();
170 UnknownInsts.emplace_back(I);
172 // Guards are marked as modifying memory for control flow modelling purposes,
173 // but don't actually modify any specific memory location.
174 using namespace PatternMatch;
175 bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
176 !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
177 if (!MayWriteMemory) {
178 Alias = SetMayAlias;
179 Access |= RefAccess;
180 return;
183 // FIXME: This should use mod/ref information to make this not suck so bad
184 Alias = SetMayAlias;
185 Access = ModRefAccess;
188 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
189 /// members in the set return the appropriate AliasResult. Otherwise return
190 /// NoAlias.
192 AliasResult AliasSet::aliasesPointer(const Value *Ptr, LocationSize Size,
193 const AAMDNodes &AAInfo,
194 AliasAnalysis &AA) const {
195 if (AliasAny)
196 return MayAlias;
198 if (Alias == SetMustAlias) {
199 assert(UnknownInsts.empty() && "Illegal must alias set!");
201 // If this is a set of MustAliases, only check to see if the pointer aliases
202 // SOME value in the set.
203 PointerRec *SomePtr = getSomePointer();
204 assert(SomePtr && "Empty must-alias set??");
205 return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
206 SomePtr->getAAInfo()),
207 MemoryLocation(Ptr, Size, AAInfo));
210 // If this is a may-alias set, we have to check all of the pointers in the set
211 // to be sure it doesn't alias the set...
212 for (iterator I = begin(), E = end(); I != E; ++I)
213 if (AliasResult AR = AA.alias(
214 MemoryLocation(Ptr, Size, AAInfo),
215 MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
216 return AR;
218 // Check the unknown instructions...
219 if (!UnknownInsts.empty()) {
220 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
221 if (auto *Inst = getUnknownInst(i))
222 if (isModOrRefSet(
223 AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
224 return MayAlias;
227 return NoAlias;
230 bool AliasSet::aliasesUnknownInst(const Instruction *Inst,
231 AliasAnalysis &AA) const {
233 if (AliasAny)
234 return true;
236 assert(Inst->mayReadOrWriteMemory() &&
237 "Instruction must either read or write memory.");
239 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
240 if (auto *UnknownInst = getUnknownInst(i)) {
241 const auto *C1 = dyn_cast<CallBase>(UnknownInst);
242 const auto *C2 = dyn_cast<CallBase>(Inst);
243 if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
244 isModOrRefSet(AA.getModRefInfo(C2, C1)))
245 return true;
249 for (iterator I = begin(), E = end(); I != E; ++I)
250 if (isModOrRefSet(AA.getModRefInfo(
251 Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
252 return true;
254 return false;
257 Instruction* AliasSet::getUniqueInstruction() {
258 if (AliasAny)
259 // May have collapses alias set
260 return nullptr;
261 if (begin() != end()) {
262 if (!UnknownInsts.empty())
263 // Another instruction found
264 return nullptr;
265 if (std::next(begin()) != end())
266 // Another instruction found
267 return nullptr;
268 Value *Addr = begin()->getValue();
269 assert(!Addr->user_empty() &&
270 "where's the instruction which added this pointer?");
271 if (std::next(Addr->user_begin()) != Addr->user_end())
272 // Another instruction found -- this is really restrictive
273 // TODO: generalize!
274 return nullptr;
275 return cast<Instruction>(*(Addr->user_begin()));
277 if (1 != UnknownInsts.size())
278 return nullptr;
279 return cast<Instruction>(UnknownInsts[0]);
282 void AliasSetTracker::clear() {
283 // Delete all the PointerRec entries.
284 for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
285 I != E; ++I)
286 I->second->eraseFromList();
288 PointerMap.clear();
290 // The alias sets should all be clear now.
291 AliasSets.clear();
294 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
295 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
296 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
297 /// is found to MustAlias all the sets it merged.
298 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
299 LocationSize Size,
300 const AAMDNodes &AAInfo,
301 bool &MustAliasAll) {
302 AliasSet *FoundSet = nullptr;
303 AliasResult AllAR = MustAlias;
304 for (iterator I = begin(), E = end(); I != E;) {
305 iterator Cur = I++;
306 if (Cur->Forward)
307 continue;
309 AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
310 if (AR == NoAlias)
311 continue;
313 AllAR =
314 AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No
316 if (!FoundSet) {
317 // If this is the first alias set ptr can go into, remember it.
318 FoundSet = &*Cur;
319 } else {
320 // Otherwise, we must merge the sets.
321 FoundSet->mergeSetIn(*Cur, *this);
325 MustAliasAll = (AllAR == MustAlias);
326 return FoundSet;
329 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
330 AliasSet *FoundSet = nullptr;
331 for (iterator I = begin(), E = end(); I != E;) {
332 iterator Cur = I++;
333 if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
334 continue;
335 if (!FoundSet) {
336 // If this is the first alias set ptr can go into, remember it.
337 FoundSet = &*Cur;
338 } else {
339 // Otherwise, we must merge the sets.
340 FoundSet->mergeSetIn(*Cur, *this);
343 return FoundSet;
346 AliasSet &AliasSetTracker::getAliasSetFor(const MemoryLocation &MemLoc) {
348 Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
349 const LocationSize Size = MemLoc.Size;
350 const AAMDNodes &AAInfo = MemLoc.AATags;
352 AliasSet::PointerRec &Entry = getEntryFor(Pointer);
354 if (AliasAnyAS) {
355 // At this point, the AST is saturated, so we only have one active alias
356 // set. That means we already know which alias set we want to return, and
357 // just need to add the pointer to that set to keep the data structure
358 // consistent.
359 // This, of course, means that we will never need a merge here.
360 if (Entry.hasAliasSet()) {
361 Entry.updateSizeAndAAInfo(Size, AAInfo);
362 assert(Entry.getAliasSet(*this) == AliasAnyAS &&
363 "Entry in saturated AST must belong to only alias set");
364 } else {
365 AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
367 return *AliasAnyAS;
370 bool MustAliasAll = false;
371 // Check to see if the pointer is already known.
372 if (Entry.hasAliasSet()) {
373 // If the size changed, we may need to merge several alias sets.
374 // Note that we can *not* return the result of mergeAliasSetsForPointer
375 // due to a quirk of alias analysis behavior. Since alias(undef, undef)
376 // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
377 // the right set for undef, even if it exists.
378 if (Entry.updateSizeAndAAInfo(Size, AAInfo))
379 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
380 // Return the set!
381 return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
384 if (AliasSet *AS =
385 mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
386 // Add it to the alias set it aliases.
387 AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
388 return *AS;
391 // Otherwise create a new alias set to hold the loaded pointer.
392 AliasSets.push_back(new AliasSet());
393 AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
394 return AliasSets.back();
397 void AliasSetTracker::add(Value *Ptr, LocationSize Size,
398 const AAMDNodes &AAInfo) {
399 addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
402 void AliasSetTracker::add(LoadInst *LI) {
403 if (isStrongerThanMonotonic(LI->getOrdering()))
404 return addUnknown(LI);
405 addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
408 void AliasSetTracker::add(StoreInst *SI) {
409 if (isStrongerThanMonotonic(SI->getOrdering()))
410 return addUnknown(SI);
411 addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
414 void AliasSetTracker::add(VAArgInst *VAAI) {
415 addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
418 void AliasSetTracker::add(AnyMemSetInst *MSI) {
419 addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
422 void AliasSetTracker::add(AnyMemTransferInst *MTI) {
423 addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
424 addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
427 void AliasSetTracker::addUnknown(Instruction *Inst) {
428 if (isa<DbgInfoIntrinsic>(Inst))
429 return; // Ignore DbgInfo Intrinsics.
431 if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
432 // These intrinsics will show up as affecting memory, but they are just
433 // markers.
434 switch (II->getIntrinsicID()) {
435 default:
436 break;
437 // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
438 case Intrinsic::assume:
439 case Intrinsic::sideeffect:
440 return;
443 if (!Inst->mayReadOrWriteMemory())
444 return; // doesn't alias anything
446 if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
447 AS->addUnknownInst(Inst, AA);
448 return;
450 AliasSets.push_back(new AliasSet());
451 AliasSets.back().addUnknownInst(Inst, AA);
454 void AliasSetTracker::add(Instruction *I) {
455 // Dispatch to one of the other add methods.
456 if (LoadInst *LI = dyn_cast<LoadInst>(I))
457 return add(LI);
458 if (StoreInst *SI = dyn_cast<StoreInst>(I))
459 return add(SI);
460 if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
461 return add(VAAI);
462 if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
463 return add(MSI);
464 if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
465 return add(MTI);
467 // Handle all calls with known mod/ref sets genericall
468 if (auto *Call = dyn_cast<CallBase>(I))
469 if (Call->onlyAccessesArgMemory()) {
470 auto getAccessFromModRef = [](ModRefInfo MRI) {
471 if (isRefSet(MRI) && isModSet(MRI))
472 return AliasSet::ModRefAccess;
473 else if (isModSet(MRI))
474 return AliasSet::ModAccess;
475 else if (isRefSet(MRI))
476 return AliasSet::RefAccess;
477 else
478 return AliasSet::NoAccess;
481 ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
483 // Some intrinsics are marked as modifying memory for control flow
484 // modelling purposes, but don't actually modify any specific memory
485 // location.
486 using namespace PatternMatch;
487 if (Call->use_empty() &&
488 match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
489 CallMask = clearMod(CallMask);
491 for (auto IdxArgPair : enumerate(Call->args())) {
492 int ArgIdx = IdxArgPair.index();
493 const Value *Arg = IdxArgPair.value();
494 if (!Arg->getType()->isPointerTy())
495 continue;
496 MemoryLocation ArgLoc =
497 MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
498 ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
499 ArgMask = intersectModRef(CallMask, ArgMask);
500 if (!isNoModRef(ArgMask))
501 addPointer(ArgLoc, getAccessFromModRef(ArgMask));
503 return;
506 return addUnknown(I);
509 void AliasSetTracker::add(BasicBlock &BB) {
510 for (auto &I : BB)
511 add(&I);
514 void AliasSetTracker::add(const AliasSetTracker &AST) {
515 assert(&AA == &AST.AA &&
516 "Merging AliasSetTracker objects with different Alias Analyses!");
518 // Loop over all of the alias sets in AST, adding the pointers contained
519 // therein into the current alias sets. This can cause alias sets to be
520 // merged together in the current AST.
521 for (const AliasSet &AS : AST) {
522 if (AS.Forward)
523 continue; // Ignore forwarding alias sets
525 // If there are any call sites in the alias set, add them to this AST.
526 for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
527 if (auto *Inst = AS.getUnknownInst(i))
528 add(Inst);
530 // Loop over all of the pointers in this alias set.
531 for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
532 addPointer(
533 MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
534 (AliasSet::AccessLattice)AS.Access);
538 void AliasSetTracker::addAllInstructionsInLoopUsingMSSA() {
539 assert(MSSA && L && "MSSA and L must be available");
540 for (const BasicBlock *BB : L->blocks())
541 if (auto *Accesses = MSSA->getBlockAccesses(BB))
542 for (auto &Access : *Accesses)
543 if (auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
544 add(MUD->getMemoryInst());
547 // deleteValue method - This method is used to remove a pointer value from the
548 // AliasSetTracker entirely. It should be used when an instruction is deleted
549 // from the program to update the AST. If you don't use this, you would have
550 // dangling pointers to deleted instructions.
552 void AliasSetTracker::deleteValue(Value *PtrVal) {
553 // First, look up the PointerRec for this pointer.
554 PointerMapType::iterator I = PointerMap.find_as(PtrVal);
555 if (I == PointerMap.end()) return; // Noop
557 // If we found one, remove the pointer from the alias set it is in.
558 AliasSet::PointerRec *PtrValEnt = I->second;
559 AliasSet *AS = PtrValEnt->getAliasSet(*this);
561 // Unlink and delete from the list of values.
562 PtrValEnt->eraseFromList();
564 if (AS->Alias == AliasSet::SetMayAlias) {
565 AS->SetSize--;
566 TotalMayAliasSetSize--;
569 // Stop using the alias set.
570 AS->dropRef(*this);
572 PointerMap.erase(I);
575 // copyValue - This method should be used whenever a preexisting value in the
576 // program is copied or cloned, introducing a new value. Note that it is ok for
577 // clients that use this method to introduce the same value multiple times: if
578 // the tracker already knows about a value, it will ignore the request.
580 void AliasSetTracker::copyValue(Value *From, Value *To) {
581 // First, look up the PointerRec for this pointer.
582 PointerMapType::iterator I = PointerMap.find_as(From);
583 if (I == PointerMap.end())
584 return; // Noop
585 assert(I->second->hasAliasSet() && "Dead entry?");
587 AliasSet::PointerRec &Entry = getEntryFor(To);
588 if (Entry.hasAliasSet()) return; // Already in the tracker!
590 // getEntryFor above may invalidate iterator \c I, so reinitialize it.
591 I = PointerMap.find_as(From);
592 // Add it to the alias set it aliases...
593 AliasSet *AS = I->second->getAliasSet(*this);
594 AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(),
595 true, true);
598 AliasSet &AliasSetTracker::mergeAllAliasSets() {
599 assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
600 "Full merge should happen once, when the saturation threshold is "
601 "reached");
603 // Collect all alias sets, so that we can drop references with impunity
604 // without worrying about iterator invalidation.
605 std::vector<AliasSet *> ASVector;
606 ASVector.reserve(SaturationThreshold);
607 for (iterator I = begin(), E = end(); I != E; I++)
608 ASVector.push_back(&*I);
610 // Copy all instructions and pointers into a new set, and forward all other
611 // sets to it.
612 AliasSets.push_back(new AliasSet());
613 AliasAnyAS = &AliasSets.back();
614 AliasAnyAS->Alias = AliasSet::SetMayAlias;
615 AliasAnyAS->Access = AliasSet::ModRefAccess;
616 AliasAnyAS->AliasAny = true;
618 for (auto Cur : ASVector) {
619 // If Cur was already forwarding, just forward to the new AS instead.
620 AliasSet *FwdTo = Cur->Forward;
621 if (FwdTo) {
622 Cur->Forward = AliasAnyAS;
623 AliasAnyAS->addRef();
624 FwdTo->dropRef(*this);
625 continue;
628 // Otherwise, perform the actual merge.
629 AliasAnyAS->mergeSetIn(*Cur, *this);
632 return *AliasAnyAS;
635 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
636 AliasSet::AccessLattice E) {
637 AliasSet &AS = getAliasSetFor(Loc);
638 AS.Access |= E;
640 if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
641 // The AST is now saturated. From here on, we conservatively consider all
642 // pointers to alias each-other.
643 return mergeAllAliasSets();
646 return AS;
649 //===----------------------------------------------------------------------===//
650 // AliasSet/AliasSetTracker Printing Support
651 //===----------------------------------------------------------------------===//
653 void AliasSet::print(raw_ostream &OS) const {
654 OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
655 OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
656 switch (Access) {
657 case NoAccess: OS << "No access "; break;
658 case RefAccess: OS << "Ref "; break;
659 case ModAccess: OS << "Mod "; break;
660 case ModRefAccess: OS << "Mod/Ref "; break;
661 default: llvm_unreachable("Bad value for Access!");
663 if (Forward)
664 OS << " forwarding to " << (void*)Forward;
666 if (!empty()) {
667 OS << "Pointers: ";
668 for (iterator I = begin(), E = end(); I != E; ++I) {
669 if (I != begin()) OS << ", ";
670 I.getPointer()->printAsOperand(OS << "(");
671 if (I.getSize() == LocationSize::unknown())
672 OS << ", unknown)";
673 else
674 OS << ", " << I.getSize() << ")";
677 if (!UnknownInsts.empty()) {
678 OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
679 for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
680 if (i) OS << ", ";
681 if (auto *I = getUnknownInst(i)) {
682 if (I->hasName())
683 I->printAsOperand(OS);
684 else
685 I->print(OS);
689 OS << "\n";
692 void AliasSetTracker::print(raw_ostream &OS) const {
693 OS << "Alias Set Tracker: " << AliasSets.size() << " alias sets for "
694 << PointerMap.size() << " pointer values.\n";
695 for (const AliasSet &AS : *this)
696 AS.print(OS);
697 OS << "\n";
700 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
701 LLVM_DUMP_METHOD void AliasSet::dump() const { print(dbgs()); }
702 LLVM_DUMP_METHOD void AliasSetTracker::dump() const { print(dbgs()); }
703 #endif
705 //===----------------------------------------------------------------------===//
706 // ASTCallbackVH Class Implementation
707 //===----------------------------------------------------------------------===//
709 void AliasSetTracker::ASTCallbackVH::deleted() {
710 assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
711 AST->deleteValue(getValPtr());
712 // this now dangles!
715 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
716 AST->copyValue(getValPtr(), V);
719 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
720 : CallbackVH(V), AST(ast) {}
722 AliasSetTracker::ASTCallbackVH &
723 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
724 return *this = ASTCallbackVH(V, AST);
727 //===----------------------------------------------------------------------===//
728 // AliasSetPrinter Pass
729 //===----------------------------------------------------------------------===//
731 namespace {
733 class AliasSetPrinter : public FunctionPass {
734 AliasSetTracker *Tracker;
736 public:
737 static char ID; // Pass identification, replacement for typeid
739 AliasSetPrinter() : FunctionPass(ID) {
740 initializeAliasSetPrinterPass(*PassRegistry::getPassRegistry());
743 void getAnalysisUsage(AnalysisUsage &AU) const override {
744 AU.setPreservesAll();
745 AU.addRequired<AAResultsWrapperPass>();
748 bool runOnFunction(Function &F) override {
749 auto &AAWP = getAnalysis<AAResultsWrapperPass>();
750 Tracker = new AliasSetTracker(AAWP.getAAResults());
751 errs() << "Alias sets for function '" << F.getName() << "':\n";
752 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
753 Tracker->add(&*I);
754 Tracker->print(errs());
755 delete Tracker;
756 return false;
760 } // end anonymous namespace
762 char AliasSetPrinter::ID = 0;
764 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
765 "Alias Set Printer", false, true)
766 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
767 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
768 "Alias Set Printer", false, true)