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 = cast<PointerType>(srcAddr->getType())->getAddressSpace();
00074   unsigned dstAS = 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(loop.getInt8Ty(), srcAddr, ind),
00088                                srcVolatile);
00089   // store at dstAddr+ind
00090   loop.CreateStore(val, loop.CreateGEP(loop.getInt8Ty(), dstAddr, ind),
00091                    dstVolatile);
00092 
00093   // The value for ind coming from backedge is (ind + 1)
00094   Value *newind = loop.CreateAdd(ind, ConstantInt::get(indType, 1));
00095   ind->addIncoming(newind, loopBB);
00096 
00097   loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
00098 }
00099 
00100 // Lower MemSetInst to loop
00101 static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr,
00102                                 Value *len, Value *val, LLVMContext &Context,
00103                                 Function &F) {
00104   BasicBlock *origBB = splitAt->getParent();
00105   BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
00106   BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
00107 
00108   origBB->getTerminator()->setSuccessor(0, loopBB);
00109   IRBuilder<> builder(origBB, origBB->getTerminator());
00110 
00111   unsigned dstAS = cast<PointerType>(dstAddr->getType())->getAddressSpace();
00112 
00113   // Cast pointer to the type of value getting stored
00114   dstAddr =
00115       builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
00116 
00117   IRBuilder<> loop(loopBB);
00118   PHINode *ind = loop.CreatePHI(len->getType(), 0);
00119   ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB);
00120 
00121   loop.CreateStore(val, loop.CreateGEP(val->getType(), dstAddr, ind), false);
00122 
00123   Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1));
00124   ind->addIncoming(newind, loopBB);
00125 
00126   loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
00127 }
00128 
00129 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
00130   SmallVector<LoadInst *, 4> aggrLoads;
00131   SmallVector<MemTransferInst *, 4> aggrMemcpys;
00132   SmallVector<MemSetInst *, 4> aggrMemsets;
00133 
00134   const DataLayout &DL = F.getParent()->getDataLayout();
00135   LLVMContext &Context = F.getParent()->getContext();
00136 
00137   //
00138   // Collect all the aggrLoads, aggrMemcpys and addrMemsets.
00139   //
00140   //const BasicBlock *firstBB = &F.front();  // first BB in F
00141   for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
00142     //BasicBlock *bb = BI;
00143     for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
00144          ++II) {
00145       if (LoadInst *load = dyn_cast<LoadInst>(II)) {
00146 
00147         if (!load->hasOneUse())
00148           continue;
00149 
00150         if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
00151           continue;
00152 
00153         User *use = load->user_back();
00154         if (StoreInst *store = dyn_cast<StoreInst>(use)) {
00155           if (store->getOperand(0) != load) //getValueOperand
00156             continue;
00157           aggrLoads.push_back(load);
00158         }
00159       } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
00160         Value *len = intr->getLength();
00161         // If the number of elements being copied is greater
00162         // than MaxAggrCopySize, lower it to a loop
00163         if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
00164           if (len_int->getZExtValue() >= MaxAggrCopySize) {
00165             aggrMemcpys.push_back(intr);
00166           }
00167         } else {
00168           // turn variable length memcpy/memmov into loop
00169           aggrMemcpys.push_back(intr);
00170         }
00171       } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
00172         Value *len = memsetintr->getLength();
00173         if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
00174           if (len_int->getZExtValue() >= MaxAggrCopySize) {
00175             aggrMemsets.push_back(memsetintr);
00176           }
00177         } else {
00178           // turn variable length memset into loop
00179           aggrMemsets.push_back(memsetintr);
00180         }
00181       }
00182     }
00183   }
00184   if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
00185       (aggrMemsets.size() == 0))
00186     return false;
00187 
00188   //
00189   // Do the transformation of an aggr load/copy/set to a loop
00190   //
00191   for (unsigned i = 0, e = aggrLoads.size(); i != e; ++i) {
00192     LoadInst *load = aggrLoads[i];
00193     StoreInst *store = dyn_cast<StoreInst>(*load->user_begin());
00194     Value *srcAddr = load->getOperand(0);
00195     Value *dstAddr = store->getOperand(1);
00196     unsigned numLoads = DL.getTypeStoreSize(load->getType());
00197     Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads);
00198 
00199     convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(),
00200                           store->isVolatile(), Context, F);
00201 
00202     store->eraseFromParent();
00203     load->eraseFromParent();
00204   }
00205 
00206   for (unsigned i = 0, e = aggrMemcpys.size(); i != e; ++i) {
00207     MemTransferInst *cpy = aggrMemcpys[i];
00208     Value *len = cpy->getLength();
00209     // llvm 2.7 version of memcpy does not have volatile
00210     // operand yet. So always making it non-volatile
00211     // optimistically, so that we don't see unnecessary
00212     // st.volatile in ptx
00213     convertTransferToLoop(cpy, cpy->getSource(), cpy->getDest(), len, false,
00214                           false, Context, F);
00215     cpy->eraseFromParent();
00216   }
00217 
00218   for (unsigned i = 0, e = aggrMemsets.size(); i != e; ++i) {
00219     MemSetInst *memsetinst = aggrMemsets[i];
00220     Value *len = memsetinst->getLength();
00221     Value *val = memsetinst->getValue();
00222     convertMemSetToLoop(memsetinst, memsetinst->getDest(), len, val, Context,
00223                         F);
00224     memsetinst->eraseFromParent();
00225   }
00226 
00227   return true;
00228 }
00229 
00230 FunctionPass *llvm::createLowerAggrCopies() {
00231   return new NVPTXLowerAggrCopies();
00232 }