2012-05-05 04:18:50 +08:00
|
|
|
//===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-07-17 00:27:19 +08:00
|
|
|
//
|
2015-07-17 04:42:38 +08:00
|
|
|
// \file
|
2012-05-05 04:18:50 +08:00
|
|
|
// Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when
|
|
|
|
// the size is large or is not a compile-time constant.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-29 20:38:19 +08:00
|
|
|
#include "NVPTXLowerAggrCopies.h"
|
2017-07-07 10:00:06 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2015-03-09 23:50:58 +08:00
|
|
|
#include "llvm/CodeGen/StackProtector.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-07-17 00:27:19 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2017-02-09 01:49:52 +08:00
|
|
|
#include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "nvptx"
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-09 23:50:58 +08:00
|
|
|
namespace {
|
2015-07-17 00:27:19 +08:00
|
|
|
|
2015-03-09 23:50:58 +08:00
|
|
|
// actual analysis class, which is a functionpass
|
|
|
|
struct NVPTXLowerAggrCopies : public FunctionPass {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
NVPTXLowerAggrCopies() : FunctionPass(ID) {}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addPreserved<StackProtector>();
|
2017-07-07 10:00:06 +08:00
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
2015-03-09 23:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
|
|
|
|
static const unsigned MaxAggrCopySize = 128;
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2015-03-09 23:50:58 +08:00
|
|
|
return "Lower aggregate copies/intrinsics into loops";
|
|
|
|
}
|
|
|
|
};
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
char NVPTXLowerAggrCopies::ID = 0;
|
|
|
|
|
|
|
|
bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
|
2015-07-17 04:42:38 +08:00
|
|
|
SmallVector<LoadInst *, 4> AggrLoads;
|
2015-07-17 00:27:19 +08:00
|
|
|
SmallVector<MemIntrinsic *, 4> MemCalls;
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2015-03-05 02:43:29 +08:00
|
|
|
const DataLayout &DL = F.getParent()->getDataLayout();
|
2012-05-05 04:18:50 +08:00
|
|
|
LLVMContext &Context = F.getParent()->getContext();
|
2017-07-07 10:00:06 +08:00
|
|
|
const TargetTransformInfo &TTI =
|
|
|
|
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2015-07-17 00:27:19 +08:00
|
|
|
// Collect all aggregate loads and mem* calls.
|
2012-05-05 04:18:50 +08:00
|
|
|
for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
|
|
|
|
for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
|
2013-03-30 22:29:21 +08:00
|
|
|
++II) {
|
2015-07-17 04:42:38 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(II)) {
|
|
|
|
if (!LI->hasOneUse())
|
2013-03-30 22:29:21 +08:00
|
|
|
continue;
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2015-07-17 04:42:38 +08:00
|
|
|
if (DL.getTypeStoreSize(LI->getType()) < MaxAggrCopySize)
|
2013-03-30 22:29:21 +08:00
|
|
|
continue;
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2015-07-17 04:42:38 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(LI->user_back())) {
|
|
|
|
if (SI->getOperand(0) != LI)
|
2013-03-30 22:29:21 +08:00
|
|
|
continue;
|
2015-07-17 04:42:38 +08:00
|
|
|
AggrLoads.push_back(LI);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
2015-07-17 00:27:19 +08:00
|
|
|
} else if (MemIntrinsic *IntrCall = dyn_cast<MemIntrinsic>(II)) {
|
|
|
|
// Convert intrinsic calls with variable size or with constant size
|
|
|
|
// larger than the MaxAggrCopySize threshold.
|
|
|
|
if (ConstantInt *LenCI = dyn_cast<ConstantInt>(IntrCall->getLength())) {
|
|
|
|
if (LenCI->getZExtValue() >= MaxAggrCopySize) {
|
|
|
|
MemCalls.push_back(IntrCall);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-17 00:27:19 +08:00
|
|
|
MemCalls.push_back(IntrCall);
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-17 00:27:19 +08:00
|
|
|
|
2015-07-17 04:42:38 +08:00
|
|
|
if (AggrLoads.size() == 0 && MemCalls.size() == 0) {
|
2013-03-30 22:29:21 +08:00
|
|
|
return false;
|
2015-07-17 00:27:19 +08:00
|
|
|
}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Do the transformation of an aggr load/copy/set to a loop
|
|
|
|
//
|
2015-07-17 04:42:38 +08:00
|
|
|
for (LoadInst *LI : AggrLoads) {
|
|
|
|
StoreInst *SI = dyn_cast<StoreInst>(*LI->user_begin());
|
|
|
|
Value *SrcAddr = LI->getOperand(0);
|
|
|
|
Value *DstAddr = SI->getOperand(1);
|
|
|
|
unsigned NumLoads = DL.getTypeStoreSize(LI->getType());
|
2017-07-07 10:00:06 +08:00
|
|
|
ConstantInt *CopyLen =
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), NumLoads);
|
|
|
|
|
|
|
|
if (!TTI.useWideIRMemcpyLoopLowering()) {
|
|
|
|
createMemCpyLoop(/* ConvertedInst */ SI,
|
|
|
|
/* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
|
|
|
|
/* CopyLen */ CopyLen,
|
|
|
|
/* SrcAlign */ LI->getAlignment(),
|
|
|
|
/* DestAlign */ SI->getAlignment(),
|
|
|
|
/* SrcIsVolatile */ LI->isVolatile(),
|
|
|
|
/* DstIsVolatile */ SI->isVolatile());
|
|
|
|
} else {
|
|
|
|
createMemCpyLoopKnownSize(/* ConvertedInst */ SI,
|
|
|
|
/* SrcAddr */ SrcAddr, /* DstAddr */ DstAddr,
|
|
|
|
/* CopyLen */ CopyLen,
|
|
|
|
/* SrcAlign */ LI->getAlignment(),
|
|
|
|
/* DestAlign */ SI->getAlignment(),
|
|
|
|
/* SrcIsVolatile */ LI->isVolatile(),
|
|
|
|
/* DstIsVolatile */ SI->isVolatile(), TTI);
|
|
|
|
}
|
2015-07-17 04:42:38 +08:00
|
|
|
|
|
|
|
SI->eraseFromParent();
|
|
|
|
LI->eraseFromParent();
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2015-07-17 00:27:19 +08:00
|
|
|
// Transform mem* intrinsic calls.
|
|
|
|
for (MemIntrinsic *MemCall : MemCalls) {
|
|
|
|
if (MemCpyInst *Memcpy = dyn_cast<MemCpyInst>(MemCall)) {
|
2017-07-07 10:00:06 +08:00
|
|
|
expandMemCpyAsLoop(Memcpy, TTI);
|
2015-07-17 00:27:19 +08:00
|
|
|
} else if (MemMoveInst *Memmove = dyn_cast<MemMoveInst>(MemCall)) {
|
2017-02-09 01:49:52 +08:00
|
|
|
expandMemMoveAsLoop(Memmove);
|
2015-07-17 00:27:19 +08:00
|
|
|
} else if (MemSetInst *Memset = dyn_cast<MemSetInst>(MemCall)) {
|
2017-02-09 01:49:52 +08:00
|
|
|
expandMemSetAsLoop(Memset);
|
2015-07-17 00:27:19 +08:00
|
|
|
}
|
|
|
|
MemCall->eraseFromParent();
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-17 00:27:19 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
void initializeNVPTXLowerAggrCopiesPass(PassRegistry &);
|
|
|
|
}
|
|
|
|
|
|
|
|
INITIALIZE_PASS(NVPTXLowerAggrCopies, "nvptx-lower-aggr-copies",
|
|
|
|
"Lower aggregate copies, and llvm.mem* intrinsics into loops",
|
|
|
|
false, false)
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
FunctionPass *llvm::createLowerAggrCopies() {
|
|
|
|
return new NVPTXLowerAggrCopies();
|
|
|
|
}
|