From: Duncan P. N. Exon Smith Date: Sat, 2 Apr 2016 17:04:38 +0000 (+0000) Subject: ValueMapper: Add support for seeding metadata with nullptr X-Git-Tag: android-x86-7.1-r4~35733 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=eeb2c7e32ce9069fbf49c5f0b2c1b9d68edd3f1c;p=android-x86%2Fexternal-llvm.git ValueMapper: Add support for seeding metadata with nullptr Support seeding a ValueMap with nullptr for Metadata entries, a situation I didn't consider in the Metadata/Value split. I added a ValueMapper::getMappedMD accessor that returns an Optional with the mapped (possibly null) metadata. IRMover needs to use this to avoid modifying the map when it's checking for unneeded subprograms. I updated a call from bugpoint since I find the new code clearer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265228 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/ValueMap.h b/include/llvm/IR/ValueMap.h index ad518ac053b..3425120ca42 100644 --- a/include/llvm/IR/ValueMap.h +++ b/include/llvm/IR/ValueMap.h @@ -27,6 +27,7 @@ #define LLVM_IR_VALUEMAP_H #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" #include "llvm/IR/TrackingMDRef.h" #include "llvm/IR/ValueHandle.h" #include "llvm/Support/Mutex.h" @@ -106,6 +107,16 @@ public: return *MDMap; } + /// Get the mapped metadata, if it's in the map. + Optional getMappedMD(const Metadata *MD) const { + if (!MDMap) + return None; + auto Where = MDMap->find(MD); + if (Where == MDMap->end()) + return None; + return Where->second.get(); + } + typedef ValueMapIterator iterator; typedef ValueMapConstIterator const_iterator; inline iterator begin() { return iterator(Map.begin()); } diff --git a/lib/Linker/IRMover.cpp b/lib/Linker/IRMover.cpp index f67bacedf1b..a9e5779fd46 100644 --- a/lib/Linker/IRMover.cpp +++ b/lib/Linker/IRMover.cpp @@ -1091,7 +1091,7 @@ void IRLinker::findNeededSubprograms() { // Any needed SPs should have been mapped as they would be reached // from the function linked in (either on the function itself for linked // function bodies, or from DILocation on inlined instructions). - if (!ValueMap.MD()[Op] && !ImportedEntitySPs.count(Op)) + if (!ValueMap.getMappedMD(Op) && !ImportedEntitySPs.count(Op)) UnneededSubprograms.insert(Op); } } diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index b658ffba0c2..a72c456ef0f 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -305,8 +305,8 @@ static Metadata *MapMetadataImpl(const Metadata *MD, ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer) { // If the value already exists in the map, use it. - if (Metadata *NewMD = VM.MD().lookup(MD).get()) - return NewMD; + if (Optional NewMD = VM.getMappedMD(MD)) + return *NewMD; if (isa(MD)) return mapToSelf(VM, MD); @@ -380,8 +380,8 @@ Metadata *llvm::MapMetadata(const Metadata *MD, ValueToValueMapTy &VM, MDNode *llvm::MapMetadata(const MDNode *MD, ValueToValueMapTy &VM, RemapFlags Flags, ValueMapTypeRemapper *TypeMapper, ValueMaterializer *Materializer) { - return cast(MapMetadata(static_cast(MD), VM, Flags, - TypeMapper, Materializer)); + return cast_or_null(MapMetadata(static_cast(MD), VM, + Flags, TypeMapper, Materializer)); } /// RemapInstruction - Convert the instruction operands from referencing the diff --git a/tools/bugpoint/CrashDebugger.cpp b/tools/bugpoint/CrashDebugger.cpp index 493bda64f19..d494007665a 100644 --- a/tools/bugpoint/CrashDebugger.cpp +++ b/tools/bugpoint/CrashDebugger.cpp @@ -639,7 +639,7 @@ bool ReduceCrashingNamedMDOps::TestNamedMDOps( // module, and that they don't include any deleted blocks. NamedMDOps.clear(); for (const MDNode *Node : OldMDNodeOps) - NamedMDOps.push_back(cast(VMap.MD()[Node].get())); + NamedMDOps.push_back(cast(*VMap.getMappedMD(Node))); BD.setNewProgram(M); // It crashed, keep the trimmed version... return true; diff --git a/unittests/Transforms/Utils/ValueMapperTest.cpp b/unittests/Transforms/Utils/ValueMapperTest.cpp index 6141c416f9a..3c7ef1b686a 100644 --- a/unittests/Transforms/Utils/ValueMapperTest.cpp +++ b/unittests/Transforms/Utils/ValueMapperTest.cpp @@ -55,4 +55,30 @@ TEST(ValueMapperTest, MapMetadataDistinctOperands) { EXPECT_EQ(New, D->getOperand(0)); } +TEST(ValueMapperTest, MapMetadataSeeded) { + LLVMContext Context; + auto *D = MDTuple::getDistinct(Context, None); + + // The node should be moved. + ValueToValueMapTy VM; + EXPECT_EQ(None, VM.getMappedMD(D)); + + VM.MD().insert(std::make_pair(D, TrackingMDRef(D))); + EXPECT_EQ(D, *VM.getMappedMD(D)); + EXPECT_EQ(D, MapMetadata(D, VM, RF_None)); +} + +TEST(ValueMapperTest, MapMetadataSeededWithNull) { + LLVMContext Context; + auto *D = MDTuple::getDistinct(Context, None); + + // The node should be moved. + ValueToValueMapTy VM; + EXPECT_EQ(None, VM.getMappedMD(D)); + + VM.MD().insert(std::make_pair(D, TrackingMDRef())); + EXPECT_EQ(nullptr, *VM.getMappedMD(D)); + EXPECT_EQ(nullptr, MapMetadata(D, VM, RF_None)); +} + } // end namespace