LLVM  10.0.0
AliasSetTracker.cpp
Go to the documentation of this file.
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 
16 #include "llvm/Analysis/LoopInfo.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/InitializePasses.h"
31 #include "llvm/Pass.h"
33 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Debug.h"
39 #include <cassert>
40 #include <cstdint>
41 #include <vector>
42 
43 using namespace llvm;
44 
45 static cl::opt<unsigned>
46  SaturationThreshold("alias-set-saturation-threshold", cl::Hidden,
47  cl::init(250),
48  cl::desc("The maximum number of pointers may-alias "
49  "sets may contain before degradation"));
50 
51 /// mergeSetIn - Merge the specified alias set into this alias set.
52 ///
54  assert(!AS.Forward && "Alias set is already forwarding!");
55  assert(!Forward && "This set is a forwarding set!!");
56 
57  bool WasMustAlias = (Alias == SetMustAlias);
58  // Update the alias and access types of this set...
59  Access |= AS.Access;
60  Alias |= AS.Alias;
61 
62  if (Alias == SetMustAlias) {
63  // Check that these two merged sets really are must aliases. Since both
64  // used to be must-alias sets, we can just check any pointer from each set
65  // for aliasing.
66  AliasAnalysis &AA = AST.getAliasAnalysis();
67  PointerRec *L = getSomePointer();
68  PointerRec *R = AS.getSomePointer();
69 
70  // If the pointers are not a must-alias pair, this set becomes a may alias.
71  if (AA.alias(MemoryLocation(L->getValue(), L->getSize(), L->getAAInfo()),
72  MemoryLocation(R->getValue(), R->getSize(), R->getAAInfo())) !=
73  MustAlias)
74  Alias = SetMayAlias;
75  }
76 
77  if (Alias == SetMayAlias) {
78  if (WasMustAlias)
79  AST.TotalMayAliasSetSize += size();
80  if (AS.Alias == SetMustAlias)
81  AST.TotalMayAliasSetSize += AS.size();
82  }
83 
84  bool ASHadUnknownInsts = !AS.UnknownInsts.empty();
85  if (UnknownInsts.empty()) { // Merge call sites...
86  if (ASHadUnknownInsts) {
87  std::swap(UnknownInsts, AS.UnknownInsts);
88  addRef();
89  }
90  } else if (ASHadUnknownInsts) {
91  UnknownInsts.insert(UnknownInsts.end(), AS.UnknownInsts.begin(), AS.UnknownInsts.end());
92  AS.UnknownInsts.clear();
93  }
94 
95  AS.Forward = this; // Forward across AS now...
96  addRef(); // AS is now pointing to us...
97 
98  // Merge the list of constituent pointers...
99  if (AS.PtrList) {
100  SetSize += AS.size();
101  AS.SetSize = 0;
102  *PtrListEnd = AS.PtrList;
103  AS.PtrList->setPrevInList(PtrListEnd);
104  PtrListEnd = AS.PtrListEnd;
105 
106  AS.PtrList = nullptr;
107  AS.PtrListEnd = &AS.PtrList;
108  assert(*AS.PtrListEnd == nullptr && "End of list is not null?");
109  }
110  if (ASHadUnknownInsts)
111  AS.dropRef(AST);
112 }
113 
114 void AliasSetTracker::removeAliasSet(AliasSet *AS) {
115  if (AliasSet *Fwd = AS->Forward) {
116  Fwd->dropRef(*this);
117  AS->Forward = nullptr;
118  } else // Update TotalMayAliasSetSize only if not forwarding.
119  if (AS->Alias == AliasSet::SetMayAlias)
120  TotalMayAliasSetSize -= AS->size();
121 
122  AliasSets.erase(AS);
123  // If we've removed the saturated alias set, set saturated marker back to
124  // nullptr and ensure this tracker is empty.
125  if (AS == AliasAnyAS) {
126  AliasAnyAS = nullptr;
127  assert(AliasSets.empty() && "Tracker not empty");
128  }
129 }
130 
131 void AliasSet::removeFromTracker(AliasSetTracker &AST) {
132  assert(RefCount == 0 && "Cannot remove non-dead alias set from tracker!");
133  AST.removeAliasSet(this);
134 }
135 
136 void AliasSet::addPointer(AliasSetTracker &AST, PointerRec &Entry,
137  LocationSize Size, const AAMDNodes &AAInfo,
138  bool KnownMustAlias, bool SkipSizeUpdate) {
139  assert(!Entry.hasAliasSet() && "Entry already in set!");
140 
141  // Check to see if we have to downgrade to _may_ alias.
142  if (isMustAlias())
143  if (PointerRec *P = getSomePointer()) {
144  if (!KnownMustAlias) {
145  AliasAnalysis &AA = AST.getAliasAnalysis();
146  AliasResult Result = AA.alias(
147  MemoryLocation(P->getValue(), P->getSize(), P->getAAInfo()),
148  MemoryLocation(Entry.getValue(), Size, AAInfo));
149  if (Result != MustAlias) {
150  Alias = SetMayAlias;
151  AST.TotalMayAliasSetSize += size();
152  }
153  assert(Result != NoAlias && "Cannot be part of must set!");
154  } else if (!SkipSizeUpdate)
155  P->updateSizeAndAAInfo(Size, AAInfo);
156  }
157 
158  Entry.setAliasSet(this);
159  Entry.updateSizeAndAAInfo(Size, AAInfo);
160 
161  // Add it to the end of the list...
162  ++SetSize;
163  assert(*PtrListEnd == nullptr && "End of list is not null?");
164  *PtrListEnd = &Entry;
165  PtrListEnd = Entry.setPrevInList(PtrListEnd);
166  assert(*PtrListEnd == nullptr && "End of list is not null?");
167  // Entry points to alias set.
168  addRef();
169 
170  if (Alias == SetMayAlias)
171  AST.TotalMayAliasSetSize++;
172 }
173 
174 void AliasSet::addUnknownInst(Instruction *I, AliasAnalysis &AA) {
175  if (UnknownInsts.empty())
176  addRef();
177  UnknownInsts.emplace_back(I);
178 
179  // Guards are marked as modifying memory for control flow modelling purposes,
180  // but don't actually modify any specific memory location.
181  using namespace PatternMatch;
182  bool MayWriteMemory = I->mayWriteToMemory() && !isGuard(I) &&
183  !(I->use_empty() && match(I, m_Intrinsic<Intrinsic::invariant_start>()));
184  if (!MayWriteMemory) {
185  Alias = SetMayAlias;
186  Access |= RefAccess;
187  return;
188  }
189 
190  // FIXME: This should use mod/ref information to make this not suck so bad
191  Alias = SetMayAlias;
192  Access = ModRefAccess;
193 }
194 
195 /// aliasesPointer - If the specified pointer "may" (or must) alias one of the
196 /// members in the set return the appropriate AliasResult. Otherwise return
197 /// NoAlias.
198 ///
200  const AAMDNodes &AAInfo,
201  AliasAnalysis &AA) const {
202  if (AliasAny)
203  return MayAlias;
204 
205  if (Alias == SetMustAlias) {
206  assert(UnknownInsts.empty() && "Illegal must alias set!");
207 
208  // If this is a set of MustAliases, only check to see if the pointer aliases
209  // SOME value in the set.
210  PointerRec *SomePtr = getSomePointer();
211  assert(SomePtr && "Empty must-alias set??");
212  return AA.alias(MemoryLocation(SomePtr->getValue(), SomePtr->getSize(),
213  SomePtr->getAAInfo()),
214  MemoryLocation(Ptr, Size, AAInfo));
215  }
216 
217  // If this is a may-alias set, we have to check all of the pointers in the set
218  // to be sure it doesn't alias the set...
219  for (iterator I = begin(), E = end(); I != E; ++I)
220  if (AliasResult AR = AA.alias(
221  MemoryLocation(Ptr, Size, AAInfo),
222  MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo())))
223  return AR;
224 
225  // Check the unknown instructions...
226  if (!UnknownInsts.empty()) {
227  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i)
228  if (auto *Inst = getUnknownInst(i))
229  if (isModOrRefSet(
230  AA.getModRefInfo(Inst, MemoryLocation(Ptr, Size, AAInfo))))
231  return MayAlias;
232  }
233 
234  return NoAlias;
235 }
236 
238  AliasAnalysis &AA) const {
239 
240  if (AliasAny)
241  return true;
242 
243  assert(Inst->mayReadOrWriteMemory() &&
244  "Instruction must either read or write memory.");
245 
246  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
247  if (auto *UnknownInst = getUnknownInst(i)) {
248  const auto *C1 = dyn_cast<CallBase>(UnknownInst);
249  const auto *C2 = dyn_cast<CallBase>(Inst);
250  if (!C1 || !C2 || isModOrRefSet(AA.getModRefInfo(C1, C2)) ||
251  isModOrRefSet(AA.getModRefInfo(C2, C1)))
252  return true;
253  }
254  }
255 
256  for (iterator I = begin(), E = end(); I != E; ++I)
258  Inst, MemoryLocation(I.getPointer(), I.getSize(), I.getAAInfo()))))
259  return true;
260 
261  return false;
262 }
263 
265  if (AliasAny)
266  // May have collapses alias set
267  return nullptr;
268  if (begin() != end()) {
269  if (!UnknownInsts.empty())
270  // Another instruction found
271  return nullptr;
272  if (std::next(begin()) != end())
273  // Another instruction found
274  return nullptr;
275  Value *Addr = begin()->getValue();
276  assert(!Addr->user_empty() &&
277  "where's the instruction which added this pointer?");
278  if (std::next(Addr->user_begin()) != Addr->user_end())
279  // Another instruction found -- this is really restrictive
280  // TODO: generalize!
281  return nullptr;
282  return cast<Instruction>(*(Addr->user_begin()));
283  }
284  if (1 != UnknownInsts.size())
285  return nullptr;
286  return cast<Instruction>(UnknownInsts[0]);
287 }
288 
290  // Delete all the PointerRec entries.
291  for (PointerMapType::iterator I = PointerMap.begin(), E = PointerMap.end();
292  I != E; ++I)
293  I->second->eraseFromList();
294 
295  PointerMap.clear();
296 
297  // The alias sets should all be clear now.
298  AliasSets.clear();
299 }
300 
301 /// mergeAliasSetsForPointer - Given a pointer, merge all alias sets that may
302 /// alias the pointer. Return the unified set, or nullptr if no set that aliases
303 /// the pointer was found. MustAliasAll is updated to true/false if the pointer
304 /// is found to MustAlias all the sets it merged.
305 AliasSet *AliasSetTracker::mergeAliasSetsForPointer(const Value *Ptr,
306  LocationSize Size,
307  const AAMDNodes &AAInfo,
308  bool &MustAliasAll) {
309  AliasSet *FoundSet = nullptr;
310  AliasResult AllAR = MustAlias;
311  for (iterator I = begin(), E = end(); I != E;) {
312  iterator Cur = I++;
313  if (Cur->Forward)
314  continue;
315 
316  AliasResult AR = Cur->aliasesPointer(Ptr, Size, AAInfo, AA);
317  if (AR == NoAlias)
318  continue;
319 
320  AllAR =
321  AliasResult(AllAR & AR); // Possible downgrade to May/Partial, even No
322 
323  if (!FoundSet) {
324  // If this is the first alias set ptr can go into, remember it.
325  FoundSet = &*Cur;
326  } else {
327  // Otherwise, we must merge the sets.
328  FoundSet->mergeSetIn(*Cur, *this);
329  }
330  }
331 
332  MustAliasAll = (AllAR == MustAlias);
333  return FoundSet;
334 }
335 
336 AliasSet *AliasSetTracker::findAliasSetForUnknownInst(Instruction *Inst) {
337  AliasSet *FoundSet = nullptr;
338  for (iterator I = begin(), E = end(); I != E;) {
339  iterator Cur = I++;
340  if (Cur->Forward || !Cur->aliasesUnknownInst(Inst, AA))
341  continue;
342  if (!FoundSet) {
343  // If this is the first alias set ptr can go into, remember it.
344  FoundSet = &*Cur;
345  } else {
346  // Otherwise, we must merge the sets.
347  FoundSet->mergeSetIn(*Cur, *this);
348  }
349  }
350  return FoundSet;
351 }
352 
354 
355  Value * const Pointer = const_cast<Value*>(MemLoc.Ptr);
356  const LocationSize Size = MemLoc.Size;
357  const AAMDNodes &AAInfo = MemLoc.AATags;
358 
359  AliasSet::PointerRec &Entry = getEntryFor(Pointer);
360 
361  if (AliasAnyAS) {
362  // At this point, the AST is saturated, so we only have one active alias
363  // set. That means we already know which alias set we want to return, and
364  // just need to add the pointer to that set to keep the data structure
365  // consistent.
366  // This, of course, means that we will never need a merge here.
367  if (Entry.hasAliasSet()) {
368  Entry.updateSizeAndAAInfo(Size, AAInfo);
369  assert(Entry.getAliasSet(*this) == AliasAnyAS &&
370  "Entry in saturated AST must belong to only alias set");
371  } else {
372  AliasAnyAS->addPointer(*this, Entry, Size, AAInfo);
373  }
374  return *AliasAnyAS;
375  }
376 
377  bool MustAliasAll = false;
378  // Check to see if the pointer is already known.
379  if (Entry.hasAliasSet()) {
380  // If the size changed, we may need to merge several alias sets.
381  // Note that we can *not* return the result of mergeAliasSetsForPointer
382  // due to a quirk of alias analysis behavior. Since alias(undef, undef)
383  // is NoAlias, mergeAliasSetsForPointer(undef, ...) will not find the
384  // the right set for undef, even if it exists.
385  if (Entry.updateSizeAndAAInfo(Size, AAInfo))
386  mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll);
387  // Return the set!
388  return *Entry.getAliasSet(*this)->getForwardedTarget(*this);
389  }
390 
391  if (AliasSet *AS =
392  mergeAliasSetsForPointer(Pointer, Size, AAInfo, MustAliasAll)) {
393  // Add it to the alias set it aliases.
394  AS->addPointer(*this, Entry, Size, AAInfo, MustAliasAll);
395  return *AS;
396  }
397 
398  // Otherwise create a new alias set to hold the loaded pointer.
399  AliasSets.push_back(new AliasSet());
400  AliasSets.back().addPointer(*this, Entry, Size, AAInfo, true);
401  return AliasSets.back();
402 }
403 
405  const AAMDNodes &AAInfo) {
406  addPointer(MemoryLocation(Ptr, Size, AAInfo), AliasSet::NoAccess);
407 }
408 
411  return addUnknown(LI);
412  addPointer(MemoryLocation::get(LI), AliasSet::RefAccess);
413 }
414 
417  return addUnknown(SI);
418  addPointer(MemoryLocation::get(SI), AliasSet::ModAccess);
419 }
420 
422  addPointer(MemoryLocation::get(VAAI), AliasSet::ModRefAccess);
423 }
424 
426  addPointer(MemoryLocation::getForDest(MSI), AliasSet::ModAccess);
427 }
428 
430  addPointer(MemoryLocation::getForDest(MTI), AliasSet::ModAccess);
431  addPointer(MemoryLocation::getForSource(MTI), AliasSet::RefAccess);
432 }
433 
435  if (isa<DbgInfoIntrinsic>(Inst))
436  return; // Ignore DbgInfo Intrinsics.
437 
438  if (auto *II = dyn_cast<IntrinsicInst>(Inst)) {
439  // These intrinsics will show up as affecting memory, but they are just
440  // markers.
441  switch (II->getIntrinsicID()) {
442  default:
443  break;
444  // FIXME: Add lifetime/invariant intrinsics (See: PR30807).
445  case Intrinsic::assume:
446  case Intrinsic::sideeffect:
447  return;
448  }
449  }
450  if (!Inst->mayReadOrWriteMemory())
451  return; // doesn't alias anything
452 
453  if (AliasSet *AS = findAliasSetForUnknownInst(Inst)) {
454  AS->addUnknownInst(Inst, AA);
455  return;
456  }
457  AliasSets.push_back(new AliasSet());
458  AliasSets.back().addUnknownInst(Inst, AA);
459 }
460 
462  // Dispatch to one of the other add methods.
463  if (LoadInst *LI = dyn_cast<LoadInst>(I))
464  return add(LI);
465  if (StoreInst *SI = dyn_cast<StoreInst>(I))
466  return add(SI);
467  if (VAArgInst *VAAI = dyn_cast<VAArgInst>(I))
468  return add(VAAI);
469  if (AnyMemSetInst *MSI = dyn_cast<AnyMemSetInst>(I))
470  return add(MSI);
471  if (AnyMemTransferInst *MTI = dyn_cast<AnyMemTransferInst>(I))
472  return add(MTI);
473 
474  // Handle all calls with known mod/ref sets genericall
475  if (auto *Call = dyn_cast<CallBase>(I))
476  if (Call->onlyAccessesArgMemory()) {
477  auto getAccessFromModRef = [](ModRefInfo MRI) {
478  if (isRefSet(MRI) && isModSet(MRI))
479  return AliasSet::ModRefAccess;
480  else if (isModSet(MRI))
481  return AliasSet::ModAccess;
482  else if (isRefSet(MRI))
483  return AliasSet::RefAccess;
484  else
485  return AliasSet::NoAccess;
486  };
487 
488  ModRefInfo CallMask = createModRefInfo(AA.getModRefBehavior(Call));
489 
490  // Some intrinsics are marked as modifying memory for control flow
491  // modelling purposes, but don't actually modify any specific memory
492  // location.
493  using namespace PatternMatch;
494  if (Call->use_empty() &&
495  match(Call, m_Intrinsic<Intrinsic::invariant_start>()))
496  CallMask = clearMod(CallMask);
497 
498  for (auto IdxArgPair : enumerate(Call->args())) {
499  int ArgIdx = IdxArgPair.index();
500  const Value *Arg = IdxArgPair.value();
501  if (!Arg->getType()->isPointerTy())
502  continue;
503  MemoryLocation ArgLoc =
504  MemoryLocation::getForArgument(Call, ArgIdx, nullptr);
505  ModRefInfo ArgMask = AA.getArgModRefInfo(Call, ArgIdx);
506  ArgMask = intersectModRef(CallMask, ArgMask);
507  if (!isNoModRef(ArgMask))
508  addPointer(ArgLoc, getAccessFromModRef(ArgMask));
509  }
510  return;
511  }
512 
513  return addUnknown(I);
514 }
515 
517  for (auto &I : BB)
518  add(&I);
519 }
520 
522  assert(&AA == &AST.AA &&
523  "Merging AliasSetTracker objects with different Alias Analyses!");
524 
525  // Loop over all of the alias sets in AST, adding the pointers contained
526  // therein into the current alias sets. This can cause alias sets to be
527  // merged together in the current AST.
528  for (const AliasSet &AS : AST) {
529  if (AS.Forward)
530  continue; // Ignore forwarding alias sets
531 
532  // If there are any call sites in the alias set, add them to this AST.
533  for (unsigned i = 0, e = AS.UnknownInsts.size(); i != e; ++i)
534  if (auto *Inst = AS.getUnknownInst(i))
535  add(Inst);
536 
537  // Loop over all of the pointers in this alias set.
538  for (AliasSet::iterator ASI = AS.begin(), E = AS.end(); ASI != E; ++ASI)
539  addPointer(
540  MemoryLocation(ASI.getPointer(), ASI.getSize(), ASI.getAAInfo()),
541  (AliasSet::AccessLattice)AS.Access);
542  }
543 }
544 
546  assert(MSSA && L && "MSSA and L must be available");
547  for (const BasicBlock *BB : L->blocks())
548  if (auto *Accesses = MSSA->getBlockAccesses(BB))
549  for (auto &Access : *Accesses)
550  if (auto *MUD = dyn_cast<MemoryUseOrDef>(&Access))
551  add(MUD->getMemoryInst());
552 }
553 
554 // deleteValue method - This method is used to remove a pointer value from the
555 // AliasSetTracker entirely. It should be used when an instruction is deleted
556 // from the program to update the AST. If you don't use this, you would have
557 // dangling pointers to deleted instructions.
558 //
560  // First, look up the PointerRec for this pointer.
561  PointerMapType::iterator I = PointerMap.find_as(PtrVal);
562  if (I == PointerMap.end()) return; // Noop
563 
564  // If we found one, remove the pointer from the alias set it is in.
565  AliasSet::PointerRec *PtrValEnt = I->second;
566  AliasSet *AS = PtrValEnt->getAliasSet(*this);
567 
568  // Unlink and delete from the list of values.
569  PtrValEnt->eraseFromList();
570 
571  if (AS->Alias == AliasSet::SetMayAlias) {
572  AS->SetSize--;
573  TotalMayAliasSetSize--;
574  }
575 
576  // Stop using the alias set.
577  AS->dropRef(*this);
578 
579  PointerMap.erase(I);
580 }
581 
582 // copyValue - This method should be used whenever a preexisting value in the
583 // program is copied or cloned, introducing a new value. Note that it is ok for
584 // clients that use this method to introduce the same value multiple times: if
585 // the tracker already knows about a value, it will ignore the request.
586 //
588  // First, look up the PointerRec for this pointer.
589  PointerMapType::iterator I = PointerMap.find_as(From);
590  if (I == PointerMap.end())
591  return; // Noop
592  assert(I->second->hasAliasSet() && "Dead entry?");
593 
594  AliasSet::PointerRec &Entry = getEntryFor(To);
595  if (Entry.hasAliasSet()) return; // Already in the tracker!
596 
597  // getEntryFor above may invalidate iterator \c I, so reinitialize it.
598  I = PointerMap.find_as(From);
599  // Add it to the alias set it aliases...
600  AliasSet *AS = I->second->getAliasSet(*this);
601  AS->addPointer(*this, Entry, I->second->getSize(), I->second->getAAInfo(),
602  true, true);
603 }
604 
605 AliasSet &AliasSetTracker::mergeAllAliasSets() {
606  assert(!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold) &&
607  "Full merge should happen once, when the saturation threshold is "
608  "reached");
609 
610  // Collect all alias sets, so that we can drop references with impunity
611  // without worrying about iterator invalidation.
612  std::vector<AliasSet *> ASVector;
613  ASVector.reserve(SaturationThreshold);
614  for (iterator I = begin(), E = end(); I != E; I++)
615  ASVector.push_back(&*I);
616 
617  // Copy all instructions and pointers into a new set, and forward all other
618  // sets to it.
619  AliasSets.push_back(new AliasSet());
620  AliasAnyAS = &AliasSets.back();
621  AliasAnyAS->Alias = AliasSet::SetMayAlias;
622  AliasAnyAS->Access = AliasSet::ModRefAccess;
623  AliasAnyAS->AliasAny = true;
624 
625  for (auto Cur : ASVector) {
626  // If Cur was already forwarding, just forward to the new AS instead.
627  AliasSet *FwdTo = Cur->Forward;
628  if (FwdTo) {
629  Cur->Forward = AliasAnyAS;
630  AliasAnyAS->addRef();
631  FwdTo->dropRef(*this);
632  continue;
633  }
634 
635  // Otherwise, perform the actual merge.
636  AliasAnyAS->mergeSetIn(*Cur, *this);
637  }
638 
639  return *AliasAnyAS;
640 }
641 
642 AliasSet &AliasSetTracker::addPointer(MemoryLocation Loc,
643  AliasSet::AccessLattice E) {
644  AliasSet &AS = getAliasSetFor(Loc);
645  AS.Access |= E;
646 
647  if (!AliasAnyAS && (TotalMayAliasSetSize > SaturationThreshold)) {
648  // The AST is now saturated. From here on, we conservatively consider all
649  // pointers to alias each-other.
650  return mergeAllAliasSets();
651  }
652 
653  return AS;
654 }
655 
656 //===----------------------------------------------------------------------===//
657 // AliasSet/AliasSetTracker Printing Support
658 //===----------------------------------------------------------------------===//
659 
660 void AliasSet::print(raw_ostream &OS) const {
661  OS << " AliasSet[" << (const void*)this << ", " << RefCount << "] ";
662  OS << (Alias == SetMustAlias ? "must" : "may") << " alias, ";
663  switch (Access) {
664  case NoAccess: OS << "No access "; break;
665  case RefAccess: OS << "Ref "; break;
666  case ModAccess: OS << "Mod "; break;
667  case ModRefAccess: OS << "Mod/Ref "; break;
668  default: llvm_unreachable("Bad value for Access!");
669  }
670  if (Forward)
671  OS << " forwarding to " << (void*)Forward;
672 
673  if (!empty()) {
674  OS << "Pointers: ";
675  for (iterator I = begin(), E = end(); I != E; ++I) {
676  if (I != begin()) OS << ", ";
677  I.getPointer()->printAsOperand(OS << "(");
678  if (I.getSize() == LocationSize::unknown())
679  OS << ", unknown)";
680  else
681  OS << ", " << I.getSize() << ")";
682  }
683  }
684  if (!UnknownInsts.empty()) {
685  OS << "\n " << UnknownInsts.size() << " Unknown instructions: ";
686  for (unsigned i = 0, e = UnknownInsts.size(); i != e; ++i) {
687  if (i) OS << ", ";
688  if (auto *I = getUnknownInst(i)) {
689  if (I->hasName())
690  I->printAsOperand(OS);
691  else
692  I->print(OS);
693  }
694  }
695  }
696  OS << "\n";
697 }
698 
700  OS << "Alias Set Tracker: " << AliasSets.size();
701  if (AliasAnyAS)
702  OS << " (Saturated)";
703  OS << " alias sets for " << PointerMap.size() << " pointer values.\n";
704  for (const AliasSet &AS : *this)
705  AS.print(OS);
706  OS << "\n";
707 }
708 
709 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
712 #endif
713 
714 //===----------------------------------------------------------------------===//
715 // ASTCallbackVH Class Implementation
716 //===----------------------------------------------------------------------===//
717 
718 void AliasSetTracker::ASTCallbackVH::deleted() {
719  assert(AST && "ASTCallbackVH called with a null AliasSetTracker!");
720  AST->deleteValue(getValPtr());
721  // this now dangles!
722 }
723 
724 void AliasSetTracker::ASTCallbackVH::allUsesReplacedWith(Value *V) {
725  AST->copyValue(getValPtr(), V);
726 }
727 
728 AliasSetTracker::ASTCallbackVH::ASTCallbackVH(Value *V, AliasSetTracker *ast)
729  : CallbackVH(V), AST(ast) {}
730 
731 AliasSetTracker::ASTCallbackVH &
732 AliasSetTracker::ASTCallbackVH::operator=(Value *V) {
733  return *this = ASTCallbackVH(V, AST);
734 }
735 
736 //===----------------------------------------------------------------------===//
737 // AliasSetPrinter Pass
738 //===----------------------------------------------------------------------===//
739 
740 namespace {
741 
742  class AliasSetPrinter : public FunctionPass {
743  AliasSetTracker *Tracker;
744 
745  public:
746  static char ID; // Pass identification, replacement for typeid
747 
748  AliasSetPrinter() : FunctionPass(ID) {
750  }
751 
752  void getAnalysisUsage(AnalysisUsage &AU) const override {
753  AU.setPreservesAll();
755  }
756 
757  bool runOnFunction(Function &F) override {
758  auto &AAWP = getAnalysis<AAResultsWrapperPass>();
759  Tracker = new AliasSetTracker(AAWP.getAAResults());
760  errs() << "Alias sets for function '" << F.getName() << "':\n";
761  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
762  Tracker->add(&*I);
763  Tracker->print(errs());
764  delete Tracker;
765  return false;
766  }
767  };
768 
769 } // end anonymous namespace
770 
771 char AliasSetPrinter::ID = 0;
772 
773 INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets",
774  "Alias Set Printer", false, true)
776 INITIALIZE_PASS_END(AliasSetPrinter, "print-alias-sets",
777  "Alias Set Printer", false, true)
void mergeSetIn(AliasSet &AS, AliasSetTracker &AST)
Merge the specified alias set into this alias set.
INITIALIZE_PASS_BEGIN(AliasSetPrinter, "print-alias-sets", "Alias Set Printer", false, true) INITIALIZE_PASS_END(AliasSetPrinter
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Atomic ordering constants.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
Define an iterator for alias sets... this is just a forward iterator.
bool user_empty() const
Definition: Value.h:384
void dump() const
static constexpr LocationSize unknown()
void add(Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo)
These methods are used to add different types of instructions to the alias sets.
static cl::opt< unsigned > SaturationThreshold("alias-set-saturation-threshold", cl::Hidden, cl::init(250), cl::desc("The maximum number of pointers may-alias " "sets may contain before degradation"))
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Definition: Instructions.h:258
print alias sets
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1105
F(f)
block Block Frequency true
An instruction for reading from memory.
Definition: Instructions.h:169
print alias Alias Set Printer
LLVM_NODISCARD ModRefInfo clearMod(const ModRefInfo MRI)
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
inst_iterator inst_begin(Function *F)
Definition: InstIterator.h:131
bool isMustAlias() const
void initializeAliasSetPrinterPass(PassRegistry &)
static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
FunctionModRefBehavior getModRefBehavior(const CallBase *Call)
Return the behavior of the given call site.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
friend class AliasSetTracker
iterator begin() const
bool aliasesUnknownInst(const Instruction *Inst, AliasAnalysis &AA) const
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Instruction * getUniqueInstruction()
If this alias set is known to contain a single instruction and only a single unique instruction...
An instruction for storing to memory.
Definition: Instructions.h:331
bool isStrongerThanMonotonic(AtomicOrdering ao)
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:78
static bool runOnFunction(Function &F, bool PostInlining)
#define P(N)
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:435
void print(raw_ostream &OS) const
unsigned const MachineRegisterInfo * MRI
bool hasName() const
Definition: Value.h:252
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
AliasAnalysis & getAliasAnalysis() const
Return the underlying alias analysis object used by this tracker.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:224
Represent the analysis usage information of a pass.
AliasSet & getAliasSetFor(const MemoryLocation &MemLoc)
Return the alias set which contains the specified memory location.
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4316
constexpr double e
Definition: MathExtras.h:57
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:282
This class represents any memset intrinsic.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4393
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:86
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
The two locations precisely alias each other.
Definition: AliasAnalysis.h:90
AliasResult aliasesPointer(const Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo, AliasAnalysis &AA) const
If the specified pointer "may" (or must) alias one of the members in the set return the appropriate A...
Iterator for intrusive lists based on ilist_node.
BlockVerifier::State From
void print(raw_ostream &OS) const
void copyValue(Value *From, Value *To)
This method should be used whenever a preexisting value in the program is copied or cloned...
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Definition: FileCheck.cpp:208
Module.h This file contains the declarations for the Module class.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental.guard intrinsic.
Definition: GuardUtils.cpp:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM_NODISCARD bool isNoModRef(const ModRefInfo MRI)
iterator end() const
LLVM_NODISCARD bool isModSet(const ModRefInfo MRI)
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
AtomicOrdering getOrdering() const
Returns the ordering constraint of this store instruction.
Definition: Instructions.h:389
This file provides utility analysis objects describing memory locations.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:215
LLVM_NODISCARD ModRefInfo intersectModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the ModRef info associated with a pointer argument of a call.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:396
LLVM Value Representation.
Definition: Value.h:74
LLVM_NODISCARD ModRefInfo createModRefInfo(const FunctionModRefBehavior FMRB)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
bool empty() const
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:399
inst_iterator inst_end(Function *F)
Definition: InstIterator.h:132
LLVM_NODISCARD bool isModOrRefSet(const ModRefInfo MRI)
void addUnknown(Instruction *I)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
bool use_empty() const
Definition: Value.h:343
void deleteValue(Value *PtrVal)
This method is used to remove a pointer value from the AliasSetTracker entirely.
detail::enumerator< R > enumerate(R &&TheRange)
Given an input range, returns a new range whose values are are pair (A,B) such that A is the 0-based ...
Definition: STLExtras.h:1511
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
user_iterator user_end()
Definition: Value.h:404
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
Definition: Instruction.h:542
LLVM_NODISCARD bool isRefSet(const ModRefInfo MRI)