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