132 using namespace llvm;
147 TBAANode() : Node(0) {}
148 explicit TBAANode(
const MDNode *
N) : Node(N) {}
151 const MDNode *getNode()
const {
return Node; }
155 if (Node->getNumOperands() < 2)
157 MDNode *
P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
167 bool TypeIsImmutable()
const {
168 if (Node->getNumOperands() < 3)
180 class TBAAStructTagNode {
185 TBAAStructTagNode() : Node(0) {}
186 explicit TBAAStructTagNode(
const MDNode *
N) : Node(N) {}
189 const MDNode *getNode()
const {
return Node; }
191 const MDNode *getBaseType()
const {
192 return dyn_cast_or_null<MDNode>(Node->getOperand(0));
195 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
197 uint64_t getOffset()
const {
198 return cast<ConstantInt>(Node->getOperand(2))->getZExtValue();
203 bool TypeIsImmutable()
const {
204 if (Node->getNumOperands() < 4)
216 class TBAAStructTypeNode {
221 TBAAStructTypeNode() : Node(0) {}
222 explicit TBAAStructTypeNode(
const MDNode *
N) : Node(N) {}
225 const MDNode *getNode()
const {
return Node; }
229 TBAAStructTypeNode
getParent(uint64_t &Offset)
const {
231 if (Node->getNumOperands() < 2)
232 return TBAAStructTypeNode();
236 if (Node->getNumOperands() <= 3) {
238 cast<ConstantInt>(Node->getOperand(2))->getZExtValue();
240 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
242 return TBAAStructTypeNode();
243 return TBAAStructTypeNode(P);
249 for (
unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) {
250 uint64_t Cur = cast<ConstantInt>(Node->getOperand(Idx + 1))->
254 "TBAAStructTypeNode::getParent should have an offset match!");
261 TheIdx = Node->getNumOperands() - 2;
262 uint64_t Cur = cast<ConstantInt>(Node->getOperand(TheIdx + 1))->
265 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx));
267 return TBAAStructTypeNode();
268 return TBAAStructTypeNode(P);
284 virtual void initializePass() {
285 InitializeAliasAnalysis(
this);
292 virtual void *getAdjustedAnalysisPointer(
const void *PI) {
303 virtual AliasResult alias(
const Location &LocA,
const Location &LocB);
304 virtual bool pointsToConstantMemory(
const Location &Loc,
bool OrLocal);
308 const Location &Loc);
317 "Type-Based Alias Analysis",
false,
true,
false)
320 return new TypeBasedAliasAnalysis();
324 TypeBasedAliasAnalysis::getAnalysisUsage(
AnalysisUsage &AU)
const {
341 TypeBasedAliasAnalysis::Aliases(
const MDNode *
A,
344 return PathAliases(A, B);
347 TBAANode RootA, RootB;
350 for (TBAANode
T(A); ; ) {
351 if (
T.getNode() == B)
362 for (TBAANode
T(B); ; ) {
363 if (
T.getNode() ==
A)
377 if (RootA.getNode() != RootB.getNode())
387 TypeBasedAliasAnalysis::PathAliases(
const MDNode *A,
390 TBAAStructTypeNode RootA, RootB;
391 TBAAStructTagNode TagA(A), TagB(B);
402 const MDNode *BaseA = TagA.getBaseType();
403 const MDNode *BaseB = TagB.getBaseType();
404 uint64_t OffsetA = TagA.getOffset(), OffsetB = TagB.getOffset();
405 for (TBAAStructTypeNode
T(BaseA); ; ) {
406 if (
T.getNode() == BaseB)
408 return OffsetA == OffsetB;
413 T =
T.getParent(OffsetA);
420 OffsetA = TagA.getOffset();
421 for (TBAAStructTypeNode
T(BaseB); ; ) {
422 if (
T.getNode() == BaseA)
424 return OffsetA == OffsetB;
429 T =
T.getParent(OffsetB);
438 if (RootA.getNode() != RootB.getNode())
446 TypeBasedAliasAnalysis::alias(
const Location &LocA,
447 const Location &LocB) {
453 const MDNode *AM = LocA.TBAATag;
455 const MDNode *BM = LocB.TBAATag;
466 bool TypeBasedAliasAnalysis::pointsToConstantMemory(
const Location &Loc,
471 const MDNode *M = Loc.TBAATag;
495 Min = OnlyReadsMemory;
501 TypeBasedAliasAnalysis::getModRefBehavior(
const Function *
F) {
508 const Location &Loc) {
512 if (
const MDNode *L = Loc.TBAATag)
531 if (!Aliases(M1, M2))
541 if (Tag1->getString() ==
"vtable pointer")
return true;
549 if (!Tag)
return false;
551 if (Tag1->getString() ==
"vtable pointer")
return true;
586 int IA = PathA.
size() - 1;
587 int IB = PathB.
size() - 1;
590 while (IA >= 0 && IB >=0) {
591 if (PathA[IA] == PathB[IB])
void push_back(const T &Elt)
virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal=false)
static PassRegistry * getPassRegistry()
ModRefResult getModRefInfo(const Instruction *I, const Location &Loc)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getNumOperands() const
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
MDNode - a tuple of other values.
static MDNode * get(LLVMContext &Context, ArrayRef< Value * > Vals)
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
void initializeTypeBasedAliasAnalysisPass(PassRegistry &)
const APInt & getValue() const
Return the constant's value.
ID
LLVM Calling Convention Representation.
bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
initializer< Ty > init(const Ty &Val)
ImmutablePass * createTypeBasedAliasAnalysisPass()
InstrTy * getInstruction() const
virtual AliasResult alias(const Location &LocA, const Location &LocB)
static Type * getAccessType(const Instruction *Inst)
getAccessType - Return the type of the memory being accessed.
static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
Methods for metadata merging.
LLVMContext & getContext() const
All values hold a context through their type.
virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS)
getModRefBehavior - Return the behavior when calling the given call site.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
Get or create an IntegerType instance.
Class for constant integers.
MDNode * getMetadata(unsigned KindID) const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis,"tbaa","Type-Based Alias Analysis", false, true, false) ImmutablePass *llvm
static cl::opt< bool > EnableTBAA("enable-tbaa", cl::init(true))
ImmutableCallSite - establish a view to a call site for examination.
LLVM Value Representation.
static const Function * getParent(const Value *V)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
static bool isStructPathTBAA(const MDNode *MD)