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 "MCTargetDesc/NVPTXBaseInfo.h"
16 #include "NVPTX.h"
17 #include "NVPTXUtilities.h"
18 #include "llvm/CodeGen/ValueTypes.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/LegacyPassManager.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/IR/ValueMap.h"
28 #include "llvm/Transforms/Utils/ValueMapper.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   bool runOnModule(Module &M) override;
44 
45   void getAnalysisUsage(AnalysisUsage &AU) const override {}
46 
47 private:
48   Value *remapConstant(Module *M, Function *F, Constant *C,
49                        IRBuilder<> &Builder);
50   Value *remapConstantVectorOrConstantAggregate(Module *M, Function *F,
51                                                 Constant *C,
52                                                 IRBuilder<> &Builder);
53   Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
54                            IRBuilder<> &Builder);
55 
56   typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
57   typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
58   GVMapTy GVMap;
59   ConstantToValueMapTy ConstantToValueMap;
60 };
61 } // end namespace
62 
63 char GenericToNVVM::ID = 0;
64 
65 ModulePass *llvm::createGenericToNVVMPass() { return new GenericToNVVM(); }
66 
67 INITIALIZE_PASS(
68     GenericToNVVM, "generic-to-nvvm",
69     "Ensure that the global variables are in the global address space", false,
70     false)
71 
72 bool GenericToNVVM::runOnModule(Module &M) {
73   // Create a clone of each global variable that has the default address space.
74   // The clone is created with the global address space  specifier, and the pair
75   // of original global variable and its clone is placed in the GVMap for later
76   // use.
77 
78   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
79        I != E;) {
80     GlobalVariable *GV = &*I++;
81     if (GV->getType()->getAddressSpace() == llvm::ADDRESS_SPACE_GENERIC &&
82         !llvm::isTexture(*GV) && !llvm::isSurface(*GV) &&
83         !llvm::isSampler(*GV) && !GV->getName().startswith("llvm.")) {
84       GlobalVariable *NewGV = new GlobalVariable(
85           M, GV->getValueType(), GV->isConstant(),
86           GV->getLinkage(),
87           GV->hasInitializer() ? GV->getInitializer() : nullptr,
88           "", GV, GV->getThreadLocalMode(), llvm::ADDRESS_SPACE_GLOBAL);
89       NewGV->copyAttributesFrom(GV);
90       GVMap[GV] = NewGV;
91     }
92   }
93 
94   // Return immediately, if every global variable has a specific address space
95   // specifier.
96   if (GVMap.empty()) {
97     return false;
98   }
99 
100   // Walk through the instructions in function defitinions, and replace any use
101   // of original global variables in GVMap with a use of the corresponding
102   // copies in GVMap.  If necessary, promote constants to instructions.
103   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
104     if (I->isDeclaration()) {
105       continue;
106     }
107     IRBuilder<> Builder(I->getEntryBlock().getFirstNonPHIOrDbg());
108     for (Function::iterator BBI = I->begin(), BBE = I->end(); BBI != BBE;
109          ++BBI) {
110       for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
111            ++II) {
112         for (unsigned i = 0, e = II->getNumOperands(); i < e; ++i) {
113           Value *Operand = II->getOperand(i);
114           if (isa<Constant>(Operand)) {
115             II->setOperand(
116                 i, remapConstant(&M, &*I, cast<Constant>(Operand), Builder));
117           }
118         }
119       }
120     }
121     ConstantToValueMap.clear();
122   }
123 
124   // Copy GVMap over to a standard value map.
125   ValueToValueMapTy VM;
126   for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
127     VM[I->first] = I->second;
128 
129   // Walk through the global variable  initializers, and replace any use of
130   // original global variables in GVMap with a use of the corresponding copies
131   // in GVMap.  The copies need to be bitcast to the original global variable
132   // types, as we cannot use cvta in global variable initializers.
133   for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
134     GlobalVariable *GV = I->first;
135     GlobalVariable *NewGV = I->second;
136 
137     // Remove GV from the map so that it can be RAUWed.  Note that
138     // DenseMap::erase() won't invalidate any iterators but this one.
139     auto Next = std::next(I);
140     GVMap.erase(I);
141     I = Next;
142 
143     Constant *BitCastNewGV = ConstantExpr::getPointerCast(NewGV, GV->getType());
144     // At this point, the remaining uses of GV should be found only in global
145     // variable initializers, as other uses have been already been removed
146     // while walking through the instructions in function definitions.
147     GV->replaceAllUsesWith(BitCastNewGV);
148     std::string Name = GV->getName();
149     GV->eraseFromParent();
150     NewGV->setName(Name);
151   }
152   assert(GVMap.empty() && "Expected it to be empty by now");
153 
154   return true;
155 }
156 
157 Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
158                                     IRBuilder<> &Builder) {
159   // If the constant C has been converted already in the given function  F, just
160   // return the converted value.
161   ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
162   if (CTII != ConstantToValueMap.end()) {
163     return CTII->second;
164   }
165 
166   Value *NewValue = C;
167   if (isa<GlobalVariable>(C)) {
168     // If the constant C is a global variable and is found in GVMap, substitute
169     //
170     //   addrspacecast GVMap[C] to addrspace(0)
171     //
172     // for our use of C.
173     GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(C));
174     if (I != GVMap.end()) {
175       GlobalVariable *GV = I->second;
176       NewValue = Builder.CreateAddrSpaceCast(
177           GV,
178           PointerType::get(GV->getValueType(), llvm::ADDRESS_SPACE_GENERIC));
179     }
180   } else if (isa<ConstantAggregate>(C)) {
181     // If any element in the constant vector or aggregate C is or uses a global
182     // variable in GVMap, the constant C needs to be reconstructed, using a set
183     // of instructions.
184     NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
185   } else if (isa<ConstantExpr>(C)) {
186     // If any operand in the constant expression C is or uses a global variable
187     // in GVMap, the constant expression C needs to be reconstructed, using a
188     // set of instructions.
189     NewValue = remapConstantExpr(M, F, cast<ConstantExpr>(C), Builder);
190   }
191 
192   ConstantToValueMap[C] = NewValue;
193   return NewValue;
194 }
195 
196 Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
197     Module *M, Function *F, Constant *C, IRBuilder<> &Builder) {
198   bool OperandChanged = false;
199   SmallVector<Value *, 4> NewOperands;
200   unsigned NumOperands = C->getNumOperands();
201 
202   // Check if any element is or uses a global variable in  GVMap, and thus
203   // converted to another value.
204   for (unsigned i = 0; i < NumOperands; ++i) {
205     Value *Operand = C->getOperand(i);
206     Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
207     OperandChanged |= Operand != NewOperand;
208     NewOperands.push_back(NewOperand);
209   }
210 
211   // If none of the elements has been modified, return C as it is.
212   if (!OperandChanged) {
213     return C;
214   }
215 
216   // If any of the elements has been  modified, construct the equivalent
217   // vector or aggregate value with a set instructions and the converted
218   // elements.
219   Value *NewValue = UndefValue::get(C->getType());
220   if (isa<ConstantVector>(C)) {
221     for (unsigned i = 0; i < NumOperands; ++i) {
222       Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
223       NewValue = Builder.CreateInsertElement(NewValue, NewOperands[i], Idx);
224     }
225   } else {
226     for (unsigned i = 0; i < NumOperands; ++i) {
227       NewValue =
228           Builder.CreateInsertValue(NewValue, NewOperands[i], makeArrayRef(i));
229     }
230   }
231 
232   return NewValue;
233 }
234 
235 Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
236                                         IRBuilder<> &Builder) {
237   bool OperandChanged = false;
238   SmallVector<Value *, 4> NewOperands;
239   unsigned NumOperands = C->getNumOperands();
240 
241   // Check if any operand is or uses a global variable in  GVMap, and thus
242   // converted to another value.
243   for (unsigned i = 0; i < NumOperands; ++i) {
244     Value *Operand = C->getOperand(i);
245     Value *NewOperand = remapConstant(M, F, cast<Constant>(Operand), Builder);
246     OperandChanged |= Operand != NewOperand;
247     NewOperands.push_back(NewOperand);
248   }
249 
250   // If none of the operands has been modified, return C as it is.
251   if (!OperandChanged) {
252     return C;
253   }
254 
255   // If any of the operands has been modified, construct the instruction with
256   // the converted operands.
257   unsigned Opcode = C->getOpcode();
258   switch (Opcode) {
259   case Instruction::ICmp:
260     // CompareConstantExpr (icmp)
261     return Builder.CreateICmp(CmpInst::Predicate(C->getPredicate()),
262                               NewOperands[0], NewOperands[1]);
263   case Instruction::FCmp:
264     // CompareConstantExpr (fcmp)
265     llvm_unreachable("Address space conversion should have no effect "
266                      "on float point CompareConstantExpr (fcmp)!");
267   case Instruction::ExtractElement:
268     // ExtractElementConstantExpr
269     return Builder.CreateExtractElement(NewOperands[0], NewOperands[1]);
270   case Instruction::InsertElement:
271     // InsertElementConstantExpr
272     return Builder.CreateInsertElement(NewOperands[0], NewOperands[1],
273                                        NewOperands[2]);
274   case Instruction::ShuffleVector:
275     // ShuffleVector
276     return Builder.CreateShuffleVector(NewOperands[0], NewOperands[1],
277                                        NewOperands[2]);
278   case Instruction::ExtractValue:
279     // ExtractValueConstantExpr
280     return Builder.CreateExtractValue(NewOperands[0], C->getIndices());
281   case Instruction::InsertValue:
282     // InsertValueConstantExpr
283     return Builder.CreateInsertValue(NewOperands[0], NewOperands[1],
284                                      C->getIndices());
285   case Instruction::GetElementPtr:
286     // GetElementPtrConstantExpr
287     return cast<GEPOperator>(C)->isInBounds()
288                ? Builder.CreateGEP(
289                      cast<GEPOperator>(C)->getSourceElementType(),
290                      NewOperands[0],
291                      makeArrayRef(&NewOperands[1], NumOperands - 1))
292                : Builder.CreateInBoundsGEP(
293                      cast<GEPOperator>(C)->getSourceElementType(),
294                      NewOperands[0],
295                      makeArrayRef(&NewOperands[1], NumOperands - 1));
296   case Instruction::Select:
297     // SelectConstantExpr
298     return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
299   default:
300     // BinaryConstantExpr
301     if (Instruction::isBinaryOp(Opcode)) {
302       return Builder.CreateBinOp(Instruction::BinaryOps(C->getOpcode()),
303                                  NewOperands[0], NewOperands[1]);
304     }
305     // UnaryConstantExpr
306     if (Instruction::isCast(Opcode)) {
307       return Builder.CreateCast(Instruction::CastOps(C->getOpcode()),
308                                 NewOperands[0], C->getType());
309     }
310     llvm_unreachable("GenericToNVVM encountered an unsupported ConstantExpr");
311   }
312 }
313