2002-08-22 20:25:32 +02:00
|
|
|
//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-08-22 20:25:32 +02:00
|
|
|
//
|
|
|
|
// This file implements the generic AliasAnalysis interface which is used as the
|
|
|
|
// common interface used by all clients and implementations of alias analysis.
|
|
|
|
//
|
|
|
|
// This file also implements the default version of the AliasAnalysis interface
|
|
|
|
// that is to be used when no other implementation is specified. This does some
|
|
|
|
// simple tests that detect obvious cases: two different global pointers cannot
|
|
|
|
// alias, a global cannot alias a malloc, two different mallocs cannot alias,
|
|
|
|
// etc.
|
|
|
|
//
|
|
|
|
// This alias analysis implementation really isn't very good for anything, but
|
|
|
|
// it is very fast, and makes a nice clean default implementation. Because it
|
|
|
|
// handles lots of little corner cases, other, more complex, alias analysis
|
|
|
|
// implementations may choose to rely on this pass to resolve these simple and
|
|
|
|
// easy cases.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-02-26 20:41:54 +01:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2012-05-14 22:35:04 +02:00
|
|
|
#include "llvm/Analysis/CaptureTracking.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Pass.h"
|
2012-08-29 17:32:21 +02:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2004-03-15 05:07:29 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-08-22 20:25:32 +02:00
|
|
|
// Register the AliasAnalysis interface, providing a nice name to refer to.
|
2010-10-19 19:21:58 +02:00
|
|
|
INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
|
2007-05-03 03:11:54 +02:00
|
|
|
char AliasAnalysis::ID = 0;
|
2002-08-22 20:25:32 +02:00
|
|
|
|
2004-05-23 23:15:48 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Default chaining methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
AliasAnalysis::AliasResult
|
2010-09-14 23:25:10 +02:00
|
|
|
AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
|
2004-05-23 23:15:48 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-09-14 23:25:10 +02:00
|
|
|
return AA->alias(LocA, LocB);
|
2004-05-23 23:15:48 +02:00
|
|
|
}
|
|
|
|
|
2010-11-08 17:45:26 +01:00
|
|
|
bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
|
|
|
|
bool OrLocal) {
|
2004-05-23 23:15:48 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-11-08 17:45:26 +01:00
|
|
|
return AA->pointsToConstantMemory(Loc, OrLocal);
|
2004-05-23 23:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AliasAnalysis::deleteValue(Value *V) {
|
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
|
|
|
AA->deleteValue(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AliasAnalysis::copyValue(Value *From, Value *To) {
|
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
|
|
|
AA->copyValue(From, To);
|
|
|
|
}
|
|
|
|
|
2011-01-03 22:38:41 +01:00
|
|
|
void AliasAnalysis::addEscapingUse(Use &U) {
|
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
|
|
|
AA->addEscapingUse(U);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-06 03:25:49 +02:00
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
|
2010-09-14 23:25:10 +02:00
|
|
|
const Location &Loc) {
|
2010-10-25 18:28:57 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-08-06 03:25:49 +02:00
|
|
|
|
|
|
|
ModRefBehavior MRB = getModRefBehavior(CS);
|
|
|
|
if (MRB == DoesNotAccessMemory)
|
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
ModRefResult Mask = ModRef;
|
2010-11-09 21:06:55 +01:00
|
|
|
if (onlyReadsMemory(MRB))
|
2010-08-06 03:25:49 +02:00
|
|
|
Mask = Ref;
|
2010-11-09 21:06:55 +01:00
|
|
|
|
2010-11-10 19:17:28 +01:00
|
|
|
if (onlyAccessesArgPointees(MRB)) {
|
2010-08-06 03:25:49 +02:00
|
|
|
bool doesAlias = false;
|
2011-04-27 20:39:03 +02:00
|
|
|
if (doesAccessArgPointees(MRB)) {
|
|
|
|
MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
|
2010-11-10 19:17:28 +01:00
|
|
|
for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
|
2011-04-27 20:39:03 +02:00
|
|
|
AI != AE; ++AI) {
|
|
|
|
const Value *Arg = *AI;
|
|
|
|
if (!Arg->getType()->isPointerTy())
|
|
|
|
continue;
|
|
|
|
Location CSLoc(Arg, UnknownSize, CSTag);
|
|
|
|
if (!isNoAlias(CSLoc, Loc)) {
|
2010-11-10 19:17:28 +01:00
|
|
|
doesAlias = true;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 20:39:03 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-06 03:25:49 +02:00
|
|
|
if (!doesAlias)
|
|
|
|
return NoModRef;
|
|
|
|
}
|
|
|
|
|
2010-09-14 23:25:10 +02:00
|
|
|
// If Loc is a constant memory location, the call definitely could not
|
2010-08-06 03:25:49 +02:00
|
|
|
// modify the memory location.
|
2010-09-14 23:25:10 +02:00
|
|
|
if ((Mask & Mod) && pointsToConstantMemory(Loc))
|
2010-08-06 03:25:49 +02:00
|
|
|
Mask = ModRefResult(Mask & ~Mod);
|
|
|
|
|
2010-10-25 18:29:52 +02:00
|
|
|
// If this is the end of the chain, don't forward.
|
2010-08-06 03:25:49 +02:00
|
|
|
if (!AA) return Mask;
|
|
|
|
|
|
|
|
// Otherwise, fall back to the next AA in the chain. But we can merge
|
|
|
|
// in any mask we've managed to compute.
|
2010-09-14 23:25:10 +02:00
|
|
|
return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
|
2010-08-06 03:25:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-23 23:15:48 +02:00
|
|
|
AliasAnalysis::ModRefResult
|
2010-08-03 23:48:53 +02:00
|
|
|
AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
|
2010-10-25 18:28:57 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-08-06 03:25:49 +02:00
|
|
|
|
|
|
|
// If CS1 or CS2 are readnone, they don't interact.
|
|
|
|
ModRefBehavior CS1B = getModRefBehavior(CS1);
|
|
|
|
if (CS1B == DoesNotAccessMemory) return NoModRef;
|
|
|
|
|
|
|
|
ModRefBehavior CS2B = getModRefBehavior(CS2);
|
|
|
|
if (CS2B == DoesNotAccessMemory) return NoModRef;
|
|
|
|
|
|
|
|
// If they both only read from memory, there is no dependence.
|
2010-11-09 21:06:55 +01:00
|
|
|
if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
|
2010-08-06 03:25:49 +02:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
AliasAnalysis::ModRefResult Mask = ModRef;
|
|
|
|
|
|
|
|
// If CS1 only reads memory, the only dependence on CS2 can be
|
|
|
|
// from CS1 reading memory written by CS2.
|
2010-11-09 21:06:55 +01:00
|
|
|
if (onlyReadsMemory(CS1B))
|
2010-08-06 03:25:49 +02:00
|
|
|
Mask = ModRefResult(Mask & Ref);
|
|
|
|
|
|
|
|
// If CS2 only access memory through arguments, accumulate the mod/ref
|
|
|
|
// information from CS1's references to the memory referenced by
|
|
|
|
// CS2's arguments.
|
2010-11-10 19:17:28 +01:00
|
|
|
if (onlyAccessesArgPointees(CS2B)) {
|
2010-08-06 03:25:49 +02:00
|
|
|
AliasAnalysis::ModRefResult R = NoModRef;
|
2011-04-27 20:39:03 +02:00
|
|
|
if (doesAccessArgPointees(CS2B)) {
|
|
|
|
MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
|
2010-11-10 19:17:28 +01:00
|
|
|
for (ImmutableCallSite::arg_iterator
|
|
|
|
I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
|
2011-04-27 20:39:03 +02:00
|
|
|
const Value *Arg = *I;
|
|
|
|
if (!Arg->getType()->isPointerTy())
|
|
|
|
continue;
|
|
|
|
Location CS2Loc(Arg, UnknownSize, CS2Tag);
|
|
|
|
R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
|
2010-11-10 19:17:28 +01:00
|
|
|
if (R == Mask)
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 20:39:03 +02:00
|
|
|
}
|
2010-08-06 03:25:49 +02:00
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If CS1 only accesses memory through arguments, check if CS2 references
|
|
|
|
// any of the memory referenced by CS1's arguments. If not, return NoModRef.
|
2010-11-10 19:17:28 +01:00
|
|
|
if (onlyAccessesArgPointees(CS1B)) {
|
2010-08-06 03:25:49 +02:00
|
|
|
AliasAnalysis::ModRefResult R = NoModRef;
|
2011-04-27 20:39:03 +02:00
|
|
|
if (doesAccessArgPointees(CS1B)) {
|
|
|
|
MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
|
2010-11-10 19:17:28 +01:00
|
|
|
for (ImmutableCallSite::arg_iterator
|
2011-04-27 20:39:03 +02:00
|
|
|
I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
|
|
|
|
const Value *Arg = *I;
|
|
|
|
if (!Arg->getType()->isPointerTy())
|
|
|
|
continue;
|
|
|
|
Location CS1Loc(Arg, UnknownSize, CS1Tag);
|
|
|
|
if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
|
2010-11-10 19:17:28 +01:00
|
|
|
R = Mask;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 20:39:03 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-06 03:25:49 +02:00
|
|
|
if (R == NoModRef)
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2010-10-25 18:29:52 +02:00
|
|
|
// If this is the end of the chain, don't forward.
|
2010-08-06 03:25:49 +02:00
|
|
|
if (!AA) return Mask;
|
|
|
|
|
|
|
|
// Otherwise, fall back to the next AA in the chain. But we can merge
|
|
|
|
// in any mask we've managed to compute.
|
|
|
|
return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::ModRefBehavior
|
|
|
|
AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
|
2010-10-25 18:28:57 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-08-06 03:25:49 +02:00
|
|
|
|
|
|
|
ModRefBehavior Min = UnknownModRefBehavior;
|
|
|
|
|
|
|
|
// Call back into the alias analysis with the other form of getModRefBehavior
|
|
|
|
// to see if it can give a better response.
|
|
|
|
if (const Function *F = CS.getCalledFunction())
|
|
|
|
Min = getModRefBehavior(F);
|
|
|
|
|
2010-10-25 18:29:52 +02:00
|
|
|
// If this is the end of the chain, don't forward.
|
2010-08-06 03:25:49 +02:00
|
|
|
if (!AA) return Min;
|
|
|
|
|
|
|
|
// Otherwise, fall back to the next AA in the chain. But we can merge
|
|
|
|
// in any result we've managed to compute.
|
2010-11-10 02:02:18 +01:00
|
|
|
return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
|
2010-08-06 03:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::ModRefBehavior
|
|
|
|
AliasAnalysis::getModRefBehavior(const Function *F) {
|
2004-05-23 23:15:48 +02:00
|
|
|
assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
|
2010-08-06 03:25:49 +02:00
|
|
|
return AA->getModRefBehavior(F);
|
2004-05-23 23:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AliasAnalysis non-virtual helper method implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-11 22:50:19 +01:00
|
|
|
AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
|
|
|
|
return Location(LI->getPointerOperand(),
|
|
|
|
getTypeStoreSize(LI->getType()),
|
|
|
|
LI->getMetadata(LLVMContext::MD_tbaa));
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
|
|
|
|
return Location(SI->getPointerOperand(),
|
|
|
|
getTypeStoreSize(SI->getValueOperand()->getType()),
|
|
|
|
SI->getMetadata(LLVMContext::MD_tbaa));
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
|
|
|
|
return Location(VI->getPointerOperand(),
|
|
|
|
UnknownSize,
|
|
|
|
VI->getMetadata(LLVMContext::MD_tbaa));
|
|
|
|
}
|
|
|
|
|
2011-09-26 22:15:28 +02:00
|
|
|
AliasAnalysis::Location
|
|
|
|
AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
|
|
|
|
return Location(CXI->getPointerOperand(),
|
|
|
|
getTypeStoreSize(CXI->getCompareOperand()->getType()),
|
|
|
|
CXI->getMetadata(LLVMContext::MD_tbaa));
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::Location
|
|
|
|
AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
|
|
|
|
return Location(RMWI->getPointerOperand(),
|
|
|
|
getTypeStoreSize(RMWI->getValOperand()->getType()),
|
|
|
|
RMWI->getMetadata(LLVMContext::MD_tbaa));
|
|
|
|
}
|
2010-11-21 08:51:27 +01:00
|
|
|
|
|
|
|
AliasAnalysis::Location
|
|
|
|
AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
|
|
|
|
uint64_t Size = UnknownSize;
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
|
|
|
|
Size = C->getValue().getZExtValue();
|
|
|
|
|
2010-12-16 03:51:19 +01:00
|
|
|
// memcpy/memmove can have TBAA tags. For memcpy, they apply
|
|
|
|
// to both the source and the destination.
|
|
|
|
MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
|
|
|
|
|
|
|
|
return Location(MTI->getRawSource(), Size, TBAATag);
|
2010-11-21 08:51:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::Location
|
2010-11-30 02:48:20 +01:00
|
|
|
AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
|
2010-11-21 08:51:27 +01:00
|
|
|
uint64_t Size = UnknownSize;
|
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
|
|
|
|
Size = C->getValue().getZExtValue();
|
2010-12-16 03:51:19 +01:00
|
|
|
|
|
|
|
// memcpy/memmove can have TBAA tags. For memcpy, they apply
|
|
|
|
// to both the source and the destination.
|
|
|
|
MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
|
2010-11-21 08:51:27 +01:00
|
|
|
|
2010-12-16 03:51:19 +01:00
|
|
|
return Location(MTI->getRawDest(), Size, TBAATag);
|
2010-11-21 08:51:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-26 20:26:51 +01:00
|
|
|
AliasAnalysis::ModRefResult
|
2010-09-14 23:25:10 +02:00
|
|
|
AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
|
2011-08-15 22:54:19 +02:00
|
|
|
// Be conservative in the face of volatile/atomic.
|
|
|
|
if (!L->isUnordered())
|
2010-08-06 20:11:28 +02:00
|
|
|
return ModRef;
|
|
|
|
|
2010-08-03 19:27:43 +02:00
|
|
|
// If the load address doesn't alias the given address, it doesn't read
|
|
|
|
// or write the specified memory.
|
2010-11-11 22:50:19 +01:00
|
|
|
if (!alias(getLocation(L), Loc))
|
2010-08-03 19:27:43 +02:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
// Otherwise, a load just reads.
|
|
|
|
return Ref;
|
2003-02-26 20:26:51 +01:00
|
|
|
}
|
2002-08-22 20:25:32 +02:00
|
|
|
|
2003-02-26 20:26:51 +01:00
|
|
|
AliasAnalysis::ModRefResult
|
2010-09-14 23:25:10 +02:00
|
|
|
AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
|
2011-08-15 22:54:19 +02:00
|
|
|
// Be conservative in the face of volatile/atomic.
|
|
|
|
if (!S->isUnordered())
|
2010-08-06 20:11:28 +02:00
|
|
|
return ModRef;
|
|
|
|
|
2010-08-06 20:10:45 +02:00
|
|
|
// If the store address cannot alias the pointer in question, then the
|
|
|
|
// specified memory cannot be modified by the store.
|
2010-11-11 22:50:19 +01:00
|
|
|
if (!alias(getLocation(S), Loc))
|
2004-01-30 23:16:42 +01:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
// If the pointer is a pointer to constant memory, then it could not have been
|
|
|
|
// modified by this store.
|
2010-09-14 23:25:10 +02:00
|
|
|
if (pointsToConstantMemory(Loc))
|
2010-08-03 19:27:43 +02:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
// Otherwise, a store just writes.
|
|
|
|
return Mod;
|
2002-08-22 20:25:32 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 20:24:38 +02:00
|
|
|
AliasAnalysis::ModRefResult
|
2010-09-14 23:25:10 +02:00
|
|
|
AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
|
2010-08-06 20:24:38 +02:00
|
|
|
// If the va_arg address cannot alias the pointer in question, then the
|
|
|
|
// specified memory cannot be accessed by the va_arg.
|
2010-11-11 22:50:19 +01:00
|
|
|
if (!alias(getLocation(V), Loc))
|
2010-08-06 20:24:38 +02:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
// If the pointer is a pointer to constant memory, then it could not have been
|
|
|
|
// modified by this va_arg.
|
2010-09-14 23:25:10 +02:00
|
|
|
if (pointsToConstantMemory(Loc))
|
2010-08-06 20:24:38 +02:00
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
// Otherwise, a va_arg reads and writes.
|
|
|
|
return ModRef;
|
|
|
|
}
|
|
|
|
|
2011-09-26 22:15:28 +02:00
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
|
|
|
|
// Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
|
|
|
|
if (CX->getOrdering() > Monotonic)
|
|
|
|
return ModRef;
|
|
|
|
|
|
|
|
// If the cmpxchg address does not alias the location, it does not access it.
|
|
|
|
if (!alias(getLocation(CX), Loc))
|
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
return ModRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
|
|
|
|
// Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
|
|
|
|
if (RMW->getOrdering() > Monotonic)
|
|
|
|
return ModRef;
|
|
|
|
|
|
|
|
// If the atomicrmw address does not alias the location, it does not access it.
|
|
|
|
if (!alias(getLocation(RMW), Loc))
|
|
|
|
return NoModRef;
|
|
|
|
|
|
|
|
return ModRef;
|
|
|
|
}
|
|
|
|
|
2012-05-14 22:35:04 +02:00
|
|
|
namespace {
|
2013-01-04 20:19:47 +01:00
|
|
|
// Conservatively return true. Return false, if there is a single path
|
|
|
|
// starting from "From" and the path does not reach "To".
|
|
|
|
static bool hasPath(const BasicBlock *From, const BasicBlock *To) {
|
|
|
|
const unsigned MaxCheck = 5;
|
|
|
|
const BasicBlock *Current = From;
|
|
|
|
for (unsigned I = 0; I < MaxCheck; I++) {
|
|
|
|
unsigned NumSuccs = Current->getTerminator()->getNumSuccessors();
|
|
|
|
if (NumSuccs > 1)
|
|
|
|
return true;
|
|
|
|
if (NumSuccs == 0)
|
|
|
|
return false;
|
|
|
|
Current = Current->getTerminator()->getSuccessor(0);
|
|
|
|
if (Current == To)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-14 22:35:04 +02:00
|
|
|
/// Only find pointer captures which happen before the given instruction. Uses
|
|
|
|
/// the dominator tree to determine whether one instruction is before another.
|
2013-01-04 20:19:47 +01:00
|
|
|
/// Only support the case where the Value is defined in the same basic block
|
|
|
|
/// as the given instruction and the use.
|
2012-05-14 22:35:04 +02:00
|
|
|
struct CapturesBefore : public CaptureTracker {
|
|
|
|
CapturesBefore(const Instruction *I, DominatorTree *DT)
|
|
|
|
: BeforeHere(I), DT(DT), Captured(false) {}
|
|
|
|
|
|
|
|
void tooManyUses() { Captured = true; }
|
|
|
|
|
|
|
|
bool shouldExplore(Use *U) {
|
|
|
|
Instruction *I = cast<Instruction>(U->getUser());
|
|
|
|
BasicBlock *BB = I->getParent();
|
2013-01-04 20:19:47 +01:00
|
|
|
// We explore this usage only if the usage can reach "BeforeHere".
|
|
|
|
// If use is not reachable from entry, there is no need to explore.
|
|
|
|
if (BeforeHere != I && !DT->isReachableFromEntry(BB))
|
|
|
|
return false;
|
|
|
|
// If the value is defined in the same basic block as use and BeforeHere,
|
|
|
|
// there is no need to explore the use if BeforeHere dominates use.
|
|
|
|
// Check whether there is a path from I to BeforeHere.
|
|
|
|
if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
|
|
|
|
!hasPath(BB, BeforeHere->getParent()))
|
2012-05-14 22:35:04 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool captured(Use *U) {
|
|
|
|
Instruction *I = cast<Instruction>(U->getUser());
|
|
|
|
BasicBlock *BB = I->getParent();
|
2013-01-04 20:19:47 +01:00
|
|
|
// Same logic as in shouldExplore.
|
|
|
|
if (BeforeHere != I && !DT->isReachableFromEntry(BB))
|
|
|
|
return false;
|
|
|
|
if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
|
|
|
|
!hasPath(BB, BeforeHere->getParent()))
|
2012-05-14 22:35:04 +02:00
|
|
|
return false;
|
|
|
|
Captured = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Instruction *BeforeHere;
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
|
|
|
bool Captured;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is really just shoring-up a deficiency in alias analysis.
|
|
|
|
// BasicAA isn't willing to spend linear time determining whether an alloca
|
|
|
|
// was captured before or after this particular call, while we are. However,
|
|
|
|
// with a smarter AA in place, this test is just wasting compile time.
|
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
AliasAnalysis::callCapturesBefore(const Instruction *I,
|
|
|
|
const AliasAnalysis::Location &MemLoc,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
if (!DT || !TD) return AliasAnalysis::ModRef;
|
|
|
|
|
|
|
|
const Value *Object = GetUnderlyingObject(MemLoc.Ptr, TD);
|
|
|
|
if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
|
|
|
|
isa<Constant>(Object))
|
|
|
|
return AliasAnalysis::ModRef;
|
|
|
|
|
|
|
|
ImmutableCallSite CS(I);
|
|
|
|
if (!CS.getInstruction() || CS.getInstruction() == Object)
|
|
|
|
return AliasAnalysis::ModRef;
|
|
|
|
|
|
|
|
CapturesBefore CB(I, DT);
|
|
|
|
llvm::PointerMayBeCaptured(Object, &CB);
|
|
|
|
if (CB.Captured)
|
|
|
|
return AliasAnalysis::ModRef;
|
|
|
|
|
|
|
|
unsigned ArgNo = 0;
|
2013-07-07 12:15:16 +02:00
|
|
|
AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef;
|
2012-05-14 22:35:04 +02:00
|
|
|
for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
|
|
|
|
CI != CE; ++CI, ++ArgNo) {
|
|
|
|
// Only look at the no-capture or byval pointer arguments. If this
|
|
|
|
// pointer were passed to arguments that were neither of these, then it
|
|
|
|
// couldn't be no-capture.
|
|
|
|
if (!(*CI)->getType()->isPointerTy() ||
|
|
|
|
(!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this is a no-capture pointer argument, see if we can tell that it
|
|
|
|
// is impossible to alias the pointer we're checking. If not, we have to
|
|
|
|
// assume that the call could touch the pointer, even though it doesn't
|
|
|
|
// escape.
|
2013-07-07 12:15:16 +02:00
|
|
|
if (isNoAlias(AliasAnalysis::Location(*CI),
|
|
|
|
AliasAnalysis::Location(Object)))
|
|
|
|
continue;
|
|
|
|
if (CS.doesNotAccessMemory(ArgNo))
|
|
|
|
continue;
|
|
|
|
if (CS.onlyReadsMemory(ArgNo)) {
|
|
|
|
R = AliasAnalysis::Ref;
|
|
|
|
continue;
|
2012-05-14 22:35:04 +02:00
|
|
|
}
|
2013-07-07 12:15:16 +02:00
|
|
|
return AliasAnalysis::ModRef;
|
2012-05-14 22:35:04 +02:00
|
|
|
}
|
2013-07-07 12:15:16 +02:00
|
|
|
return R;
|
2012-05-14 22:35:04 +02:00
|
|
|
}
|
2011-09-26 22:15:28 +02:00
|
|
|
|
2002-08-22 20:25:32 +02:00
|
|
|
// AliasAnalysis destructor: DO NOT move this to the header file for
|
|
|
|
// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
|
|
|
|
// the AliasAnalysis.o file in the current .a file, causing alias analysis
|
|
|
|
// support to not be included in the tool correctly!
|
|
|
|
//
|
|
|
|
AliasAnalysis::~AliasAnalysis() {}
|
|
|
|
|
2008-05-30 02:02:02 +02:00
|
|
|
/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
|
2003-02-26 20:26:51 +01:00
|
|
|
/// AliasAnalysis interface before any other methods are called.
|
2002-08-23 00:46:39 +02:00
|
|
|
///
|
2003-02-26 20:26:51 +01:00
|
|
|
void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
|
2012-10-08 18:38:25 +02:00
|
|
|
TD = P->getAnalysisIfAvailable<DataLayout>();
|
2012-08-29 17:32:21 +02:00
|
|
|
TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>();
|
2004-05-23 23:15:48 +02:00
|
|
|
AA = &P->getAnalysis<AliasAnalysis>();
|
2003-02-26 20:26:51 +01:00
|
|
|
}
|
2002-08-22 20:25:32 +02:00
|
|
|
|
2003-02-26 20:26:51 +01:00
|
|
|
// getAnalysisUsage - All alias analysis implementations should invoke this
|
2009-07-25 02:48:42 +02:00
|
|
|
// directly (using AliasAnalysis::getAnalysisUsage(AU)).
|
2003-02-26 20:26:51 +01:00
|
|
|
void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
|
2004-05-23 23:15:48 +02:00
|
|
|
AU.addRequired<AliasAnalysis>(); // All AA's chain
|
2003-02-26 20:26:51 +01:00
|
|
|
}
|
2002-08-22 20:25:32 +02:00
|
|
|
|
2012-10-08 18:38:25 +02:00
|
|
|
/// getTypeStoreSize - Return the DataLayout store size for the given type,
|
2009-07-25 02:48:42 +02:00
|
|
|
/// if known, or a conservative value otherwise.
|
|
|
|
///
|
2011-07-18 06:54:35 +02:00
|
|
|
uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
|
2010-10-19 19:06:23 +02:00
|
|
|
return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
|
2009-07-25 02:48:42 +02:00
|
|
|
}
|
|
|
|
|
2003-02-26 20:26:51 +01:00
|
|
|
/// canBasicBlockModify - Return true if it is possible for execution of the
|
|
|
|
/// specified basic block to modify the value pointed to by Ptr.
|
|
|
|
///
|
|
|
|
bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
|
2010-09-14 23:25:10 +02:00
|
|
|
const Location &Loc) {
|
|
|
|
return canInstructionRangeModify(BB.front(), BB.back(), Loc);
|
2002-08-22 20:25:32 +02:00
|
|
|
}
|
|
|
|
|
2002-08-23 00:46:39 +02:00
|
|
|
/// canInstructionRangeModify - Return true if it is possible for the execution
|
|
|
|
/// of the specified instructions to modify the value pointed to by Ptr. The
|
|
|
|
/// instructions to consider are all of the instructions in the range of [I1,I2]
|
|
|
|
/// INCLUSIVE. I1 and I2 must be in the same basic block.
|
|
|
|
///
|
2002-08-22 20:25:32 +02:00
|
|
|
bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
|
|
|
|
const Instruction &I2,
|
2010-09-14 23:25:10 +02:00
|
|
|
const Location &Loc) {
|
2002-08-22 20:25:32 +02:00
|
|
|
assert(I1.getParent() == I2.getParent() &&
|
|
|
|
"Instructions not in same basic block!");
|
2010-08-03 23:48:53 +02:00
|
|
|
BasicBlock::const_iterator I = &I1;
|
|
|
|
BasicBlock::const_iterator E = &I2;
|
2002-08-22 20:25:32 +02:00
|
|
|
++E; // Convert from inclusive to exclusive range.
|
|
|
|
|
2003-02-26 20:26:51 +01:00
|
|
|
for (; I != E; ++I) // Check every instruction in range
|
2010-09-14 23:25:10 +02:00
|
|
|
if (getModRefInfo(I, Loc) & Mod)
|
2002-08-22 20:25:32 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-03 02:28:32 +01:00
|
|
|
/// isNoAliasCall - Return true if this pointer is returned by a noalias
|
|
|
|
/// function.
|
|
|
|
bool llvm::isNoAliasCall(const Value *V) {
|
|
|
|
if (isa<CallInst>(V) || isa<InvokeInst>(V))
|
2010-08-03 23:48:53 +02:00
|
|
|
return ImmutableCallSite(cast<Instruction>(V))
|
2012-12-19 08:18:57 +01:00
|
|
|
.paramHasAttr(0, Attribute::NoAlias);
|
2009-02-03 02:28:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-28 10:17:48 +02:00
|
|
|
/// isNoAliasArgument - Return true if this is an argument with the noalias
|
|
|
|
/// attribute.
|
|
|
|
bool llvm::isNoAliasArgument(const Value *V)
|
|
|
|
{
|
|
|
|
if (const Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->hasNoAliasAttr();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-03 02:28:32 +01:00
|
|
|
/// isIdentifiedObject - Return true if this pointer refers to a distinct and
|
|
|
|
/// identifiable object. This returns true for:
|
2009-08-27 19:52:56 +02:00
|
|
|
/// Global Variables and Functions (but not Global Aliases)
|
2009-02-03 02:28:32 +01:00
|
|
|
/// Allocas and Mallocs
|
2010-07-07 16:27:09 +02:00
|
|
|
/// ByVal and NoAlias Arguments
|
|
|
|
/// NoAlias returns
|
2009-02-03 02:28:32 +01:00
|
|
|
///
|
2010-07-07 16:27:09 +02:00
|
|
|
bool llvm::isIdentifiedObject(const Value *V) {
|
2010-06-29 02:50:39 +02:00
|
|
|
if (isa<AllocaInst>(V))
|
2009-08-27 19:52:56 +02:00
|
|
|
return true;
|
|
|
|
if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
|
2009-02-03 02:28:32 +01:00
|
|
|
return true;
|
2010-07-07 16:27:09 +02:00
|
|
|
if (isNoAliasCall(V))
|
|
|
|
return true;
|
|
|
|
if (const Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->hasNoAliasAttr() || A->hasByValAttr();
|
2009-02-03 02:28:32 +01:00
|
|
|
return false;
|
|
|
|
}
|