1 //===- NVPTXLowerAggrCopies.cpp - ------------------------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Lower aggregate copies, memset, memcpy, memmov intrinsics into loops when
10 // the size is large or is not a compile-time constant.
12 //===----------------------------------------------------------------------===//
14 #include "NVPTXLowerAggrCopies.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/InstIterator.h"
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"
25 #include "llvm/Support/Debug.h"
27 #define DEBUG_TYPE "nvptx"
31 namespace llvm { FunctionPass *createLowerAggrCopies(); }
33 char NVPTXLowerAggrCopies::ID = 0;
35 // Lower MemTransferInst or load-store pair to loop
36 static void convertTransferToLoop(
37 Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
39 bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
40 Type *indType = len->getType();
42 BasicBlock *origBB = splitAt->getParent();
43 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
44 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
46 origBB->getTerminator()->setSuccessor(0, loopBB);
47 IRBuilder<> builder(origBB, origBB->getTerminator());
49 // srcAddr and dstAddr are expected to be pointer types,
50 // so no check is made here.
51 unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace();
52 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
54 // Cast pointers to (char *)
55 srcAddr = builder.CreateBitCast(srcAddr, Type::getInt8PtrTy(Context, srcAS));
56 dstAddr = builder.CreateBitCast(dstAddr, Type::getInt8PtrTy(Context, dstAS));
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);
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);
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);
73 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
76 // Lower MemSetInst to loop
77 static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr,
78 Value *len, Value *val, LLVMContext &Context,
80 BasicBlock *origBB = splitAt->getParent();
81 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
82 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
84 origBB->getTerminator()->setSuccessor(0, loopBB);
85 IRBuilder<> builder(origBB, origBB->getTerminator());
87 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
89 // Cast pointer to the type of value getting stored
91 builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
93 IRBuilder<> loop(loopBB);
94 PHINode *ind = loop.CreatePHI(len->getType(), 0);
95 ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB);
97 loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), false);
99 Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1));
100 ind->addIncoming(newind, loopBB);
102 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
105 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
106 SmallVector<LoadInst *, 4> aggrLoads;
107 SmallVector<MemTransferInst *, 4> aggrMemcpys;
108 SmallVector<MemSetInst *, 4> aggrMemsets;
110 const DataLayout &DL = F.getParent()->getDataLayout();
111 LLVMContext &Context = F.getParent()->getContext();
114 // Collect all the aggrLoads, aggrMemcpys and addrMemsets.
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;
121 if (LoadInst *load = dyn_cast<LoadInst>(II)) {
123 if (load->hasOneUse() == false)
126 if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
129 User *use = load->user_back();
130 if (StoreInst *store = dyn_cast<StoreInst>(use)) {
131 if (store->getOperand(0) != load) //getValueOperand
133 aggrLoads.push_back(load);
135 } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
136 Value *len = intr->getLength();
137 // If the number of elements being copied is greater
138 // than MaxAggrCopySize, lower it to a loop
139 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
140 if (len_int->getZExtValue() >= MaxAggrCopySize) {
141 aggrMemcpys.push_back(intr);
144 // turn variable length memcpy/memmov into loop
145 aggrMemcpys.push_back(intr);
147 } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
148 Value *len = memsetintr->getLength();
149 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
150 if (len_int->getZExtValue() >= MaxAggrCopySize) {
151 aggrMemsets.push_back(memsetintr);
154 // turn variable length memset into loop
155 aggrMemsets.push_back(memsetintr);
160 if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
161 (aggrMemsets.size() == 0))
165 // Do the transformation of an aggr load/copy/set to a loop
167 for (unsigned i = 0, e = aggrLoads.size(); i != e; ++i) {
168 LoadInst *load = aggrLoads[i];
169 StoreInst *store = dyn_cast<StoreInst>(*load->user_begin());
170 Value *srcAddr = load->getOperand(0);
171 Value *dstAddr = store->getOperand(1);
172 unsigned numLoads = DL.getTypeStoreSize(load->getType());
173 Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads);
175 convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(),
176 store->isVolatile(), Context, F);
178 store->eraseFromParent();
179 load->eraseFromParent();
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,
191 cpy->eraseFromParent();
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,
200 memsetinst->eraseFromParent();
206 FunctionPass *llvm::createLowerAggrCopies() {
207 return new NVPTXLowerAggrCopies();