Skip to content

Commit

Permalink
Enable llvm's isa/cast/dyn_cast on MemAccInst.
Browse files Browse the repository at this point in the history
Differential Revision: http://reviews.llvm.org/D17250

llvm-svn: 262100
  • Loading branch information
etherzhhb committed Feb 27, 2016
1 parent 272a2bc commit 8efb22e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
19 changes: 18 additions & 1 deletion polly/include/polly/Support/ScopHelper.h
Expand Up @@ -242,6 +242,10 @@ class MemAccInst {

bool isNull() const { return !I; }
bool isInstruction() const { return I; }

llvm::Instruction *asInstruction() const { return I; }

private:
bool isLoad() const { return I && llvm::isa<llvm::LoadInst>(I); }
bool isStore() const { return I && llvm::isa<llvm::StoreInst>(I); }
bool isCallInst() const { return I && llvm::isa<llvm::CallInst>(I); }
Expand All @@ -251,7 +255,6 @@ class MemAccInst {
return I && llvm::isa<llvm::MemTransferInst>(I);
}

llvm::Instruction *asInstruction() const { return I; }
llvm::LoadInst *asLoad() const { return llvm::cast<llvm::LoadInst>(I); }
llvm::StoreInst *asStore() const { return llvm::cast<llvm::StoreInst>(I); }
llvm::CallInst *asCallInst() const { return llvm::cast<llvm::CallInst>(I); }
Expand All @@ -265,6 +268,20 @@ class MemAccInst {
return llvm::cast<llvm::MemTransferInst>(I);
}
};
}

namespace llvm {
/// @brief Specialize simplify_type for MemAccInst to enable dyn_cast and cast
/// from a MemAccInst object.
template <> struct simplify_type<polly::MemAccInst> {
typedef Instruction *SimpleType;
static SimpleType getSimplifiedValue(polly::MemAccInst &I) {
return I.asInstruction();
}
};
}

namespace polly {

/// @brief Check if the PHINode has any incoming Invoke edge.
///
Expand Down
4 changes: 2 additions & 2 deletions polly/lib/Analysis/ScopDetection.cpp
Expand Up @@ -989,8 +989,8 @@ bool ScopDetection::isValidInstruction(Instruction &Inst,

// Check the access function.
if (auto MemInst = MemAccInst::dyn_cast(Inst)) {
Context.hasStores |= MemInst.isStore();
Context.hasLoads |= MemInst.isLoad();
Context.hasStores |= isa<StoreInst>(MemInst);
Context.hasLoads |= isa<LoadInst>(MemInst);
if (!MemInst.isSimple())
return invalid<ReportNonSimpleMemoryAccess>(Context, /*Assert=*/true,
&Inst);
Expand Down
46 changes: 24 additions & 22 deletions polly/lib/Analysis/ScopInfo.cpp
Expand Up @@ -616,9 +616,7 @@ void MemoryAccess::assumeNoOutOfBound() {
}

void MemoryAccess::buildMemIntrinsicAccessRelation() {
auto MAI = MemAccInst(getAccessInstruction());
(void)MAI;
assert(MAI.isMemIntrinsic());
assert(isa<MemIntrinsic>(getAccessInstruction()));
assert(Subscripts.size() == 2 && Sizes.size() == 0);

auto *SubscriptPWA = Statement->getPwAff(Subscripts[0]);
Expand Down Expand Up @@ -646,7 +644,7 @@ void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) {
ScalarEvolution *SE = Statement->getParent()->getSE();

auto MAI = MemAccInst(getAccessInstruction());
if (MAI.isMemIntrinsic())
if (isa<MemIntrinsic>(MAI))
return;

Value *Ptr = MAI.getPointerOperand();
Expand Down Expand Up @@ -2613,8 +2611,8 @@ bool Scop::buildAliasGroups(AliasAnalysis &AA) {
if (!MA->isRead())
HasWriteAccess.insert(MA->getBaseAddr());
MemAccInst Acc(MA->getAccessInstruction());
if (MA->isRead() && Acc.isMemTransferInst())
PtrToAcc[Acc.asMemTransferInst()->getSource()] = MA;
if (MA->isRead() && isa<MemTransferInst>(Acc))
PtrToAcc[cast<MemTransferInst>(Acc)->getSource()] = MA;
else
PtrToAcc[Acc.getPointerOperand()] = MA;
AST.add(Acc);
Expand Down Expand Up @@ -3850,7 +3848,7 @@ bool ScopInfo::buildAccessMultiDimFixed(
const SCEVUnknown *BasePointer =
dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
enum MemoryAccess::AccessType Type =
Inst.isLoad() ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;

if (auto *BitCast = dyn_cast<BitCastInst>(Address)) {
auto *Src = BitCast->getOperand(0);
Expand Down Expand Up @@ -3905,7 +3903,7 @@ bool ScopInfo::buildAccessMultiDimParam(
Type *ElementType = Val->getType();
unsigned ElementSize = DL->getTypeAllocSize(ElementType);
enum MemoryAccess::AccessType Type =
Inst.isLoad() ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;

const SCEV *AccessFunction = SE->getSCEVAtScope(Address, L);
const SCEVUnknown *BasePointer =
Expand Down Expand Up @@ -3942,10 +3940,12 @@ bool ScopInfo::buildAccessMemIntrinsic(
MemAccInst Inst, Loop *L, Region *R,
const ScopDetection::BoxedLoopsSetTy *BoxedLoops,
const InvariantLoadsSetTy &ScopRIL) {
if (!Inst.isMemIntrinsic())
auto *MemIntr = dyn_cast_or_null<MemIntrinsic>(Inst);

if (MemIntr == nullptr)
return false;

auto *LengthVal = SE->getSCEVAtScope(Inst.asMemIntrinsic()->getLength(), L);
auto *LengthVal = SE->getSCEVAtScope(MemIntr->getLength(), L);
assert(LengthVal);

// Check if the length val is actually affine or if we overapproximate it
Expand All @@ -3957,7 +3957,7 @@ bool ScopInfo::buildAccessMemIntrinsic(
if (!LengthIsAffine)
LengthVal = nullptr;

auto *DestPtrVal = Inst.asMemIntrinsic()->getDest();
auto *DestPtrVal = MemIntr->getDest();
assert(DestPtrVal);
auto *DestAccFunc = SE->getSCEVAtScope(DestPtrVal, L);
assert(DestAccFunc);
Expand All @@ -3968,10 +3968,11 @@ bool ScopInfo::buildAccessMemIntrinsic(
IntegerType::getInt8Ty(DestPtrVal->getContext()), false,
{DestAccFunc, LengthVal}, {}, Inst.getValueOperand());

if (!Inst.isMemTransferInst())
auto *MemTrans = dyn_cast<MemTransferInst>(MemIntr);
if (!MemTrans)
return true;

auto *SrcPtrVal = Inst.asMemTransferInst()->getSource();
auto *SrcPtrVal = MemTrans->getSource();
assert(SrcPtrVal);
auto *SrcAccFunc = SE->getSCEVAtScope(SrcPtrVal, L);
assert(SrcAccFunc);
Expand All @@ -3989,30 +3990,31 @@ bool ScopInfo::buildAccessCallInst(
MemAccInst Inst, Loop *L, Region *R,
const ScopDetection::BoxedLoopsSetTy *BoxedLoops,
const InvariantLoadsSetTy &ScopRIL) {
if (!Inst.isCallInst())
auto *CI = dyn_cast_or_null<CallInst>(Inst);

if (CI == nullptr)
return false;

auto &CI = *Inst.asCallInst();
if (CI.doesNotAccessMemory() || isIgnoredIntrinsic(&CI))
if (CI->doesNotAccessMemory() || isIgnoredIntrinsic(CI))
return true;

bool ReadOnly = false;
auto *AF = SE->getConstant(IntegerType::getInt64Ty(CI.getContext()), 0);
auto *CalledFunction = CI.getCalledFunction();
auto *AF = SE->getConstant(IntegerType::getInt64Ty(CI->getContext()), 0);
auto *CalledFunction = CI->getCalledFunction();
switch (AA->getModRefBehavior(CalledFunction)) {
case llvm::FMRB_UnknownModRefBehavior:
llvm_unreachable("Unknown mod ref behaviour cannot be represented.");
case llvm::FMRB_DoesNotAccessMemory:
return true;
case llvm::FMRB_OnlyReadsMemory:
GlobalReads.push_back(&CI);
GlobalReads.push_back(CI);
return true;
case llvm::FMRB_OnlyReadsArgumentPointees:
ReadOnly = true;
// Fall through
case llvm::FMRB_OnlyAccessesArgumentPointees:
auto AccType = ReadOnly ? MemoryAccess::READ : MemoryAccess::MAY_WRITE;
for (const auto &Arg : CI.arg_operands()) {
for (const auto &Arg : CI->arg_operands()) {
if (!Arg->getType()->isPointerTy())
continue;

Expand All @@ -4022,7 +4024,7 @@ bool ScopInfo::buildAccessCallInst(

auto *ArgBasePtr = cast<SCEVUnknown>(SE->getPointerBase(ArgSCEV));
addArrayAccess(Inst, AccType, ArgBasePtr->getValue(),
ArgBasePtr->getType(), false, {AF}, {}, &CI);
ArgBasePtr->getType(), false, {AF}, {}, CI);
}
return true;
}
Expand All @@ -4038,7 +4040,7 @@ void ScopInfo::buildAccessSingleDim(
Value *Val = Inst.getValueOperand();
Type *ElementType = Val->getType();
enum MemoryAccess::AccessType Type =
Inst.isLoad() ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;
isa<LoadInst>(Inst) ? MemoryAccess::READ : MemoryAccess::MUST_WRITE;

const SCEV *AccessFunction = SE->getSCEVAtScope(Address, L);
const SCEVUnknown *BasePointer =
Expand Down

0 comments on commit 8efb22e

Please sign in to comment.