[C++11] Add range based accessors for the Use-Def chain of a Value.
[oota-llvm.git] / lib / Target / NVPTX / NVPTXGenericToNVVM.cpp
1 //===-- GenericToNVVM.cpp - Convert generic module to NVVM module - 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 //
10 // Convert generic global variables into either .global or .const access based
11 // on the variable's "constant" qualifier.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "NVPTX.h"
16 #include "MCTargetDesc/NVPTXBaseInfo.h"
17 #include "NVPTXUtilities.h"
18 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Intrinsics.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/IR/ValueMap.h"
28 #include "llvm/PassManager.h"
29
30 using namespace llvm;
31
32 namespace llvm {
33 void initializeGenericToNVVMPass(PassRegistry &);
34 }
35
36 namespace {
37 class GenericToNVVM : public ModulePass {
38 public:
39   static char ID;
40
41   GenericToNVVM() : ModulePass(ID) {}
42
43   virtual bool runOnModule(Module &M);
44
45   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46   }
47
48 private:
49   Value *getOrInsertCVTA(Module *M, Function *F, GlobalVariable *GV,
50                          IRBuilder<> &Builder);
51   Value *remapConstant(Module *M, Function *F, Constant *C,
52                        IRBuilder<> &Builder);
53   Value *remapConstantVectorOrConstantAggregate(Module *M, Function *F,
54                                                 Constant *C,
55                                                 IRBuilder<> &Builder);
56   Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
57                            IRBuilder<> &Builder);
58   void remapNamedMDNode(Module *M, NamedMDNode *N);
59   MDNode *remapMDNode(Module *M, MDNode *N);
60
61   typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
62   typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
63   GVMapTy GVMap;
64   ConstantToValueMapTy ConstantToValueMap;
65 };
66 } // end namespace
67
68 char GenericToNVVM::ID = 0;
69
70 ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
71
72 INITIALIZE_PASS(
73     GenericToNVVM, "generic-to-nvvm",
74     "Ensure that the global variables are in the global address space", false,
75     false)
76
77 bool GenericToNVVM::runOnModule(Module &M) {
78   // Create a clone of each global variable that has the default address space.
79   // The clone is created with the global address space  specifier, and the pair
80   // of original global variable and its clone is placed in the GVMap for later
81   // use.
82
83   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
84        I != E;) {
85     GlobalVariable *GV = I++;
86     if (GV->getType()->getAddressSpace() == llvm::ADDRESS_SPACE_GENERIC &&
87         !llvm::isTexture(*GV) && !llvm::isSurface(*GV) &&
88         !GV->getName().startswith("llvm.")) {
89       GlobalVariable *NewGV = new GlobalVariable(
90           M, GV->getType()->getElementType(), GV->isConstant(),
91           GV->getLinkage(), GV->hasInitializer() ? GV->getInitializer() : NULL,
92           "", GV, GV->getThreadLocalMode(), llvm::ADDRESS_SPACE_GLOBAL);
93       NewGV->copyAttributesFrom(GV);
94       GVMap[GV] = NewGV;
95     }
96   }
97
98   // Return immediately, if every global variable has a specific address space
99   // specifier.
100   if (GVMap.empty()) {
101     return false;
102   }
103
104   // Walk through the instructions in function defitinions, and replace any use
105   // of original global variables in GVMap with a use of the corresponding
106   // copies in GVMap.  If necessary, promote constants to instructions.
107   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
108     if (I->isDeclaration()) {
109       continue;
110     }
111     IRBuilder<> Builder(I->getEntryBlock().getFirstNonPHIOrDbg());
112     for (Function::iterator BBI = I->begin(), BBE = I->end(); BBI != BBE;
113          ++BBI) {
114       for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
115            ++II) {
116         for (unsigned i = 0, e = II->getNumOperands(); i < e; ++i) {
117           Value *Operand = II->getOperand(i);
118           if (isa<Constant>(Operand)) {
119             II->setOperand(
120                 i, remapConstant(&M, I, cast<Constant>(Operand), Builder));
121           }
122         }
123       }
124     }
125     ConstantToValueMap.clear();
126   }
127
128   // Walk through the metadata section and update the debug information
129   // associated with the global variables in the default address space.
130   for (Module::named_metadata_iterator I = M.named_metadata_begin(),
131                                        E = M.named_metadata_end();
132        I != E; I++) {
133     remapNamedMDNode(&M, I);
134   }
135
136   // Walk through the global variable  initializers, and replace any use of
137   // original global variables in GVMap with a use of the corresponding copies
138   // in GVMap.  The copies need to be bitcast to the original global variable
139   // types, as we cannot use cvta in global variable initializers.
140   for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
141     GlobalVariable *GV = I->first;
142     GlobalVariable *NewGV = I->second;
143     ++I;
144     Constant *BitCastNewGV = ConstantExpr::getPointerCast(NewGV, GV->getType());
145     // At this point, the remaining uses of GV should be found only in global
146     // variable initializers, as other uses have been already been removed
147     // while walking through the instructions in function definitions.
148     for (Value::use_iterator UI = GV->use_begin(), UE = GV->use_end();
149          UI != UE;)
150       (UI++)->set(BitCastNewGV);
151     std::string Name = GV->getName();
152     GV->removeDeadConstantUsers();
153     GV->eraseFromParent();
154     NewGV->setName(Name);
155   }
156   GVMap.clear();
157
158   return true;
159 }
160
161 Value *GenericToNVVM::getOrInsertCVTA(Module *M, Function *F,
162                                       GlobalVariable *GV,
163                                       IRBuilder<> &Builder) {
164   PointerType *GVType = GV->getType();
165   Value *CVTA = NULL;
166
167   // See if the address space conversion requires the operand to be bitcast
168   // to i8 addrspace(n)* first.
169   EVT ExtendedGVType = EVT::getEVT(GVType->getElementType(), true);
170   if (!ExtendedGVType.isInteger() && !ExtendedGVType.isFloatingPoint()) {
171     // A bitcast to i8 addrspace(n)* on the operand is needed.
172     LLVMContext &Context = M->getContext();
173     unsigned int AddrSpace = GVType->getAddressSpace();
174     Type *DestTy = PointerType::get(Type::getInt8Ty(Context), AddrSpace);
175     CVTA = Builder.CreateBitCast(GV, DestTy, "cvta");
176     // Insert the address space conversion.
177     Type *ResultType =
178         PointerType::get(Type::getInt8Ty(Context), llvm::ADDRESS_SPACE_GENERIC);
179     SmallVector<Type *, 2> ParamTypes;
180     ParamTypes.push_back(ResultType);
181     ParamTypes.push_back(DestTy);
182     Function *CVTAFunction = Intrinsic::getDeclaration(
183         M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
184     CVTA = Builder.CreateCall(CVTAFunction, CVTA, "cvta");
185     // Another bitcast from i8 * to <the element type of GVType> * is
186     // required.
187     DestTy =
188         PointerType::get(GVType->getElementType(), llvm::ADDRESS_SPACE_GENERIC);
189     CVTA = Builder.CreateBitCast(CVTA, DestTy, "cvta");
190   } else {
191     // A simple CVTA is enough.
192     SmallVector<Type *, 2> ParamTypes;
193     ParamTypes.push_back(PointerType::get(GVType->getElementType(),
194                                           llvm::ADDRESS_SPACE_GENERIC));
195     ParamTypes.push_back(GVType);
196     Function *CVTAFunction = Intrinsic::getDeclaration(
197         M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
198     CVTA = Builder.CreateCall(CVTAFunction, GV, "cvta");
199   }
200
201   return CVTA;
202 }
203
204 Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
205                                     IRBuilder<> &Builder) {
206   // If the constant C has been converted already in the given function  F, just
207   // return the converted value.
208   ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
209   if (CTII != ConstantToValueMap.end()) {
210     return CTII->second;
211   }
212
213   Value *NewValue = C;
214   if (isa<GlobalVariable>(C)) {
215     // If the constant C is a global variable and is found in  GVMap, generate a
216     // set set of instructions that convert the clone of C with the global
217     // address space specifier to a generic pointer.
218     // The constant C cannot be used here, as it will be erased from the
219     // module eventually.  And the clone of C with the global address space
220     // specifier cannot be used here either, as it will affect the types of
221     // other instructions in the function.  Hence, this address space conversion
222     // is required.
223     GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
224     if (I != GVMap.end()) {
225       NewValue = getOrInsertCVTA(M, F, I->second, Builder);
226     }
227   } else if (isa<ConstantVector>(C) || isa<ConstantArray>(C) ||
228              isa<ConstantStruct>(C)) {
229     // If any element in the constant vector or aggregate C is or uses a global
230     // variable in GVMap, the constant C needs to be reconstructed, using a set
231     // of instructions.
232     NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
233   } else if (isa<ConstantExpr>(C)) {
234     // If any operand in the constant expression C is or uses a global variable
235     // in GVMap, the constant expression C needs to be reconstructed, using a
236     // set of instructions.
237     NewValue = remapConstantExpr(M, F, cast<ConstantExpr>(C), Builder);
238   }
239
240   ConstantToValueMap[C] = NewValue;
241   return NewValue;
242 }
243
244 Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
245     Module *M, Function *F, Constant *C, IRBuilder<> &Builder) {
246   bool OperandChanged = false;
247   SmallVector<Value *, 4> NewOperands;
248   unsigned NumOperands = C->getNumOperands();
249
250   // Check if any element is or uses a global variable in  GVMap, and thus
251   // converted to another value.
252   for (unsigned i = 0; i < NumOperands; ++i) {
253     Value *Operand = C->getOperand(i);
254     Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
255     OperandChanged |= Operand != NewOperand;
256     NewOperands.push_back(NewOperand);
257   }
258
259   // If none of the elements has been modified, return C as it is.
260   if (!OperandChanged) {
261     return C;
262   }
263
264   // If any of the elements has been  modified, construct the equivalent
265   // vector or aggregate value with a set instructions and the converted
266   // elements.
267   Value *NewValue = UndefValue::get(C->getType());
268   if (isa<ConstantVector>(C)) {
269     for (unsigned i = 0; i < NumOperands; ++i) {
270       Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
271       NewValue = Builder.CreateInsertElement(NewValue, NewOperands[i], Idx);
272     }
273   } else {
274     for (unsigned i = 0; i < NumOperands; ++i) {
275       NewValue =
276           Builder.CreateInsertValue(NewValue, NewOperands[i], makeArrayRef(i));
277     }
278   }
279
280   return NewValue;
281 }
282
283 Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
284                                         IRBuilder<> &Builder) {
285   bool OperandChanged = false;
286   SmallVector<Value *, 4> NewOperands;
287   unsigned NumOperands = C->getNumOperands();
288
289   // Check if any operand is or uses a global variable in  GVMap, and thus
290   // converted to another value.
291   for (unsigned i = 0; i < NumOperands; ++i) {
292     Value *Operand = C->getOperand(i);
293     Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
294     OperandChanged |= Operand != NewOperand;
295     NewOperands.push_back(NewOperand);
296   }
297
298   // If none of the operands has been modified, return C as it is.
299   if (!OperandChanged) {
300     return C;
301   }
302
303   // If any of the operands has been modified, construct the instruction with
304   // the converted operands.
305   unsigned Opcode = C->getOpcode();
306   switch (Opcode) {
307   case Instruction::ICmp:
308     // CompareConstantExpr (icmp)
309     return Builder.CreateICmp(CmpInst::Predicate(C->getPredicate()),
310                               NewOperands[0], NewOperands[1]);
311   case Instruction::FCmp:
312     // CompareConstantExpr (fcmp)
313     assert(false && "Address space conversion should have no effect "
314                     "on float point CompareConstantExpr (fcmp)!");
315     return C;
316   case Instruction::ExtractElement:
317     // ExtractElementConstantExpr
318     return Builder.CreateExtractElement(NewOperands[0], NewOperands[1]);
319   case Instruction::InsertElement:
320     // InsertElementConstantExpr
321     return Builder.CreateInsertElement(NewOperands[0], NewOperands[1],
322                                        NewOperands[2]);
323   case Instruction::ShuffleVector:
324     // ShuffleVector
325     return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
326                                        NewOperands[2]);
327   case Instruction::ExtractValue:
328     // ExtractValueConstantExpr
329     return Builder.CreateExtractValue(NewOperands[0], C->getIndices());
330   case Instruction::InsertValue:
331     // InsertValueConstantExpr
332     return Builder.CreateInsertValue(NewOperands[0], NewOperands[1],
333                                      C->getIndices());
334   case Instruction::GetElementPtr:
335     // GetElementPtrConstantExpr
336     return cast<GEPOperator>(C)->isInBounds()
337                ? Builder.CreateGEP(
338                      NewOperands[0],
339                      makeArrayRef(&NewOperands[1], NumOperands - 1))
340                : Builder.CreateInBoundsGEP(
341                      NewOperands[0],
342                      makeArrayRef(&NewOperands[1], NumOperands - 1));
343   case Instruction::Select:
344     // SelectConstantExpr
345     return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
346   default:
347     // BinaryConstantExpr
348     if (Instruction::isBinaryOp(Opcode)) {
349       return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
350                                  NewOperands[0], NewOperands[1]);
351     }
352     // UnaryConstantExpr
353     if (Instruction::isCast(Opcode)) {
354       return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
355                                 NewOperands[0], C->getType());
356     }
357     assert(false && "GenericToNVVM encountered an unsupported ConstantExpr");
358     return C;
359   }
360 }
361
362 void GenericToNVVM::remapNamedMDNode(Module *M, NamedMDNode *N) {
363
364   bool OperandChanged = false;
365   SmallVector<MDNode *, 16> NewOperands;
366   unsigned NumOperands = N->getNumOperands();
367
368   // Check if any operand is or contains a global variable in  GVMap, and thus
369   // converted to another value.
370   for (unsigned i = 0; i < NumOperands; ++i) {
371     MDNode *Operand = N->getOperand(i);
372     MDNode *NewOperand = remapMDNode(M, Operand);
373     OperandChanged |= Operand != NewOperand;
374     NewOperands.push_back(NewOperand);
375   }
376
377   // If none of the operands has been modified, return immediately.
378   if (!OperandChanged) {
379     return;
380   }
381
382   // Replace the old operands with the new operands.
383   N->dropAllReferences();
384   for (SmallVectorImpl<MDNode *>::iterator I = NewOperands.begin(),
385                                            E = NewOperands.end();
386        I != E; ++I) {
387     N->addOperand(*I);
388   }
389 }
390
391 MDNode *GenericToNVVM::remapMDNode(Module *M, MDNode *N) {
392
393   bool OperandChanged = false;
394   SmallVector<Value *, 8> NewOperands;
395   unsigned NumOperands = N->getNumOperands();
396
397   // Check if any operand is or contains a global variable in  GVMap, and thus
398   // converted to another value.
399   for (unsigned i = 0; i < NumOperands; ++i) {
400     Value *Operand = N->getOperand(i);
401     Value *NewOperand = Operand;
402     if (Operand) {
403       if (isa<GlobalVariable>(Operand)) {
404         GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(Operand));
405         if (I != GVMap.end()) {
406           NewOperand = I->second;
407           if (++i < NumOperands) {
408             NewOperands.push_back(NewOperand);
409             // Address space of the global variable follows the global variable
410             // in the global variable debug info (see createGlobalVariable in
411             // lib/Analysis/DIBuilder.cpp).
412             NewOperand =
413                 ConstantInt::get(Type::getInt32Ty(M->getContext()),
414                                  I->second->getType()->getAddressSpace());
415           }
416         }
417       } else if (isa<MDNode>(Operand)) {
418         NewOperand = remapMDNode(M, cast<MDNode>(Operand));
419       }
420     }
421     OperandChanged |= Operand != NewOperand;
422     NewOperands.push_back(NewOperand);
423   }
424
425   // If none of the operands has been modified, return N as it is.
426   if (!OperandChanged) {
427     return N;
428   }
429
430   // If any of the operands has been modified, create a new MDNode with the new
431   // operands.
432   return MDNode::get(M->getContext(), makeArrayRef(NewOperands));
433 }