LLVM API Documentation

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