llvm.org GIT mirror llvm / 1f62091
NVPTX: Use MapMetadata() instead of custom/stale/untested logic Copy the `GVMap` over to a standard `ValueToValueMapTy` so that we can reuse the `MapMetadata()` logic. Unfortunately the `GVMap` can't just be replaced, since `MapMetadata()` likes to modify the map, but at least this will prevent NVPTX from bitrotting. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225944 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
1 changed file(s) with 10 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
2525 #include "llvm/IR/Operator.h"
2626 #include "llvm/IR/ValueMap.h"
2727 #include "llvm/PassManager.h"
28 #include "llvm/Transforms/Utils/ValueMapper.h"
2829
2930 using namespace llvm;
3031
5354 IRBuilder<> &Builder);
5455 Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
5556 IRBuilder<> &Builder);
56 void remapNamedMDNode(Module *M, NamedMDNode *N);
57 MDNode *remapMDNode(Module *M, MDNode *N);
57 void remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N);
5858
5959 typedef ValueMap GVMapTy;
6060 typedef ValueMap ConstantToValueMapTy;
124124 ConstantToValueMap.clear();
125125 }
126126
127 // Copy GVMap over to a standard value map.
128 ValueToValueMapTy VM;
129 for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
130 VM[I->first] = I->second;
131
127132 // Walk through the metadata section and update the debug information
128133 // associated with the global variables in the default address space.
129134 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
130135 E = M.named_metadata_end();
131136 I != E; I++) {
132 remapNamedMDNode(&M, I);
137 remapNamedMDNode(VM, I);
133138 }
134139
135140 // Walk through the global variable initializers, and replace any use of
361366 }
362367 }
363368
364 void GenericToNVVM::remapNamedMDNode(Module *M, NamedMDNode *N) {
369 void GenericToNVVM::remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N) {
365370
366371 bool OperandChanged = false;
367372 SmallVector NewOperands;
371376 // converted to another value.
372377 for (unsigned i = 0; i < NumOperands; ++i) {
373378 MDNode *Operand = N->getOperand(i);
374 MDNode *NewOperand = remapMDNode(M, Operand);
379 MDNode *NewOperand = MapMetadata(Operand, VM);
375380 OperandChanged |= Operand != NewOperand;
376381 NewOperands.push_back(NewOperand);
377382 }
389394 N->addOperand(*I);
390395 }
391396 }
392
393 MDNode *GenericToNVVM::remapMDNode(Module *M, MDNode *N) {
394
395 bool OperandChanged = false;
396 SmallVector NewOperands;
397 unsigned NumOperands = N->getNumOperands();
398
399 // Check if any operand is or contains a global variable in GVMap, and thus
400 // converted to another value.
401 for (unsigned i = 0; i < NumOperands; ++i) {
402 Metadata *Operand = N->getOperand(i);
403 Metadata *NewOperand = Operand;
404 if (Operand) {
405 if (auto *N = dyn_cast(Operand)) {
406 NewOperand = remapMDNode(M, N);
407 } else if (auto *C = dyn_cast(Operand)) {
408 if (auto *G = dyn_cast(C->getValue())) {
409 GVMapTy::iterator I = GVMap.find(G);
410 if (I != GVMap.end())
411 NewOperand = ConstantAsMetadata::get(I->second);
412 }
413 }
414 }
415 OperandChanged |= Operand != NewOperand;
416 NewOperands.push_back(NewOperand);
417 }
418
419 // If none of the operands has been modified, return N as it is.
420 if (!OperandChanged) {
421 return N;
422 }
423
424 // If any of the operands has been modified, create a new MDNode with the new
425 // operands.
426 return MDNode::get(M->getContext(), makeArrayRef(NewOperands));
427 }