blob: f6b62b762cfb71bfebca55f62927db21db09cfab [file] [log] [blame]
Justin Holewinskiae556d32012-05-04 20:18:50 +00001//===- 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 Holewinskiae556d32012-05-04 20:18:50 +000014#include "NVPTXLowerAggrCopies.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/IRBuilder.h"
Chandler Carruth83948572014-03-04 10:30:26 +000019#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#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 Amini46a43552015-03-04 18:43:29 +000025#include "llvm/Support/Debug.h"
26
27#define DEBUG_TYPE "nvptx"
Justin Holewinskiae556d32012-05-04 20:18:50 +000028
29using namespace llvm;
30
Justin Holewinski0497ab12013-03-30 14:29:21 +000031namespace llvm { FunctionPass *createLowerAggrCopies(); }
Justin Holewinskiae556d32012-05-04 20:18:50 +000032
33char NVPTXLowerAggrCopies::ID = 0;
34
35// Lower MemTransferInst or load-store pair to loop
Justin Holewinski0497ab12013-03-30 14:29:21 +000036static void convertTransferToLoop(
37 Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
38 //unsigned numLoads,
39 bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
Justin Holewinskiae556d32012-05-04 20:18:50 +000040 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 Holewinski0497ab12013-03-30 14:29:21 +000051 unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace();
52 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
Justin Holewinskiae556d32012-05-04 20:18:50 +000053
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
77static 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 Holewinski0497ab12013-03-30 14:29:21 +000087 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
Justin Holewinskiae556d32012-05-04 20:18:50 +000088
89 // Cast pointer to the type of value getting stored
Justin Holewinski0497ab12013-03-30 14:29:21 +000090 dstAddr =
91 builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
Justin Holewinskiae556d32012-05-04 20:18:50 +000092
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
105bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
106 SmallVector<LoadInst *, 4> aggrLoads;
107 SmallVector<MemTransferInst *, 4> aggrMemcpys;
108 SmallVector<MemSetInst *, 4> aggrMemsets;
109
Mehdi Amini46a43552015-03-04 18:43:29 +0000110 const DataLayout &DL = F.getParent()->getDataLayout();
Justin Holewinskiae556d32012-05-04 20:18:50 +0000111 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 Holewinski0497ab12013-03-30 14:29:21 +0000120 ++II) {
121 if (LoadInst *load = dyn_cast<LoadInst>(II)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000122
Justin Holewinski0497ab12013-03-30 14:29:21 +0000123 if (load->hasOneUse() == false)
124 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000125
Mehdi Amini46a43552015-03-04 18:43:29 +0000126 if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
Justin Holewinski0497ab12013-03-30 14:29:21 +0000127 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000128
Chandler Carruthcdf47882014-03-09 03:16:01 +0000129 User *use = load->user_back();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000130 if (StoreInst *store = dyn_cast<StoreInst>(use)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000131 if (store->getOperand(0) != load) //getValueOperand
Justin Holewinski0497ab12013-03-30 14:29:21 +0000132 continue;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000133 aggrLoads.push_back(load);
134 }
Justin Holewinski0497ab12013-03-30 14:29:21 +0000135 } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000136 Value *len = intr->getLength();
137 // If the number of elements being copied is greater
138 // than MaxAggrCopySize, lower it to a loop
Justin Holewinski0497ab12013-03-30 14:29:21 +0000139 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000140 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 Holewinski0497ab12013-03-30 14:29:21 +0000147 } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000148 Value *len = memsetintr->getLength();
Justin Holewinski0497ab12013-03-30 14:29:21 +0000149 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
Justin Holewinskiae556d32012-05-04 20:18:50 +0000150 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 Holewinski0497ab12013-03-30 14:29:21 +0000160 if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
161 (aggrMemsets.size() == 0))
162 return false;
Justin Holewinskiae556d32012-05-04 20:18:50 +0000163
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 Carruthcdf47882014-03-09 03:16:01 +0000169 StoreInst *store = dyn_cast<StoreInst>(*load->user_begin());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000170 Value *srcAddr = load->getOperand(0);
171 Value *dstAddr = store->getOperand(1);
Mehdi Amini46a43552015-03-04 18:43:29 +0000172 unsigned numLoads = DL.getTypeStoreSize(load->getType());
Justin Holewinskiae556d32012-05-04 20:18:50 +0000173 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
206FunctionPass *llvm::createLowerAggrCopies() {
207 return new NVPTXLowerAggrCopies();
208}