blob: 7c257b4c6a8979ec48289c8e28537eff2b756851 [file] [log] [blame]
Justin Holewinski49683f32012-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 Holewinski49683f32012-05-04 20:18:50 +000014#include "NVPTXLowerAggrCopies.h"
Chandler Carruth0b8c9a82013-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"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/IntrinsicInst.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000024#include "llvm/Support/InstIterator.h"
Justin Holewinski49683f32012-05-04 20:18:50 +000025
26using namespace llvm;
27
Justin Holewinski3639ce22013-03-30 14:29:21 +000028namespace llvm { FunctionPass *createLowerAggrCopies(); }
Justin Holewinski49683f32012-05-04 20:18:50 +000029
30char NVPTXLowerAggrCopies::ID = 0;
31
32// Lower MemTransferInst or load-store pair to loop
Justin Holewinski3639ce22013-03-30 14:29:21 +000033static void convertTransferToLoop(
34 Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
35 //unsigned numLoads,
36 bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
Justin Holewinski49683f32012-05-04 20:18:50 +000037 Type *indType = len->getType();
38
39 BasicBlock *origBB = splitAt->getParent();
40 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
41 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
42
43 origBB->getTerminator()->setSuccessor(0, loopBB);
44 IRBuilder<> builder(origBB, origBB->getTerminator());
45
46 // srcAddr and dstAddr are expected to be pointer types,
47 // so no check is made here.
Justin Holewinski3639ce22013-03-30 14:29:21 +000048 unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace();
49 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
Justin Holewinski49683f32012-05-04 20:18:50 +000050
51 // Cast pointers to (char *)
52 srcAddr = builder.CreateBitCast(srcAddr, Type::getInt8PtrTy(Context, srcAS));
53 dstAddr = builder.CreateBitCast(dstAddr, Type::getInt8PtrTy(Context, dstAS));
54
55 IRBuilder<> loop(loopBB);
56 // The loop index (ind) is a phi node.
57 PHINode *ind = loop.CreatePHI(indType, 0);
58 // Incoming value for ind is 0
59 ind->addIncoming(ConstantInt::get(indType, 0), origBB);
60
61 // load from srcAddr+ind
62 Value *val = loop.CreateLoad(loop.CreateGEP(srcAddr, ind), srcVolatile);
63 // store at dstAddr+ind
64 loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), dstVolatile);
65
66 // The value for ind coming from backedge is (ind + 1)
67 Value *newind = loop.CreateAdd(ind, ConstantInt::get(indType, 1));
68 ind->addIncoming(newind, loopBB);
69
70 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
71}
72
73// Lower MemSetInst to loop
74static void convertMemSetToLoop(Instruction *splitAt, Value *dstAddr,
75 Value *len, Value *val, LLVMContext &Context,
76 Function &F) {
77 BasicBlock *origBB = splitAt->getParent();
78 BasicBlock *newBB = splitAt->getParent()->splitBasicBlock(splitAt, "split");
79 BasicBlock *loopBB = BasicBlock::Create(Context, "loadstoreloop", &F, newBB);
80
81 origBB->getTerminator()->setSuccessor(0, loopBB);
82 IRBuilder<> builder(origBB, origBB->getTerminator());
83
Justin Holewinski3639ce22013-03-30 14:29:21 +000084 unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
Justin Holewinski49683f32012-05-04 20:18:50 +000085
86 // Cast pointer to the type of value getting stored
Justin Holewinski3639ce22013-03-30 14:29:21 +000087 dstAddr =
88 builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
Justin Holewinski49683f32012-05-04 20:18:50 +000089
90 IRBuilder<> loop(loopBB);
91 PHINode *ind = loop.CreatePHI(len->getType(), 0);
92 ind->addIncoming(ConstantInt::get(len->getType(), 0), origBB);
93
94 loop.CreateStore(val, loop.CreateGEP(dstAddr, ind), false);
95
96 Value *newind = loop.CreateAdd(ind, ConstantInt::get(len->getType(), 1));
97 ind->addIncoming(newind, loopBB);
98
99 loop.CreateCondBr(loop.CreateICmpULT(newind, len), loopBB, newBB);
100}
101
102bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
103 SmallVector<LoadInst *, 4> aggrLoads;
104 SmallVector<MemTransferInst *, 4> aggrMemcpys;
105 SmallVector<MemSetInst *, 4> aggrMemsets;
106
Micah Villmow3574eca2012-10-08 16:38:25 +0000107 DataLayout *TD = &getAnalysis<DataLayout>();
Justin Holewinski49683f32012-05-04 20:18:50 +0000108 LLVMContext &Context = F.getParent()->getContext();
109
110 //
111 // Collect all the aggrLoads, aggrMemcpys and addrMemsets.
112 //
113 //const BasicBlock *firstBB = &F.front(); // first BB in F
114 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
115 //BasicBlock *bb = BI;
116 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
Justin Holewinski3639ce22013-03-30 14:29:21 +0000117 ++II) {
118 if (LoadInst *load = dyn_cast<LoadInst>(II)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000119
Justin Holewinski3639ce22013-03-30 14:29:21 +0000120 if (load->hasOneUse() == false)
121 continue;
Justin Holewinski49683f32012-05-04 20:18:50 +0000122
Justin Holewinski3639ce22013-03-30 14:29:21 +0000123 if (TD->getTypeStoreSize(load->getType()) < MaxAggrCopySize)
124 continue;
Justin Holewinski49683f32012-05-04 20:18:50 +0000125
126 User *use = *(load->use_begin());
Justin Holewinski3639ce22013-03-30 14:29:21 +0000127 if (StoreInst *store = dyn_cast<StoreInst>(use)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000128 if (store->getOperand(0) != load) //getValueOperand
Justin Holewinski3639ce22013-03-30 14:29:21 +0000129 continue;
Justin Holewinski49683f32012-05-04 20:18:50 +0000130 aggrLoads.push_back(load);
131 }
Justin Holewinski3639ce22013-03-30 14:29:21 +0000132 } else if (MemTransferInst *intr = dyn_cast<MemTransferInst>(II)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000133 Value *len = intr->getLength();
134 // If the number of elements being copied is greater
135 // than MaxAggrCopySize, lower it to a loop
Justin Holewinski3639ce22013-03-30 14:29:21 +0000136 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000137 if (len_int->getZExtValue() >= MaxAggrCopySize) {
138 aggrMemcpys.push_back(intr);
139 }
140 } else {
141 // turn variable length memcpy/memmov into loop
142 aggrMemcpys.push_back(intr);
143 }
Justin Holewinski3639ce22013-03-30 14:29:21 +0000144 } else if (MemSetInst *memsetintr = dyn_cast<MemSetInst>(II)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000145 Value *len = memsetintr->getLength();
Justin Holewinski3639ce22013-03-30 14:29:21 +0000146 if (ConstantInt *len_int = dyn_cast<ConstantInt>(len)) {
Justin Holewinski49683f32012-05-04 20:18:50 +0000147 if (len_int->getZExtValue() >= MaxAggrCopySize) {
148 aggrMemsets.push_back(memsetintr);
149 }
150 } else {
151 // turn variable length memset into loop
152 aggrMemsets.push_back(memsetintr);
153 }
154 }
155 }
156 }
Justin Holewinski3639ce22013-03-30 14:29:21 +0000157 if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
158 (aggrMemsets.size() == 0))
159 return false;
Justin Holewinski49683f32012-05-04 20:18:50 +0000160
161 //
162 // Do the transformation of an aggr load/copy/set to a loop
163 //
164 for (unsigned i = 0, e = aggrLoads.size(); i != e; ++i) {
165 LoadInst *load = aggrLoads[i];
166 StoreInst *store = dyn_cast<StoreInst>(*load->use_begin());
167 Value *srcAddr = load->getOperand(0);
168 Value *dstAddr = store->getOperand(1);
169 unsigned numLoads = TD->getTypeStoreSize(load->getType());
170 Value *len = ConstantInt::get(Type::getInt32Ty(Context), numLoads);
171
172 convertTransferToLoop(store, srcAddr, dstAddr, len, load->isVolatile(),
173 store->isVolatile(), Context, F);
174
175 store->eraseFromParent();
176 load->eraseFromParent();
177 }
178
179 for (unsigned i = 0, e = aggrMemcpys.size(); i != e; ++i) {
180 MemTransferInst *cpy = aggrMemcpys[i];
181 Value *len = cpy->getLength();
182 // llvm 2.7 version of memcpy does not have volatile
183 // operand yet. So always making it non-volatile
184 // optimistically, so that we don't see unnecessary
185 // st.volatile in ptx
186 convertTransferToLoop(cpy, cpy->getSource(), cpy->getDest(), len, false,
187 false, Context, F);
188 cpy->eraseFromParent();
189 }
190
191 for (unsigned i = 0, e = aggrMemsets.size(); i != e; ++i) {
192 MemSetInst *memsetinst = aggrMemsets[i];
193 Value *len = memsetinst->getLength();
194 Value *val = memsetinst->getValue();
195 convertMemSetToLoop(memsetinst, memsetinst->getDest(), len, val, Context,
196 F);
197 memsetinst->eraseFromParent();
198 }
199
200 return true;
201}
202
203FunctionPass *llvm::createLowerAggrCopies() {
204 return new NVPTXLowerAggrCopies();
205}