LLVM  mainline
NVPTXLowerAggrCopies.cpp
Go to the documentation of this file.
00001 //===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when
00010 // the size is large or is not a compile-time constant.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "NVPTXLowerAggrCopies.h"
00015 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
00016 #include "llvm/CodeGen/StackProtector.h"
00017 #include "llvm/IR/Constants.h"
00018 #include "llvm/IR/DataLayout.h"
00019 #include "llvm/IR/Function.h"
00020 #include "llvm/IR/IRBuilder.h"
00021 #include "llvm/IR/InstIterator.h"
00022 #include "llvm/IR/Instructions.h"
00023 #include "llvm/IR/IntrinsicInst.h"
00024 #include "llvm/IR/Intrinsics.h"
00025 #include "llvm/IR/LLVMContext.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/Support/Debug.h"
00028 
00029 #define DEBUG_TYPE "nvptx"
00030 
00031 using namespace llvm;
00032 
00033 namespace {
00034 // actual analysis class, which is a functionpass
00035 struct NVPTXLowerAggrCopies : public FunctionPass {
00036   static char ID;
00037 
00038   NVPTXLowerAggrCopies() : FunctionPass(ID) {}
00039 
00040   void getAnalysisUsage(AnalysisUsage &AU) const override {
00041     AU.addPreserved<MachineFunctionAnalysis>();
00042     AU.addPreserved<StackProtector>();
00043   }
00044 
00045   bool runOnFunction(Function &F) override;
00046 
00047   static const unsigned MaxAggrCopySize = 128;
00048 
00049   const char *getPassName() const override {
00050     return "Lower aggregate copies/intrinsics into loops";
00051   }
00052 };
00053 } // namespace
00054 
00055 char NVPTXLowerAggrCopies::ID = 0;
00056 
00057 // Lower MemTransferInst or load-store pair to loop
00058 static void convertTransferToLoop(
00059     Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
00060     //unsigned numLoads,
00061     bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
00062   Type *indType = len->getType();
00063 
00064   BasicBlock *origBB = splitAt->getParent();
00065   BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
00066   BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
00067 
00068   origBB->getTerminator()->setSuccessor(0, loopBB);
00069   IRBuilder<> builder(origBB, origBB->getTerminator());
00070 
00071   // srcAddr and dstAddr are expected to be pointer types,
00072   // so no check is made here.
00073   unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace();
00074   unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
00075 
00076   // Cast pointers to (char *)
00077   srcAddr = builder.CreateBitCast(srcAddr, Type::getInt8PtrTy(Context, srcAS));
00078   dstAddr = builder.CreateBitCast(dstAddr, Type::getInt8PtrTy(Context, dstAS));
00079 
00080   IRBuilder<> loop(loopBB);
00081   // The loop index (ind) is a phi node.
00082   PHINode *ind = loop.CreatePHI(indType, 0);
00083   // Incoming value for ind is 0
00084   ind->addIncoming(ConstantInt::get(indType, 0), origBB);
00085 
00086   // load from srcAddr+ind
00087   Value *val = loop.CreateLoad(loop.CreateGEP(srcAddr, ind), srcVolatile);
00088   // store at dstAddr+ind
00089   loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), dstVolatile);
00090 
00091   // The value for ind coming from backedge is (ind + 1)
00092   Value *newind = loop.CreateAdd(ind, ConstantInt::get(indType, 1));
00093   ind->addIncoming(newind, loopBB);
00094 
00095   loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
00096 }
00097 
00098 // Lower MemSetInst to loop
00099 static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr,
00100                                 Value *len, Value *val, LLVMContext &Context,
00101                                 Function &F) {
00102   BasicBlock *origBB = splitAt->getParent();
00103   BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
00104   BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
00105 
00106   origBB->getTerminator()->setSuccessor(0, loopBB);
00107   IRBuilder<> builder(origBB, origBB->getTerminator());
00108 
00109   unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
00110 
00111   // Cast pointer to the type of value getting stored
00112   dstAddr =
00113       builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
00114 
00115   IRBuilder<> loop(loopBB);
00116   PHINode *ind = loop.CreatePHI(len->getType(), 0);
00117   ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB);
00118 
00119   loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), false);
00120 
00121   Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1));
00122   ind->addIncoming(newind, loopBB);
00123 
00124   loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
00125 }
00126 
00127 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
00128   SmallVector<LoadInst *, 4> aggrLoads;
00129   SmallVector<MemTransferInst *, 4> aggrMemcpys;
00130   SmallVector<MemSetInst *, 4> aggrMemsets;
00131 
00132   const DataLayout &DL = F.getParent()->getDataLayout();
00133   LLVMContext &Context = F.getParent()->getContext();
00134 
00135   //
00136   // Collect all the aggrLoads, aggrMemcpys and addrMemsets.
00137   //
00138   //const BasicBlock *firstBB = &F.front();  // first BB in F
00139   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
00140     //BasicBlock *bb = BI;
00141     for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
00142          ++II) {
00143       if (LoadInst *load = dyn_cast<LoadInst>(II)) {
00144 
00145         if (!load->hasOneUse())
00146           continue;
00147 
00148         if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
00149           continue;
00150 
00151         User *use = load->user_back();
00152         if (StoreInst *store = dyn_cast<StoreInst>(use)) {
00153           if (store->getOperand(0) != load) //getValueOperand
00154             continue;
00155           aggrLoads.push_back(load);
00156         }
00157       } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
00158         Value *len = intr->getLength();
00159         // If the number of elements being copied is greater
00160         // than MaxAggrCopySize, lower it to a loop
00161         if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
00162           if (len_int->getZExtValue() >= MaxAggrCopySize) {
00163             aggrMemcpys.push_back(intr);
00164           }
00165         } else {
00166           // turn variable length memcpy/memmov into loop
00167           aggrMemcpys.push_back(intr);
00168         }
00169       } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
00170         Value *len = memsetintr->getLength();
00171         if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
00172           if (len_int->getZExtValue() >= MaxAggrCopySize) {
00173             aggrMemsets.push_back(memsetintr);
00174           }
00175         } else {
00176           // turn variable length memset into loop
00177           aggrMemsets.push_back(memsetintr);
00178         }
00179       }
00180     }
00181   }
00182   if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
00183       (aggrMemsets.size() == 0))
00184     return false;
00185 
00186   //
00187   // Do the transformation of an aggr load/copy/set to a loop
00188   //
00189   for (unsigned i = 0, e = aggrLoads.size(); i != e; ++i) {
00190     LoadInst *load = aggrLoads[i];
00191     StoreInst *store = dyn_cast<StoreInst>(*load->user_begin());
00192     Value *srcAddr = load->getOperand(0);
00193     Value *dstAddr = store->getOperand(1);
00194     unsigned numLoads = DL.getTypeStoreSize(load->getType());
00195     Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads);
00196 
00197     convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(),
00198                           store->isVolatile(), Context, F);
00199 
00200     store->eraseFromParent();
00201     load->eraseFromParent();
00202   }
00203 
00204   for (unsigned i = 0, e = aggrMemcpys.size(); i != e; ++i) {
00205     MemTransferInst *cpy = aggrMemcpys[i];
00206     Value *len = cpy->getLength();
00207     // llvm 2.7 version of memcpy does not have volatile
00208     // operand yet. So always making it non-volatile
00209     // optimistically, so that we don't see unnecessary
00210     // st.volatile in ptx
00211     convertTransferToLoop(cpy, cpy->getSource(), cpy->getDest(), len, false,
00212                           false, Context, F);
00213     cpy->eraseFromParent();
00214   }
00215 
00216   for (unsigned i = 0, e = aggrMemsets.size(); i != e; ++i) {
00217     MemSetInst *memsetinst = aggrMemsets[i];
00218     Value *len = memsetinst->getLength();
00219     Value *val = memsetinst->getValue();
00220     convertMemSetToLoop(memsetinst, memsetinst->getDest(), len, val, Context,
00221                         F);
00222     memsetinst->eraseFromParent();
00223   }
00224 
00225   return true;
00226 }
00227 
00228 FunctionPass *llvm::createLowerAggrCopies() {
00229   return new NVPTXLowerAggrCopies();
00230 }