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/CodeGen/MachineFunctionAnalysis.h"
16 #include "llvm/CodeGen/StackProtector.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/InstIterator.h"
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"
27 #include "llvm/Support/Debug.h"
29 #define DEBUG_TYPE "nvptx"
34 // actual analysis class, which is a functionpass
35 struct NVPTXLowerAggrCopies : public FunctionPass {
38 NVPTXLowerAggrCopies() : FunctionPass(ID) {}
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.addPreserved<MachineFunctionAnalysis>();
42 AU.addPreserved<StackProtector>();
45 bool runOnFunction(Function &F) override;
47 static const unsigned MaxAggrCopySize = 128;
49 const char *getPassName() const override {
50 return "Lower aggregate copies/intrinsics into loops";
55 char NVPTXLowerAggrCopies::ID = 0;
57 // Lower MemTransferInst or load-store pair to loop
58 static void convertTransferToLoop(
59 Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
60 bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
61 Type *indType = len->getType();
63 BasicBlock *origBB = splitAt->getParent();
64 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
65 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
67 origBB->getTerminator()->setSuccessor(0, loopBB);
68 IRBuilder<> builder(origBB, origBB->getTerminator());
70 // srcAddr and dstAddr are expected to be pointer types,
71 // so no check is made here.
72 unsigned srcAS = cast<PointerType>(srcAddr->getType())->getAddressSpace();
73 unsigned dstAS = cast<PointerType>(dstAddr->getType())->getAddressSpace();
75 // Cast pointers to (char *)
76 srcAddr = builder.CreateBitCast(srcAddr, Type::getInt8PtrTy(Context, srcAS));
77 dstAddr = builder.CreateBitCast(dstAddr, Type::getInt8PtrTy(Context, dstAS));
79 IRBuilder<> loop(loopBB);
80 // The loop index (ind) is a phi node.
81 PHINode *ind = loop.CreatePHI(indType, 0);
82 // Incoming value for ind is 0
83 ind->addIncoming(ConstantInt::get(indType, 0), origBB);
85 // load from srcAddr+ind
86 // TODO: we can leverage the align parameter of llvm.memcpy for more efficient
87 // word-sized loads and stores.
88 Value *val = loop.CreateLoad(loop.CreateGEP(loop.getInt8Ty(), srcAddr, ind),
90 // store at dstAddr+ind
91 loop.CreateStore(val, loop.CreateGEP(loop.getInt8Ty(), dstAddr, ind),
94 // The value for ind coming from backedge is (ind + 1)
95 Value *newind = loop.CreateAdd(ind, ConstantInt::get(indType, 1));
96 ind->addIncoming(newind, loopBB);
98 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
101 // Lower MemSetInst to loop
102 static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr,
103 Value *len, Value *val, LLVMContext &Context,
105 BasicBlock *origBB = splitAt->getParent();
106 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
107 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
109 origBB->getTerminator()->setSuccessor(0, loopBB);
110 IRBuilder<> builder(origBB, origBB->getTerminator());
112 unsigned dstAS = cast<PointerType>(dstAddr->getType())->getAddressSpace();
114 // Cast pointer to the type of value getting stored
116 builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
118 IRBuilder<> loop(loopBB);
119 PHINode *ind = loop.CreatePHI(len->getType(), 0);
120 ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB);
122 loop.CreateStore(val, loop.CreateGEP(val->getType(), dstAddr, ind), false);
124 Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1));
125 ind->addIncoming(newind, loopBB);
127 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
130 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
131 SmallVector<LoadInst *, 4> aggrLoads;
132 SmallVector<MemTransferInst *, 4> aggrMemcpys;
133 SmallVector<MemSetInst *, 4> aggrMemsets;
135 const DataLayout &DL = F.getParent()->getDataLayout();
136 LLVMContext &Context = F.getParent()->getContext();
139 // Collect all the aggrLoads, aggrMemcpys and addrMemsets.
141 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
142 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
144 if (LoadInst *load = dyn_cast<LoadInst>(II)) {
145 if (!load->hasOneUse())
148 if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
151 User *use = load->user_back();
152 if (StoreInst *store = dyn_cast<StoreInst>(use)) {
153 if (store->getOperand(0) != load)
155 aggrLoads.push_back(load);
157 } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
158 Value *len = intr->getLength();
159 // If the number of elements being copied is greater
160 // than MaxAggrCopySize, lower it to a loop
161 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
162 if (len_int->getZExtValue() >= MaxAggrCopySize) {
163 aggrMemcpys.push_back(intr);
166 // turn variable length memcpy/memmov into loop
167 aggrMemcpys.push_back(intr);
169 } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
170 Value *len = memsetintr->getLength();
171 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
172 if (len_int->getZExtValue() >= MaxAggrCopySize) {
173 aggrMemsets.push_back(memsetintr);
176 // turn variable length memset into loop
177 aggrMemsets.push_back(memsetintr);
182 if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
183 (aggrMemsets.size() == 0))
187 // Do the transformation of an aggr load/copy/set to a loop
189 for (LoadInst *load : aggrLoads) {
190 StoreInst *store = dyn_cast<StoreInst>(*load->user_begin());
191 Value *srcAddr = load->getOperand(0);
192 Value *dstAddr = store->getOperand(1);
193 unsigned numLoads = DL.getTypeStoreSize(load->getType());
194 Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads);
196 convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(),
197 store->isVolatile(), Context, F);
199 store->eraseFromParent();
200 load->eraseFromParent();
203 for (MemTransferInst *cpy : aggrMemcpys) {
204 convertTransferToLoop(/* splitAt */ cpy,
205 /* srcAddr */ cpy->getSource(),
206 /* dstAddr */ cpy->getDest(),
207 /* len */ cpy->getLength(),
208 /* srcVolatile */ cpy->isVolatile(),
209 /* dstVolatile */ cpy->isVolatile(),
210 /* Context */ Context,
212 cpy->eraseFromParent();
215 for (MemSetInst *memsetinst : aggrMemsets) {
216 Value *len = memsetinst->getLength();
217 Value *val = memsetinst->getValue();
218 convertMemSetToLoop(memsetinst, memsetinst->getDest(), len, val, Context,
220 memsetinst->eraseFromParent();
226 FunctionPass *llvm::createLowerAggrCopies() {
227 return new NVPTXLowerAggrCopies();