LLVM: lib/Target/NVPTX/NVPTXGenericToNVVM.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
28
29using namespace llvm;
30
31namespace {
32class GenericToNVVM {
33public:
34 bool runOnModule(Module &M);
35
36private:
37 Value *remapConstant(Module *M, Function *F, Constant *C,
39 Value *remapConstantVectorOrConstantAggregate(Module *M, Function *F,
40 Constant *C,
42 Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
44
45 typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
46 typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
47 GVMapTy GVMap;
48 ConstantToValueMapTy ConstantToValueMap;
49};
50}
51
52bool GenericToNVVM::runOnModule(Module &M) {
53
54
55
56
57
61 !GV.getName().starts_with("llvm.")) {
62 GlobalVariable *NewGV = new GlobalVariable(
63 M, GV.getValueType(), GV.isConstant(), GV.getLinkage(),
64 GV.hasInitializer() ? GV.getInitializer() : nullptr, "", &GV,
68 GVMap[&GV] = NewGV;
69 }
70 }
71
72
73
74 if (GVMap.empty()) {
75 return false;
76 }
77
78
79
80
81 for (Function &F : M) {
82 if (F.isDeclaration()) {
83 continue;
84 }
85 IRBuilder<> Builder(&*F.getEntryBlock().getFirstNonPHIOrDbg());
86 for (BasicBlock &BB : F) {
87 for (Instruction &II : BB) {
88 for (unsigned i = 0, e = II.getNumOperands(); i < e; ++i) {
89 Value *Operand = II.getOperand(i);
91 II.setOperand(
92 i, remapConstant(&M, &F, cast(Operand), Builder));
93 }
94 }
95 }
96 }
97 ConstantToValueMap.clear();
98 }
99
100
102 for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
104
105
106
107
108
109 for (GVMapTy::iterator I = GVMap.begin(), E = GVMap.end(); I != E;) {
110 GlobalVariable *GV = I->first;
111 GlobalVariable *NewGV = I->second;
112
113
114
118
120
121
122
124 std::string Name = std::string(GV->getName());
127 }
128 assert(GVMap.empty() && "Expected it to be empty by now");
129
130 return true;
131}
132
133Value *GenericToNVVM::remapConstant(Module *M, Function *F, Constant *C,
135
136
137 ConstantToValueMapTy::iterator CTII = ConstantToValueMap.find(C);
138 if (CTII != ConstantToValueMap.end()) {
139 return CTII->second;
140 }
141
144
145
146
147
148
151 GlobalVariable *GV = I->second;
154 }
156
157
158
159 NewValue = remapConstantVectorOrConstantAggregate(M, F, C, Builder);
161
162
163
165 }
166
167 ConstantToValueMap[C] = NewValue;
168 return NewValue;
169}
170
171Value *GenericToNVVM::remapConstantVectorOrConstantAggregate(
173 bool OperandChanged = false;
175 unsigned NumOperands = C->getNumOperands();
176
177
178
179 for (unsigned i = 0; i < NumOperands; ++i) {
180 Value *Operand = C->getOperand(i);
182 OperandChanged |= Operand != NewOperand;
183 NewOperands.push_back(NewOperand);
184 }
185
186
187 if (!OperandChanged) {
188 return C;
189 }
190
191
192
193
196 for (unsigned i = 0; i < NumOperands; ++i) {
197 Value *Idx = ConstantInt::get(Type::getInt32Ty(M->getContext()), i);
199 }
200 } else {
201 for (unsigned i = 0; i < NumOperands; ++i) {
202 NewValue =
204 }
205 }
206
207 return NewValue;
208}
209
210Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
212 bool OperandChanged = false;
214 unsigned NumOperands = C->getNumOperands();
215
216
217
218 for (unsigned i = 0; i < NumOperands; ++i) {
219 Value *Operand = C->getOperand(i);
221 OperandChanged |= Operand != NewOperand;
222 NewOperands.push_back(NewOperand);
223 }
224
225
226 if (!OperandChanged) {
227 return C;
228 }
229
230
231
232 unsigned Opcode = C->getOpcode();
233 switch (Opcode) {
234 case Instruction::ExtractElement:
235
237 case Instruction::InsertElement:
238
240 NewOperands[2]);
241 case Instruction::ShuffleVector:
242
244 NewOperands[2]);
245 case Instruction::GetElementPtr:
246
248 NewOperands[0],
249 ArrayRef(&NewOperands[1], NumOperands - 1), "",
251 case Instruction::Select:
252
253 return Builder.CreateSelect(NewOperands[0], NewOperands[1], NewOperands[2]);
254 default:
255
258 NewOperands[0], NewOperands[1]);
259 }
260
263 NewOperands[0], C->getType());
264 }
265 llvm_unreachable("GenericToNVVM encountered an unsupported ConstantExpr");
266 }
267}
268
269namespace {
270class GenericToNVVMLegacyPass : public ModulePass {
271public:
272 static char ID;
273
274 GenericToNVVMLegacyPass() : ModulePass(ID) {}
275
276 bool runOnModule(Module &M) override;
277};
278}
279
280char GenericToNVVMLegacyPass::ID = 0;
281
283 return new GenericToNVVMLegacyPass();
284}
285
287 GenericToNVVMLegacyPass, "generic-to-nvvm",
288 "Ensure that the global variables are in the global address space", false,
289 false)
290
291bool GenericToNVVMLegacyPass::runOnModule(Module &M) {
292 return GenericToNVVM().runOnModule(M);
293}
294
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static LLVM_ABI Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
LLVM_ABI void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
PointerType * getType() const
Global values are always pointers.
LLVM_ABI void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
LLVM_ABI void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="", MDNode *FPMathTag=nullptr, FMFSource FMFSource={})
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Value * CreateAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void push_back(const T &Elt)
iterator find(const KeyT &Val)
bool erase(const KeyT &Val)
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
ModulePass * createGenericToNVVMLegacyPass()
Definition NVPTXGenericToNVVM.cpp:282
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool isSampler(const Value &V)
bool isSurface(const Value &V)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
FunctionAddr VTableAddr Next
ArrayRef(const T &OneElt) -> ArrayRef< T >
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
bool isTexture(const Value &V)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition NVPTXGenericToNVVM.cpp:295