#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/MemoryLocation.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <cstdint>
using namespace llvm;
static cl::opt<bool> EnableTBAA("enable-tbaa", cl::init(true), cl::Hidden);
namespace {
static bool isNewFormatTypeNode(const MDNode *N) {
if (N->getNumOperands() < 3)
return false;
if (!isa<MDNode>(N->getOperand(0)))
return false;
return true;
}
template<typename MDNodeTy>
class TBAANodeImpl {
MDNodeTy *Node = nullptr;
public:
TBAANodeImpl() = default;
explicit TBAANodeImpl(MDNodeTy *N) : Node(N) {}
MDNodeTy *getNode() const { return Node; }
bool isNewFormat() const { return isNewFormatTypeNode(Node); }
TBAANodeImpl<MDNodeTy> getParent() const {
if (isNewFormat())
return TBAANodeImpl(cast<MDNodeTy>(Node->getOperand(0)));
if (Node->getNumOperands() < 2)
return TBAANodeImpl<MDNodeTy>();
MDNodeTy *P = dyn_cast_or_null<MDNodeTy>(Node->getOperand(1));
if (!P)
return TBAANodeImpl<MDNodeTy>();
return TBAANodeImpl<MDNodeTy>(P);
}
bool isTypeImmutable() const {
if (Node->getNumOperands() < 3)
return false;
ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(2));
if (!CI)
return false;
return CI->getValue()[0];
}
};
using TBAANode = TBAANodeImpl<const MDNode>;
using MutableTBAANode = TBAANodeImpl<MDNode>;
template<typename MDNodeTy>
class TBAAStructTagNodeImpl {
MDNodeTy *Node;
public:
explicit TBAAStructTagNodeImpl(MDNodeTy *N) : Node(N) {}
MDNodeTy *getNode() const { return Node; }
bool isNewFormat() const {
if (Node->getNumOperands() < 4)
return false;
if (MDNodeTy *AccessType = getAccessType())
if (!TBAANodeImpl<MDNodeTy>(AccessType).isNewFormat())
return false;
return true;
}
MDNodeTy *getBaseType() const {
return dyn_cast_or_null<MDNode>(Node->getOperand(0));
}
MDNodeTy *getAccessType() const {
return dyn_cast_or_null<MDNode>(Node->getOperand(1));
}
uint64_t getOffset() const {
return mdconst::extract<ConstantInt>(Node->getOperand(2))->getZExtValue();
}
uint64_t getSize() const {
if (!isNewFormat())
return UINT64_MAX;
return mdconst::extract<ConstantInt>(Node->getOperand(3))->getZExtValue();
}
bool isTypeImmutable() const {
unsigned OpNo = isNewFormat() ? 4 : 3;
if (Node->getNumOperands() < OpNo + 1)
return false;
ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpNo));
if (!CI)
return false;
return CI->getValue()[0];
}
};
using TBAAStructTagNode = TBAAStructTagNodeImpl<const MDNode>;
using MutableTBAAStructTagNode = TBAAStructTagNodeImpl<MDNode>;
class TBAAStructTypeNode {
const MDNode *Node = nullptr;
public:
TBAAStructTypeNode() = default;
explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
const MDNode *getNode() const { return Node; }
bool isNewFormat() const { return isNewFormatTypeNode(Node); }
bool operator==(const TBAAStructTypeNode &Other) const {
return getNode() == Other.getNode();
}
Metadata *getId() const {
return Node->getOperand(isNewFormat() ? 2 : 0);
}
unsigned getNumFields() const {
unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
return (getNode()->getNumOperands() - FirstFieldOpNo) / NumOpsPerField;
}
TBAAStructTypeNode getFieldType(unsigned FieldIndex) const {
unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
unsigned OpIndex = FirstFieldOpNo + FieldIndex * NumOpsPerField;
auto *TypeNode = cast<MDNode>(getNode()->getOperand(OpIndex));
return TBAAStructTypeNode(TypeNode);
}
TBAAStructTypeNode getField(uint64_t &Offset) const {
bool NewFormat = isNewFormat();
const ArrayRef<MDOperand> Operands = Node->operands();
const unsigned NumOperands = Operands.size();
if (NewFormat) {
if (NumOperands < 6)
return TBAAStructTypeNode();
} else {
if (NumOperands < 2)
return TBAAStructTypeNode();
if (NumOperands <= 3) {
uint64_t Cur =
NumOperands == 2
? 0
: mdconst::extract<ConstantInt>(Operands[2])->getZExtValue();
Offset -= Cur;
MDNode *P = dyn_cast_or_null<MDNode>(Operands[1]);
if (!P)
return TBAAStructTypeNode();
return TBAAStructTypeNode(P);
}
}
unsigned FirstFieldOpNo = NewFormat ? 3 : 1;
unsigned NumOpsPerField = NewFormat ? 3 : 2;
unsigned TheIdx = 0;
for (unsigned Idx = FirstFieldOpNo; Idx < NumOperands;
Idx += NumOpsPerField) {
uint64_t Cur =
mdconst::extract<ConstantInt>(Operands[Idx + 1])->getZExtValue();
if (Cur > Offset) {
assert(Idx >= FirstFieldOpNo + NumOpsPerField &&
"TBAAStructTypeNode::getField should have an offset match!");
TheIdx = Idx - NumOpsPerField;
break;
}
}
if (TheIdx == 0)
TheIdx = NumOperands - NumOpsPerField;
uint64_t Cur =
mdconst::extract<ConstantInt>(Operands[TheIdx + 1])->getZExtValue();
Offset -= Cur;
MDNode *P = dyn_cast_or_null<MDNode>(Operands[TheIdx]);
if (!P)
return TBAAStructTypeNode();
return TBAAStructTypeNode(P);
}
};
}
static bool isStructPathTBAA(const MDNode *MD) {
return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
}
AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
const MemoryLocation &LocB,
AAQueryInfo &AAQI) {
if (!EnableTBAA)
return AAResultBase::alias(LocA, LocB, AAQI);
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
return AAResultBase::alias(LocA, LocB, AAQI);
return AliasResult::NoAlias;
}
bool TypeBasedAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
AAQueryInfo &AAQI,
bool OrLocal) {
if (!EnableTBAA)
return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
const MDNode *M = Loc.AATags.TBAA;
if (!M)
return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
if ((!isStructPathTBAA(M) && TBAANode(M).isTypeImmutable()) ||
(isStructPathTBAA(M) && TBAAStructTagNode(M).isTypeImmutable()))
return true;
return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
}
FunctionModRefBehavior
TypeBasedAAResult::getModRefBehavior(const CallBase *Call) {
if (!EnableTBAA)
return AAResultBase::getModRefBehavior(Call);
FunctionModRefBehavior Min = FMRB_UnknownModRefBehavior;
if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
if ((!isStructPathTBAA(M) && TBAANode(M).isTypeImmutable()) ||
(isStructPathTBAA(M) && TBAAStructTagNode(M).isTypeImmutable()))
Min = FMRB_OnlyReadsMemory;
return FunctionModRefBehavior(AAResultBase::getModRefBehavior(Call) & Min);
}
FunctionModRefBehavior TypeBasedAAResult::getModRefBehavior(const Function *F) {
return AAResultBase::getModRefBehavior(F);
}
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
const MemoryLocation &Loc,
AAQueryInfo &AAQI) {
if (!EnableTBAA)
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
if (const MDNode *L = Loc.AATags.TBAA)
if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
if (!Aliases(L, M))
return ModRefInfo::NoModRef;
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
}
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
const CallBase *Call2,
AAQueryInfo &AAQI) {
if (!EnableTBAA)
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
if (const MDNode *M2 = Call2->getMetadata(LLVMContext::MD_tbaa))
if (!Aliases(M1, M2))
return ModRefInfo::NoModRef;
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
}
bool MDNode::isTBAAVtableAccess() const {
if (!isStructPathTBAA(this)) {
if (getNumOperands() < 1)
return false;
if (MDString *Tag1 = dyn_cast<MDString>(getOperand(0))) {
if (Tag1->getString() == "vtable pointer")
return true;
}
return false;
}
TBAAStructTagNode Tag(this);
TBAAStructTypeNode AccessType(Tag.getAccessType());
if(auto *Id = dyn_cast<MDString>(AccessType.getId()))
if (Id->getString() == "vtable pointer")
return true;
return false;
}
static bool matchAccessTags(const MDNode *A, const MDNode *B,
const MDNode **GenericTag = nullptr);
MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
const MDNode *GenericTag;
matchAccessTags(A, B, &GenericTag);
return const_cast<MDNode*>(GenericTag);
}
static const MDNode *getLeastCommonType(const MDNode *A, const MDNode *B) {
if (!A || !B)
return nullptr;
if (A == B)
return A;
SmallSetVector<const MDNode *, 4> PathA;
TBAANode TA(A);
while (TA.getNode()) {
if (PathA.count(TA.getNode()))
report_fatal_error("Cycle found in TBAA metadata.");
PathA.insert(TA.getNode());
TA = TA.getParent();
}
SmallSetVector<const MDNode *, 4> PathB;
TBAANode TB(B);
while (TB.getNode()) {
if (PathB.count(TB.getNode()))
report_fatal_error("Cycle found in TBAA metadata.");
PathB.insert(TB.getNode());
TB = TB.getParent();
}
int IA = PathA.size() - 1;
int IB = PathB.size() - 1;
const MDNode *Ret = nullptr;
while (IA >= 0 && IB >= 0) {
if (PathA[IA] == PathB[IB])
Ret = PathA[IA];
else
break;
--IA;
--IB;
}
return Ret;
}
AAMDNodes AAMDNodes::merge(const AAMDNodes &Other) const {
AAMDNodes Result;
Result.TBAA = MDNode::getMostGenericTBAA(TBAA, Other.TBAA);
Result.TBAAStruct = nullptr;
Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
return Result;
}
AAMDNodes AAMDNodes::concat(const AAMDNodes &Other) const {
AAMDNodes Result;
Result.TBAA = Result.TBAAStruct = nullptr;
Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
return Result;
}
static const MDNode *createAccessTag(const MDNode *AccessType) {
if (!AccessType || AccessType->getNumOperands() < 2)
return nullptr;
Type *Int64 = IntegerType::get(AccessType->getContext(), 64);
auto *OffsetNode = ConstantAsMetadata::get(ConstantInt::get(Int64, 0));
if (TBAAStructTypeNode(AccessType).isNewFormat()) {
uint64_t AccessSize = UINT64_MAX;
auto *SizeNode =
ConstantAsMetadata::get(ConstantInt::get(Int64, AccessSize));
Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
const_cast<MDNode*>(AccessType),
OffsetNode, SizeNode};
return MDNode::get(AccessType->getContext(), Ops);
}
Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
const_cast<MDNode*>(AccessType),
OffsetNode};
return MDNode::get(AccessType->getContext(), Ops);
}
static bool hasField(TBAAStructTypeNode BaseType,
TBAAStructTypeNode FieldType) {
for (unsigned I = 0, E = BaseType.getNumFields(); I != E; ++I) {
TBAAStructTypeNode T = BaseType.getFieldType(I);
if (T == FieldType || hasField(T, FieldType))
return true;
}
return false;
}
static bool mayBeAccessToSubobjectOf(TBAAStructTagNode BaseTag,
TBAAStructTagNode SubobjectTag,
const MDNode *CommonType,
const MDNode **GenericTag,
bool &MayAlias) {
if (BaseTag.getAccessType() == BaseTag.getBaseType() &&
BaseTag.getAccessType() == CommonType) {
if (GenericTag)
*GenericTag = createAccessTag(CommonType);
MayAlias = true;
return true;
}
bool NewFormat = BaseTag.isNewFormat();
TBAAStructTypeNode BaseType(BaseTag.getBaseType());
uint64_t OffsetInBase = BaseTag.getOffset();
for (;;) {
if (!BaseType.getNode()) {
assert(!NewFormat && "Did not see access type in access path!");
break;
}
if (BaseType.getNode() == SubobjectTag.getBaseType()) {
bool SameMemberAccess = OffsetInBase == SubobjectTag.getOffset();
if (GenericTag) {
*GenericTag = SameMemberAccess ? SubobjectTag.getNode() :
createAccessTag(CommonType);
}
MayAlias = SameMemberAccess;
return true;
}
if (NewFormat && BaseType.getNode() == BaseTag.getAccessType())
break;
BaseType = BaseType.getField(OffsetInBase);
}
if (NewFormat) {
TBAAStructTypeNode FieldType(SubobjectTag.getBaseType());
if (hasField(BaseType, FieldType)) {
if (GenericTag)
*GenericTag = createAccessTag(CommonType);
MayAlias = true;
return true;
}
}
return false;
}
static bool matchAccessTags(const MDNode *A, const MDNode *B,
const MDNode **GenericTag) {
if (A == B) {
if (GenericTag)
*GenericTag = A;
return true;
}
if (!A || !B) {
if (GenericTag)
*GenericTag = nullptr;
return true;
}
assert(isStructPathTBAA(A) && "Access A is not struct-path aware!");
assert(isStructPathTBAA(B) && "Access B is not struct-path aware!");
TBAAStructTagNode TagA(A), TagB(B);
const MDNode *CommonType = getLeastCommonType(TagA.getAccessType(),
TagB.getAccessType());
if (!CommonType) {
if (GenericTag)
*GenericTag = nullptr;
return true;
}
bool MayAlias;
if (mayBeAccessToSubobjectOf( TagA, TagB,
CommonType, GenericTag, MayAlias) ||
mayBeAccessToSubobjectOf( TagB, TagA,
CommonType, GenericTag, MayAlias))
return MayAlias;
if (GenericTag)
*GenericTag = createAccessTag(CommonType);
return false;
}
bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
return matchAccessTags(A, B);
}
AnalysisKey TypeBasedAA::Key;
TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) {
return TypeBasedAAResult();
}
char TypeBasedAAWrapperPass::ID = 0;
INITIALIZE_PASS(TypeBasedAAWrapperPass, "tbaa", "Type-Based Alias Analysis",
false, true)
ImmutablePass *llvm::createTypeBasedAAWrapperPass() {
return new TypeBasedAAWrapperPass();
}
TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) {
initializeTypeBasedAAWrapperPassPass(*PassRegistry::getPassRegistry());
}
bool TypeBasedAAWrapperPass::doInitialization(Module &M) {
Result.reset(new TypeBasedAAResult());
return false;
}
bool TypeBasedAAWrapperPass::doFinalization(Module &M) {
Result.reset();
return false;
}
void TypeBasedAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}
MDNode *AAMDNodes::shiftTBAA(MDNode *MD, size_t Offset) {
if (Offset == 0)
return MD;
if (!isStructPathTBAA(MD))
return MD;
return MD;
}
MDNode *AAMDNodes::shiftTBAAStruct(MDNode *MD, size_t Offset) {
if (Offset == 0)
return MD;
SmallVector<Metadata *, 3> Sub;
for (size_t i = 0, size = MD->getNumOperands(); i < size; i += 3) {
ConstantInt *InnerOffset = mdconst::extract<ConstantInt>(MD->getOperand(i));
ConstantInt *InnerSize =
mdconst::extract<ConstantInt>(MD->getOperand(i + 1));
if (InnerOffset->getZExtValue() + InnerSize->getZExtValue() <= Offset)
continue;
uint64_t NewSize = InnerSize->getZExtValue();
uint64_t NewOffset = InnerOffset->getZExtValue() - Offset;
if (InnerOffset->getZExtValue() < Offset) {
NewOffset = 0;
NewSize -= Offset - InnerOffset->getZExtValue();
}
Sub.push_back(ConstantAsMetadata::get(
ConstantInt::get(InnerOffset->getType(), NewOffset)));
Sub.push_back(ConstantAsMetadata::get(
ConstantInt::get(InnerSize->getType(), NewSize)));
Sub.push_back(MD->getOperand(i + 2));
}
return MDNode::get(MD->getContext(), Sub);
}
MDNode *AAMDNodes::extendToTBAA(MDNode *MD, ssize_t Len) {
if (Len == 0)
return nullptr;
if (!isStructPathTBAA(MD))
return MD;
TBAAStructTagNode Tag(MD);
if (!Tag.isNewFormat())
return MD;
if (Len == -1)
return nullptr;
ArrayRef<MDOperand> MDOperands = MD->operands();
SmallVector<Metadata *, 4> NextNodes(MDOperands.begin(), MDOperands.end());
ConstantInt *PreviousSize = mdconst::extract<ConstantInt>(NextNodes[3]);
if (PreviousSize->equalsInt(Len))
return MD;
NextNodes[3] =
ConstantAsMetadata::get(ConstantInt::get(PreviousSize->getType(), Len));
return MDNode::get(MD->getContext(), NextNodes);
}