Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 1 | //===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when |
| 10 | // the size is large or is not a compile-time constant. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 14 | #include "NVPTXLowerAggrCopies.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/Function.h" |
| 18 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 19 | #include "llvm/IR/InstIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Intrinsics.h" |
| 23 | #include "llvm/IR/LLVMContext.h" |
| 24 | #include "llvm/IR/Module.h" |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/Debug.h" |
| 26 | |
| 27 | #define DEBUG_TYPE "nvptx" |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 31 | namespace llvm { FunctionPass *createLowerAggrCopies(); } |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 32 | |
| 33 | char NVPTXLowerAggrCopies::ID = 0; |
| 34 | |
| 35 | // Lower MemTransferInst or load-store pair to loop |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 36 | static void convertTransferToLoop( |
| 37 | Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len, |
| 38 | //unsigned numLoads, |
| 39 | bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 40 | Type *indType = len->getType(); |
| 41 | |
| 42 | BasicBlock *origBB = splitAt->getParent(); |
| 43 | BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split"); |
| 44 | BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB); |
| 45 | |
| 46 | origBB->getTerminator()->setSuccessor(0, loopBB); |
| 47 | IRBuilder<> builder(origBB, origBB->getTerminator()); |
| 48 | |
| 49 | // srcAddr and dstAddr are expected to be pointer types, |
| 50 | // so no check is made here. |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 51 | unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace(); |
| 52 | unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 53 | |
| 54 | // Cast pointers to (char *) |
| 55 | srcAddr = builder.CreateBitCast(srcAddr, Type::getInt8PtrTy(Context, srcAS)); |
| 56 | dstAddr = builder.CreateBitCast(dstAddr, Type::getInt8PtrTy(Context, dstAS)); |
| 57 | |
| 58 | IRBuilder<> loop(loopBB); |
| 59 | // The loop index (ind) is a phi node. |
| 60 | PHINode *ind = loop.CreatePHI(indType, 0); |
| 61 | // Incoming value for ind is 0 |
| 62 | ind->addIncoming(ConstantInt::get(indType, 0), origBB); |
| 63 | |
| 64 | // load from srcAddr+ind |
| 65 | Value *val = loop.CreateLoad(loop.CreateGEP(srcAddr, ind), srcVolatile); |
| 66 | // store at dstAddr+ind |
| 67 | loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), dstVolatile); |
| 68 | |
| 69 | // The value for ind coming from backedge is (ind + 1) |
| 70 | Value *newind = loop.CreateAdd(ind, ConstantInt::get(indType, 1)); |
| 71 | ind->addIncoming(newind, loopBB); |
| 72 | |
| 73 | loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB); |
| 74 | } |
| 75 | |
| 76 | // Lower MemSetInst to loop |
| 77 | static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr, |
| 78 | Value *len, Value *val, LLVMContext &Context, |
| 79 | Function &F) { |
| 80 | BasicBlock *origBB = splitAt->getParent(); |
| 81 | BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split"); |
| 82 | BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB); |
| 83 | |
| 84 | origBB->getTerminator()->setSuccessor(0, loopBB); |
| 85 | IRBuilder<> builder(origBB, origBB->getTerminator()); |
| 86 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 87 | unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 88 | |
| 89 | // Cast pointer to the type of value getting stored |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 90 | dstAddr = |
| 91 | builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS)); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 92 | |
| 93 | IRBuilder<> loop(loopBB); |
| 94 | PHINode *ind = loop.CreatePHI(len->getType(), 0); |
| 95 | ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB); |
| 96 | |
| 97 | loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), false); |
| 98 | |
| 99 | Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1)); |
| 100 | ind->addIncoming(newind, loopBB); |
| 101 | |
| 102 | loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB); |
| 103 | } |
| 104 | |
| 105 | bool NVPTXLowerAggrCopies::runOnFunction(Function &F) { |
| 106 | SmallVector<LoadInst *, 4> aggrLoads; |
| 107 | SmallVector<MemTransferInst *, 4> aggrMemcpys; |
| 108 | SmallVector<MemSetInst *, 4> aggrMemsets; |
| 109 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame^] | 110 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 111 | LLVMContext &Context = F.getParent()->getContext(); |
| 112 | |
| 113 | // |
| 114 | // Collect all the aggrLoads, aggrMemcpys and addrMemsets. |
| 115 | // |
| 116 | //const BasicBlock *firstBB = &F.front(); // first BB in F |
| 117 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { |
| 118 | //BasicBlock *bb = BI; |
| 119 | for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 120 | ++II) { |
| 121 | if (LoadInst *load = dyn_cast<LoadInst>(II)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 122 | |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 123 | if (load->hasOneUse() == false) |
| 124 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 125 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame^] | 126 | if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize) |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 127 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 128 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 129 | User *use = load->user_back(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 130 | if (StoreInst *store = dyn_cast<StoreInst>(use)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 131 | if (store->getOperand(0) != load) //getValueOperand |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 132 | continue; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 133 | aggrLoads.push_back(load); |
| 134 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 135 | } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 136 | Value *len = intr->getLength(); |
| 137 | // If the number of elements being copied is greater |
| 138 | // than MaxAggrCopySize, lower it to a loop |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 139 | if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 140 | if (len_int->getZExtValue() >= MaxAggrCopySize) { |
| 141 | aggrMemcpys.push_back(intr); |
| 142 | } |
| 143 | } else { |
| 144 | // turn variable length memcpy/memmov into loop |
| 145 | aggrMemcpys.push_back(intr); |
| 146 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 147 | } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 148 | Value *len = memsetintr->getLength(); |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 149 | if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) { |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 150 | if (len_int->getZExtValue() >= MaxAggrCopySize) { |
| 151 | aggrMemsets.push_back(memsetintr); |
| 152 | } |
| 153 | } else { |
| 154 | // turn variable length memset into loop |
| 155 | aggrMemsets.push_back(memsetintr); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
Justin Holewinski | 0497ab1 | 2013-03-30 14:29:21 +0000 | [diff] [blame] | 160 | if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) && |
| 161 | (aggrMemsets.size() == 0)) |
| 162 | return false; |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 163 | |
| 164 | // |
| 165 | // Do the transformation of an aggr load/copy/set to a loop |
| 166 | // |
| 167 | for (unsigned i = 0, e = aggrLoads.size(); i != e; ++i) { |
| 168 | LoadInst *load = aggrLoads[i]; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 169 | StoreInst *store = dyn_cast<StoreInst>(*load->user_begin()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 170 | Value *srcAddr = load->getOperand(0); |
| 171 | Value *dstAddr = store->getOperand(1); |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame^] | 172 | unsigned numLoads = DL.getTypeStoreSize(load->getType()); |
Justin Holewinski | ae556d3 | 2012-05-04 20:18:50 +0000 | [diff] [blame] | 173 | Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads); |
| 174 | |
| 175 | convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(), |
| 176 | store->isVolatile(), Context, F); |
| 177 | |
| 178 | store->eraseFromParent(); |
| 179 | load->eraseFromParent(); |
| 180 | } |
| 181 | |
| 182 | for (unsigned i = 0, e = aggrMemcpys.size(); i != e; ++i) { |
| 183 | MemTransferInst *cpy = aggrMemcpys[i]; |
| 184 | Value *len = cpy->getLength(); |
| 185 | // llvm 2.7 version of memcpy does not have volatile |
| 186 | // operand yet. So always making it non-volatile |
| 187 | // optimistically, so that we don't see unnecessary |
| 188 | // st.volatile in ptx |
| 189 | convertTransferToLoop(cpy, cpy->getSource(), cpy->getDest(), len, false, |
| 190 | false, Context, F); |
| 191 | cpy->eraseFromParent(); |
| 192 | } |
| 193 | |
| 194 | for (unsigned i = 0, e = aggrMemsets.size(); i != e; ++i) { |
| 195 | MemSetInst *memsetinst = aggrMemsets[i]; |
| 196 | Value *len = memsetinst->getLength(); |
| 197 | Value *val = memsetinst->getValue(); |
| 198 | convertMemSetToLoop(memsetinst, memsetinst->getDest(), len, val, Context, |
| 199 | F); |
| 200 | memsetinst->eraseFromParent(); |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | FunctionPass *llvm::createLowerAggrCopies() { |
| 207 | return new NVPTXLowerAggrCopies(); |
| 208 | } |