1 //===- ValueMapper.cpp - Unit tests for ValueMapper -----------------------===//
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 #include "llvm/IR/LLVMContext.h"
11 #include "llvm/IR/Metadata.h"
12 #include "llvm/Transforms/Utils/ValueMapper.h"
13 #include "gtest/gtest.h"
14 
15 using namespace llvm;
16 
17 namespace {
18 
TEST(ValueMapperTest,MapMetadataUnresolved)19 TEST(ValueMapperTest, MapMetadataUnresolved) {
20   LLVMContext Context;
21   TempMDTuple T = MDTuple::getTemporary(Context, None);
22 
23   ValueToValueMapTy VM;
24   EXPECT_EQ(T.get(), MapMetadata(T.get(), VM, RF_NoModuleLevelChanges));
25 }
26 
TEST(ValueMapperTest,MapMetadataDistinct)27 TEST(ValueMapperTest, MapMetadataDistinct) {
28   LLVMContext Context;
29   auto *D = MDTuple::getDistinct(Context, None);
30 
31   {
32     // The node should be cloned.
33     ValueToValueMapTy VM;
34     EXPECT_NE(D, MapMetadata(D, VM, RF_None));
35   }
36   {
37     // The node should be moved.
38     ValueToValueMapTy VM;
39     EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
40   }
41 }
42 
TEST(ValueMapperTest,MapMetadataDistinctOperands)43 TEST(ValueMapperTest, MapMetadataDistinctOperands) {
44   LLVMContext Context;
45   Metadata *Old = MDTuple::getDistinct(Context, None);
46   auto *D = MDTuple::getDistinct(Context, Old);
47   ASSERT_EQ(Old, D->getOperand(0));
48 
49   Metadata *New = MDTuple::getDistinct(Context, None);
50   ValueToValueMapTy VM;
51   VM.MD()[Old].reset(New);
52 
53   // Make sure operands are updated.
54   EXPECT_EQ(D, MapMetadata(D, VM, RF_MoveDistinctMDs));
55   EXPECT_EQ(New, D->getOperand(0));
56 }
57 
58 }
59