#ifndef LLVM_ANALYSIS_ALIASSETTRACKER_H
#define LLVM_ANALYSIS_ALIASSETTRACKER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include <cassert>
#include <cstddef>
#include <iterator>
#include <vector>
namespace llvm {
class AAResults;
class AliasResult;
class AliasSetTracker;
class AnyMemSetInst;
class AnyMemTransferInst;
class BasicBlock;
class LoadInst;
class raw_ostream;
class StoreInst;
class VAArgInst;
class Value;
class AliasSet : public ilist_node<AliasSet> {
friend class AliasSetTracker;
class PointerRec {
Value *Val; PointerRec **PrevInList = nullptr;
PointerRec *NextInList = nullptr;
AliasSet *AS = nullptr;
LocationSize Size = LocationSize::mapEmpty();
AAMDNodes AAInfo;
bool isSizeSet() const { return Size != LocationSize::mapEmpty(); }
public:
PointerRec(Value *V)
: Val(V), AAInfo(DenseMapInfo<AAMDNodes>::getEmptyKey()) {}
Value *getValue() const { return Val; }
PointerRec *getNext() const { return NextInList; }
bool hasAliasSet() const { return AS != nullptr; }
PointerRec** setPrevInList(PointerRec **PIL) {
PrevInList = PIL;
return &NextInList;
}
bool updateSizeAndAAInfo(LocationSize NewSize, const AAMDNodes &NewAAInfo) {
bool SizeChanged = false;
if (NewSize != Size) {
LocationSize OldSize = Size;
Size = isSizeSet() ? Size.unionWith(NewSize) : NewSize;
SizeChanged = OldSize != Size;
}
if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey())
AAInfo = NewAAInfo;
else {
AAMDNodes Intersection(AAInfo.intersect(NewAAInfo));
SizeChanged |= Intersection != AAInfo;
AAInfo = Intersection;
}
return SizeChanged;
}
LocationSize getSize() const {
assert(isSizeSet() && "Getting an unset size!");
return Size;
}
AAMDNodes getAAInfo() const {
if (AAInfo == DenseMapInfo<AAMDNodes>::getEmptyKey() ||
AAInfo == DenseMapInfo<AAMDNodes>::getTombstoneKey())
return AAMDNodes();
return AAInfo;
}
AliasSet *getAliasSet(AliasSetTracker &AST) {
assert(AS && "No AliasSet yet!");
if (AS->Forward) {
AliasSet *OldAS = AS;
AS = OldAS->getForwardedTarget(AST);
AS->addRef();
OldAS->dropRef(AST);
}
return AS;
}
void setAliasSet(AliasSet *as) {
assert(!AS && "Already have an alias set!");
AS = as;
}
void eraseFromList() {
if (NextInList) NextInList->PrevInList = PrevInList;
*PrevInList = NextInList;
if (AS->PtrListEnd == &NextInList) {
AS->PtrListEnd = PrevInList;
assert(*AS->PtrListEnd == nullptr && "List not terminated right!");
}
delete this;
}
};
PointerRec *PtrList = nullptr;
PointerRec **PtrListEnd;
AliasSet *Forward = nullptr;
std::vector<WeakVH> UnknownInsts;
unsigned RefCount : 27;
unsigned AliasAny : 1;
enum AccessLattice {
NoAccess = 0,
RefAccess = 1,
ModAccess = 2,
ModRefAccess = RefAccess | ModAccess
};
unsigned Access : 2;
enum AliasLattice {
SetMustAlias = 0, SetMayAlias = 1
};
unsigned Alias : 1;
unsigned SetSize = 0;
void addRef() { ++RefCount; }
void dropRef(AliasSetTracker &AST) {
assert(RefCount >= 1 && "Invalid reference count detected!");
if (--RefCount == 0)
removeFromTracker(AST);
}
Instruction *getUnknownInst(unsigned i) const {
assert(i < UnknownInsts.size());
return cast_or_null<Instruction>(UnknownInsts[i]);
}
public:
AliasSet(const AliasSet &) = delete;
AliasSet &operator=(const AliasSet &) = delete;
bool isRef() const { return Access & RefAccess; }
bool isMod() const { return Access & ModAccess; }
bool isMustAlias() const { return Alias == SetMustAlias; }
bool isMayAlias() const { return Alias == SetMayAlias; }
bool isForwardingAliasSet() const { return Forward; }
void mergeSetIn(AliasSet &AS, AliasSetTracker &AST);
class iterator;
iterator begin() const { return iterator(PtrList); }
iterator end() const { return iterator(); }
bool empty() const { return PtrList == nullptr; }
unsigned size() { return SetSize; }
void print(raw_ostream &OS) const;
void dump() const;
class iterator {
PointerRec *CurNode;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = PointerRec;
using difference_type = std::ptrdiff_t;
using pointer = value_type *;
using reference = value_type &;
explicit iterator(PointerRec *CN = nullptr) : CurNode(CN) {}
bool operator==(const iterator& x) const {
return CurNode == x.CurNode;
}
bool operator!=(const iterator& x) const { return !operator==(x); }
value_type &operator*() const {
assert(CurNode && "Dereferencing AliasSet.end()!");
return *CurNode;
}
value_type *operator->() const { return &operator*(); }
Value *getPointer() const { return CurNode->getValue(); }
LocationSize getSize() const { return CurNode->getSize(); }
AAMDNodes getAAInfo() const { return CurNode->getAAInfo(); }
iterator& operator++() { assert(CurNode && "Advancing past AliasSet.end()!");
CurNode = CurNode->getNext();
return *this;
}
iterator operator++(int) { iterator tmp = *this; ++*this; return tmp;
}
};
private:
AliasSet()
: PtrListEnd(&PtrList), RefCount(0), AliasAny(false), Access(NoAccess),
Alias(SetMustAlias) {}
PointerRec *getSomePointer() const {
return PtrList;
}
AliasSet *getForwardedTarget(AliasSetTracker &AST) {
if (!Forward) return this;
AliasSet *Dest = Forward->getForwardedTarget(AST);
if (Dest != Forward) {
Dest->addRef();
Forward->dropRef(AST);
Forward = Dest;
}
return Dest;
}
void removeFromTracker(AliasSetTracker &AST);
void addPointer(AliasSetTracker &AST, PointerRec &Entry, LocationSize Size,
const AAMDNodes &AAInfo, bool KnownMustAlias = false,
bool SkipSizeUpdate = false);
void addUnknownInst(Instruction *I, AAResults &AA);
void removeUnknownInst(AliasSetTracker &AST, Instruction *I) {
bool WasEmpty = UnknownInsts.empty();
for (size_t i = 0, e = UnknownInsts.size(); i != e; ++i)
if (UnknownInsts[i] == I) {
UnknownInsts[i] = UnknownInsts.back();
UnknownInsts.pop_back();
--i; --e; }
if (!WasEmpty && UnknownInsts.empty())
dropRef(AST);
}
public:
AliasResult aliasesPointer(const Value *Ptr, LocationSize Size,
const AAMDNodes &AAInfo, AAResults &AA) const;
bool aliasesUnknownInst(const Instruction *Inst, AAResults &AA) const;
};
inline raw_ostream& operator<<(raw_ostream &OS, const AliasSet &AS) {
AS.print(OS);
return OS;
}
class AliasSetTracker {
class ASTCallbackVH final : public CallbackVH {
AliasSetTracker *AST;
void deleted() override;
void allUsesReplacedWith(Value *) override;
public:
ASTCallbackVH(Value *V, AliasSetTracker *AST = nullptr);
ASTCallbackVH &operator=(Value *V);
};
struct ASTCallbackVHDenseMapInfo : public DenseMapInfo<Value *> {};
AAResults &AA;
ilist<AliasSet> AliasSets;
using PointerMapType = DenseMap<ASTCallbackVH, AliasSet::PointerRec *,
ASTCallbackVHDenseMapInfo>;
PointerMapType PointerMap;
public:
explicit AliasSetTracker(AAResults &AA) : AA(AA) {}
~AliasSetTracker() { clear(); }
void add(Value *Ptr, LocationSize Size, const AAMDNodes &AAInfo); void add(LoadInst *LI);
void add(StoreInst *SI);
void add(VAArgInst *VAAI);
void add(AnyMemSetInst *MSI);
void add(AnyMemTransferInst *MTI);
void add(Instruction *I); void add(BasicBlock &BB); void add(const AliasSetTracker &AST); void addUnknown(Instruction *I);
void clear();
const ilist<AliasSet> &getAliasSets() const { return AliasSets; }
AliasSet &getAliasSetFor(const MemoryLocation &MemLoc);
AAResults &getAliasAnalysis() const { return AA; }
void deleteValue(Value *PtrVal);
void copyValue(Value *From, Value *To);
using iterator = ilist<AliasSet>::iterator;
using const_iterator = ilist<AliasSet>::const_iterator;
const_iterator begin() const { return AliasSets.begin(); }
const_iterator end() const { return AliasSets.end(); }
iterator begin() { return AliasSets.begin(); }
iterator end() { return AliasSets.end(); }
void print(raw_ostream &OS) const;
void dump() const;
private:
friend class AliasSet;
unsigned TotalMayAliasSetSize = 0;
AliasSet *AliasAnyAS = nullptr;
void removeAliasSet(AliasSet *AS);
AliasSet::PointerRec &getEntryFor(Value *V) {
AliasSet::PointerRec *&Entry = PointerMap[ASTCallbackVH(V, this)];
if (!Entry)
Entry = new AliasSet::PointerRec(V);
return *Entry;
}
AliasSet &addPointer(MemoryLocation Loc, AliasSet::AccessLattice E);
AliasSet *mergeAliasSetsForPointer(const Value *Ptr, LocationSize Size,
const AAMDNodes &AAInfo,
bool &MustAliasAll);
AliasSet &mergeAllAliasSets();
AliasSet *findAliasSetForUnknownInst(Instruction *Inst);
};
inline raw_ostream& operator<<(raw_ostream &OS, const AliasSetTracker &AST) {
AST.print(OS);
return OS;
}
class AliasSetsPrinterPass : public PassInfoMixin<AliasSetsPrinterPass> {
raw_ostream &OS;
public:
explicit AliasSetsPrinterPass(raw_ostream &OS);
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
}
#endif