LLVM  3.7.0
NVPTXGenericToNVVM.cpp
Go to the documentation of this file.
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"
17 #include "NVPTXUtilities.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"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueMap.h"
30 
31 using namespace llvm;
32 
33 namespace llvm {
35 }
36 
37 namespace {
38 class GenericToNVVM : public ModulePass {
39 public:
40  static char ID;
41 
42  GenericToNVVM() : ModulePass(ID) {}
43 
44  bool runOnModule(Module &M) override;
45 
46  void getAnalysisUsage(AnalysisUsage &AU) const override {}
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(ValueToValueMapTy &VM, NamedMDNode *N);
59 
61  typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
62  GVMapTy GVMap;
63  ConstantToValueMapTy ConstantToValueMap;
64 };
65 } // end namespace
66 
67 char GenericToNVVM::ID = 0;
68 
69 ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
70 
72  GenericToNVVM, "generic-to-nvvm",
73  "Ensure that the global variables are in the global address space", false,
74  false)
75 
76 bool GenericToNVVM::runOnModule(Module &M) {
77  // Create a clone of each global variable that has the default address space.
78  // The clone is created with the global address space specifier, and the pair
79  // of original global variable and its clone is placed in the GVMap for later
80  // use.
81 
82  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
83  I != E;) {
84  GlobalVariable *GV = I++;
86  !llvm::isTexture(*GV) && !llvm::isSurface(*GV) &&
87  !llvm::isSampler(*GV) && !GV->getName().startswith("llvm.")) {
88  GlobalVariable *NewGV = new GlobalVariable(
89  M, GV->getType()->getElementType(), GV->isConstant(),
90  GV->getLinkage(),
91  GV->hasInitializer() ? GV->getInitializer() : nullptr,
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  // Copy GVMap over to a standard value map.
130  for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
131  VM[I->first] = I->second;
132 
133  // Walk through the metadata section and update the debug information
134  // associated with the global variables in the default address space.
135  for (Module::named_metadata_iterator I = M.named_metadata_begin(),
136  E = M.named_metadata_end();
137  I != E; I++) {
138  remapNamedMDNode(VM, I);
139  }
140 
141  // Walk through the global variable initializers, and replace any use of
142  // original global variables in GVMap with a use of the corresponding copies
143  // in GVMap. The copies need to be bitcast to the original global variable
144  // types, as we cannot use cvta in global variable initializers.
145  for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
146  GlobalVariable *GV = I->first;
147  GlobalVariable *NewGV = I->second;
148 
149  // Remove GV from the map so that it can be RAUWed. Note that
150  // DenseMap::erase() won't invalidate any iterators but this one.
151  auto Next = std::next(I);
152  GVMap.erase(I);
153  I = Next;
154 
155  Constant *BitCastNewGV = ConstantExpr::getPointerCast(NewGV, GV->getType());
156  // At this point, the remaining uses of GV should be found only in global
157  // variable initializers, as other uses have been already been removed
158  // while walking through the instructions in function definitions.
159  GV->replaceAllUsesWith(BitCastNewGV);
160  std::string Name = GV->getName();
161  GV->eraseFromParent();
162  NewGV->setName(Name);
163  }
164  assert(GVMap.empty() && "Expected it to be empty by now");
165 
166  return true;
167 }
168 
169 Value *GenericToNVVM::getOrInsertCVTA(Module *M, Function *F,
170  GlobalVariable *GV,
171  IRBuilder<> &Builder) {
172  PointerType *GVType = GV->getType();
173  Value *CVTA = nullptr;
174 
175  // See if the address space conversion requires the operand to be bitcast
176  // to i8 addrspace(n)* first.
177  EVT ExtendedGVType = EVT::getEVT(GVType->getElementType(), true);
178  if (!ExtendedGVType.isInteger() && !ExtendedGVType.isFloatingPoint()) {
179  // A bitcast to i8 addrspace(n)* on the operand is needed.
180  LLVMContext &Context = M->getContext();
181  unsigned int AddrSpace = GVType->getAddressSpace();
182  Type *DestTy = PointerType::get(Type::getInt8Ty(Context), AddrSpace);
183  CVTA = Builder.CreateBitCast(GV, DestTy, "cvta");
184  // Insert the address space conversion.
185  Type *ResultType =
187  SmallVector<Type *, 2> ParamTypes;
188  ParamTypes.push_back(ResultType);
189  ParamTypes.push_back(DestTy);
190  Function *CVTAFunction = Intrinsic::getDeclaration(
191  M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
192  CVTA = Builder.CreateCall(CVTAFunction, CVTA, "cvta");
193  // Another bitcast from i8 * to <the element type of GVType> * is
194  // required.
195  DestTy =
197  CVTA = Builder.CreateBitCast(CVTA, DestTy, "cvta");
198  } else {
199  // A simple CVTA is enough.
200  SmallVector<Type *, 2> ParamTypes;
201  ParamTypes.push_back(PointerType::get(GVType->getElementType(),
203  ParamTypes.push_back(GVType);
204  Function *CVTAFunction = Intrinsic::getDeclaration(
205  M, Intrinsic::nvvm_ptr_global_to_gen, ParamTypes);
206  CVTA = Builder.CreateCall(CVTAFunction, GV, "cvta");
207  }
208 
209  return CVTA;
210 }
211 
212 Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
213  IRBuilder<> &Builder) {
214  // If the constant C has been converted already in the given function F, just
215  // return the converted value.
216  ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
217  if (CTII != ConstantToValueMap.end()) {
218  return CTII->second;
219  }
220 
221  Value *NewValue = C;
222  if (isa<GlobalVariable>(C)) {
223  // If the constant C is a global variable and is found in GVMap, generate a
224  // set set of instructions that convert the clone of C with the global
225  // address space specifier to a generic pointer.
226  // The constant C cannot be used here, as it will be erased from the
227  // module eventually. And the clone of C with the global address space
228  // specifier cannot be used here either, as it will affect the types of
229  // other instructions in the function. Hence, this address space conversion
230  // is required.
231  GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
232  if (I != GVMap.end()) {
233  NewValue = getOrInsertCVTA(M, F, I->second, Builder);
234  }
235  } else if (isa<ConstantVector>(C) || isa<ConstantArray>(C) ||
236  isa<ConstantStruct>(C)) {
237  // If any element in the constant vector or aggregate C is or uses a global
238  // variable in GVMap, the constant C needs to be reconstructed, using a set
239  // of instructions.
240  NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
241  } else if (isa<ConstantExpr>(C)) {
242  // If any operand in the constant expression C is or uses a global variable
243  // in GVMap, the constant expression C needs to be reconstructed, using a
244  // set of instructions.
245  NewValue = remapConstantExpr(M, F, cast<ConstantExpr>(C), Builder);
246  }
247 
248  ConstantToValueMap[C] = NewValue;
249  return NewValue;
250 }
251 
252 Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
253  Module *M, Function *F, Constant *C, IRBuilder<> &Builder) {
254  bool OperandChanged = false;
255  SmallVector<Value *, 4> NewOperands;
256  unsigned NumOperands = C->getNumOperands();
257 
258  // Check if any element is or uses a global variable in GVMap, and thus
259  // converted to another value.
260  for (unsigned i = 0; i < NumOperands; ++i) {
261  Value *Operand = C->getOperand(i);
262  Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
263  OperandChanged |= Operand != NewOperand;
264  NewOperands.push_back(NewOperand);
265  }
266 
267  // If none of the elements has been modified, return C as it is.
268  if (!OperandChanged) {
269  return C;
270  }
271 
272  // If any of the elements has been modified, construct the equivalent
273  // vector or aggregate value with a set instructions and the converted
274  // elements.
275  Value *NewValue = UndefValue::get(C->getType());
276  if (isa<ConstantVector>(C)) {
277  for (unsigned i = 0; i < NumOperands; ++i) {
279  NewValue = Builder.CreateInsertElement(NewValue, NewOperands[i], Idx);
280  }
281  } else {
282  for (unsigned i = 0; i < NumOperands; ++i) {
283  NewValue =
284  Builder.CreateInsertValue(NewValue, NewOperands[i], makeArrayRef(i));
285  }
286  }
287 
288  return NewValue;
289 }
290 
291 Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
292  IRBuilder<> &Builder) {
293  bool OperandChanged = false;
294  SmallVector<Value *, 4> NewOperands;
295  unsigned NumOperands = C->getNumOperands();
296 
297  // Check if any operand is or uses a global variable in GVMap, and thus
298  // converted to another value.
299  for (unsigned i = 0; i < NumOperands; ++i) {
300  Value *Operand = C->getOperand(i);
301  Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
302  OperandChanged |= Operand != NewOperand;
303  NewOperands.push_back(NewOperand);
304  }
305 
306  // If none of the operands has been modified, return C as it is.
307  if (!OperandChanged) {
308  return C;
309  }
310 
311  // If any of the operands has been modified, construct the instruction with
312  // the converted operands.
313  unsigned Opcode = C->getOpcode();
314  switch (Opcode) {
315  case Instruction::ICmp:
316  // CompareConstantExpr (icmp)
317  return Builder.CreateICmp(CmpInst::Predicate(C->getPredicate()),
318  NewOperands[0], NewOperands[1]);
319  case Instruction::FCmp:
320  // CompareConstantExpr (fcmp)
321  assert(false && "Address space conversion should have no effect "
322  "on float point CompareConstantExpr (fcmp)!");
323  return C;
325  // ExtractElementConstantExpr
326  return Builder.CreateExtractElement(NewOperands[0], NewOperands[1]);
327  case Instruction::InsertElement:
328  // InsertElementConstantExpr
329  return Builder.CreateInsertElement(NewOperands[0], NewOperands[1],
330  NewOperands[2]);
331  case Instruction::ShuffleVector:
332  // ShuffleVector
333  return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
334  NewOperands[2]);
335  case Instruction::ExtractValue:
336  // ExtractValueConstantExpr
337  return Builder.CreateExtractValue(NewOperands[0], C->getIndices());
338  case Instruction::InsertValue:
339  // InsertValueConstantExpr
340  return Builder.CreateInsertValue(NewOperands[0], NewOperands[1],
341  C->getIndices());
342  case Instruction::GetElementPtr:
343  // GetElementPtrConstantExpr
344  return cast<GEPOperator>(C)->isInBounds()
345  ? Builder.CreateGEP(
346  cast<GEPOperator>(C)->getSourceElementType(),
347  NewOperands[0],
348  makeArrayRef(&NewOperands[1], NumOperands - 1))
349  : Builder.CreateInBoundsGEP(
350  cast<GEPOperator>(C)->getSourceElementType(),
351  NewOperands[0],
352  makeArrayRef(&NewOperands[1], NumOperands - 1));
353  case Instruction::Select:
354  // SelectConstantExpr
355  return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
356  default:
357  // BinaryConstantExpr
358  if (Instruction::isBinaryOp(Opcode)) {
359  return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
360  NewOperands[0], NewOperands[1]);
361  }
362  // UnaryConstantExpr
363  if (Instruction::isCast(Opcode)) {
364  return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
365  NewOperands[0], C->getType());
366  }
367  assert(false && "GenericToNVVM encountered an unsupported ConstantExpr");
368  return C;
369  }
370 }
371 
372 void GenericToNVVM::remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N) {
373 
374  bool OperandChanged = false;
375  SmallVector<MDNode *, 16> NewOperands;
376  unsigned NumOperands = N->getNumOperands();
377 
378  // Check if any operand is or contains a global variable in GVMap, and thus
379  // converted to another value.
380  for (unsigned i = 0; i < NumOperands; ++i) {
381  MDNode *Operand = N->getOperand(i);
382  MDNode *NewOperand = MapMetadata(Operand, VM);
383  OperandChanged |= Operand != NewOperand;
384  NewOperands.push_back(NewOperand);
385  }
386 
387  // If none of the operands has been modified, return immediately.
388  if (!OperandChanged) {
389  return;
390  }
391 
392  // Replace the old operands with the new operands.
393  N->dropAllReferences();
394  for (SmallVectorImpl<MDNode *>::iterator I = NewOperands.begin(),
395  E = NewOperands.end();
396  I != E; ++I) {
397  N->addOperand(*I);
398  }
399 }
Value * CreateGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1032
LinkageTypes getLinkage() const
Definition: GlobalValue.h:289
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1285
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1442
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumOperands() const
Definition: User.h:138
void addOperand(MDNode *M)
Definition: Metadata.cpp:971
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
Definition: Type.cpp:738
INITIALIZE_PASS(GenericToNVVM,"generic-to-nvvm","Ensure that the global variables are in the global address space", false, false) bool GenericToNVVM
bool isSurface(const llvm::Value &)
Metadata node.
Definition: Metadata.h:740
F(f)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:472
ModulePass * createGenericToNVVMPass()
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:1522
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1144
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:923
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1508
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
bool isCast() const
Definition: Instruction.h:118
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:1541
A tuple of MDNodes.
Definition: Metadata.h:1127
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:308
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:250
static ConstantInt * ExtractElement(Constant *V, Constant *Idx)
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:194
bool isInteger() const
isInteger - Return true if this is an integer, or a vector integer type.
Definition: ValueTypes.h:110
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:866
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
Type * getElementType() const
Definition: DerivedTypes.h:323
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
This is an important base class in LLVM.
Definition: Constant.h:41
Metadata * MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:965
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
Value * getOperand(unsigned i) const
Definition: User.h:118
unsigned getPredicate() const
getPredicate - Return the ICMP or FCMP predicate value.
Definition: Constants.cpp:1223
ArrayRef< unsigned > getIndices() const
getIndices - Assert that this is an insertvalue or exactvalue expression and return the list of indic...
Definition: Constants.cpp:1215
EVT - Extended Value Type.
Definition: ValueTypes.h:31
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1049
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
void initializeGenericToNVVMPass(PassRegistry &)
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="")
Definition: IRBuilder.h:1467
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1648
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1495
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1253
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
bool hasInitializer() const
Definitions have initializers, declarations don't.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
Definition: IRBuilder.h:1482
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:1549
bool isSampler(const llvm::Value &)
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:160
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
bool isBinaryOp() const
Definition: Instruction.h:116
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
getEVT - Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:277
void copyAttributesFrom(const GlobalValue *Src) override
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:221
NamedMDListType::iterator named_metadata_iterator
The named metadata iterators.
Definition: Module.h:150
bool isFloatingPoint() const
isFloatingPoint - Return true if this is a FP, or a vector FP type.
Definition: ValueTypes.h:105
bool isTexture(const llvm::Value &)
LLVM Value Representation.
Definition: Value.h:69
unsigned getNumOperands() const
Definition: Metadata.cpp:961
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.cpp:982
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:237
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:265