Make DataLayout Non-Optional in the Module
[oota-llvm.git] / lib / Target / NVPTX / NVPTXLowerAggrCopies.cpp
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
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"
26
27 #define DEBUG_TYPE "nvptx"
28
29 using namespace llvm;
30
31 namespace llvm { FunctionPass *createLowerAggrCopies(); }
32
33 char NVPTXLowerAggrCopies::ID = 0;
34
35 // Lower MemTransferInst or load-store pair to loop
36 static void convertTransferToLoop(
37     Instruction *splitAt, Value *srcAddr, Value *dstAddr, Value *len,
38     //unsigned numLoads,
39     bool srcVolatile, bool dstVolatile, LLVMContext &Context, Function &F) {
40   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.
51   unsigned srcAS = dyn_cast<PointerType>(srcAddr->getType())->getAddressSpace();
52   unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
53
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
77 static 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
87   unsigned dstAS = dyn_cast<PointerType>(dstAddr->getType())->getAddressSpace();
88
89   // Cast pointer to the type of value getting stored
90   dstAddr =
91       builder.CreateBitCast(dstAddr, PointerType::get(val->getType(), dstAS));
92
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
105 bool NVPTXLowerAggrCopies::runOnFunction(Function &F) {
106   SmallVector<LoadInst *, 4> aggrLoads;
107   SmallVector<MemTransferInst *, 4> aggrMemcpys;
108   SmallVector<MemSetInst *, 4> aggrMemsets;
109
110   const DataLayout &DL = F.getParent()->getDataLayout();
111   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;
120          ++II) {
121       if (LoadInst *load = dyn_cast<LoadInst>(II)) {
122
123         if (load->hasOneUse() == false)
124           continue;
125
126         if (DL.getTypeStoreSize(load->getType()) < MaxAggrCopySize)
127           continue;
128
129         User *use = load->user_back();
130         if (StoreInst *store = dyn_cast<StoreInst>(use)) {
131           if (store->getOperand(0) != load) //getValueOperand
132             continue;
133           aggrLoads.push_back(load);
134         }
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);
142           }
143         } else {
144           // turn variable length memcpy/memmov into loop
145           aggrMemcpys.push_back(intr);
146         }
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);
152           }
153         } else {
154           // turn variable length memset into loop
155           aggrMemsets.push_back(memsetintr);
156         }
157       }
158     }
159   }
160   if ((aggrLoads.size() == 0) && (aggrMemcpys.size() == 0) &&
161       (aggrMemsets.size() == 0))
162     return false;
163
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];
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);
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
206 FunctionPass *llvm::createLowerAggrCopies() {
207   return new NVPTXLowerAggrCopies();
208 }