1 //===- llvm/unittest/IR/IntrinsicsTest.cpp - ------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Intrinsics.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/IR/Constant.h"
12 #include "llvm/IR/IRBuilder.h"
13 #include "llvm/IR/IntrinsicInst.h"
14 #include "llvm/IR/Module.h"
15 #include "gtest/gtest.h"
21 static const char *const NameTable1
[] = {
22 "llvm.foo", "llvm.foo.a", "llvm.foo.b", "llvm.foo.b.a", "llvm.foo.c",
25 class IntrinsicsTest
: public ::testing::Test
{
27 std::unique_ptr
<Module
> M
;
28 BasicBlock
*BB
= nullptr;
30 void TearDown() override
{ M
.reset(); }
32 void SetUp() override
{
33 M
= std::make_unique
<Module
>("Test", Context
);
34 auto F
= M
->getOrInsertFunction(
35 "test", FunctionType::get(Type::getVoidTy(Context
), false));
36 BB
= BasicBlock::Create(Context
, "", cast
<Function
>(F
.getCallee()));
37 EXPECT_NE(BB
, nullptr);
41 Instruction
*makeIntrinsic(Intrinsic::ID ID
) const {
42 IRBuilder
<> Builder(BB
);
43 SmallVector
<Value
*, 4> ProcessedArgs
;
44 auto *Decl
= Intrinsic::getDeclaration(M
.get(), ID
);
45 for (auto *Ty
: Decl
->getFunctionType()->params()) {
46 auto *Val
= Constant::getNullValue(Ty
);
47 ProcessedArgs
.push_back(Val
);
49 return Builder
.CreateCall(Decl
, ProcessedArgs
);
51 template <typename T
> void checkIsa(const Instruction
&I
) {
52 EXPECT_TRUE(isa
<T
>(I
));
56 TEST(IntrinsicNameLookup
, Basic
) {
57 int I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo");
59 I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo.f64");
61 I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo.b");
63 I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo.b.a");
65 I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo.c");
67 I
= Intrinsic::lookupLLVMIntrinsicByName(NameTable1
, "llvm.foo.c.f64");
71 TEST_F(IntrinsicsTest
, InstrProfInheritance
) {
72 auto isInstrProfInstBase
= [](const Instruction
&I
) {
73 return isa
<InstrProfInstBase
>(I
);
75 #define __ISA(TYPE, PARENT) \
76 auto is##TYPE = [&](const Instruction &I) -> bool { \
77 return isa<TYPE>(I) && is##PARENT(I); \
79 __ISA(InstrProfCntrInstBase
, InstrProfInstBase
);
80 __ISA(InstrProfCoverInst
, InstrProfCntrInstBase
);
81 __ISA(InstrProfIncrementInst
, InstrProfCntrInstBase
);
82 __ISA(InstrProfIncrementInstStep
, InstrProfIncrementInst
);
83 __ISA(InstrProfCallsite
, InstrProfCntrInstBase
);
84 __ISA(InstrProfTimestampInst
, InstrProfCntrInstBase
);
85 __ISA(InstrProfValueProfileInst
, InstrProfCntrInstBase
);
86 __ISA(InstrProfMCDCBitmapInstBase
, InstrProfInstBase
);
87 __ISA(InstrProfMCDCBitmapParameters
, InstrProfMCDCBitmapInstBase
);
88 __ISA(InstrProfMCDCTVBitmapUpdate
, InstrProfMCDCBitmapInstBase
);
92 std::pair
<Intrinsic::ID
, std::function
<bool(const Instruction
&)>>>
94 {Intrinsic::instrprof_cover
, isInstrProfCoverInst
},
95 {Intrinsic::instrprof_increment
, isInstrProfIncrementInst
},
96 {Intrinsic::instrprof_increment_step
, isInstrProfIncrementInstStep
},
97 {Intrinsic::instrprof_callsite
, isInstrProfCallsite
},
98 {Intrinsic::instrprof_mcdc_parameters
,
99 isInstrProfMCDCBitmapParameters
},
100 {Intrinsic::instrprof_mcdc_tvbitmap_update
,
101 isInstrProfMCDCTVBitmapUpdate
},
102 {Intrinsic::instrprof_timestamp
, isInstrProfTimestampInst
},
103 {Intrinsic::instrprof_value_profile
, isInstrProfValueProfileInst
}};
104 for (const auto &[ID
, Checker
] : LeafIDs
) {
105 auto *Intr
= makeIntrinsic(ID
);
106 EXPECT_TRUE(Checker(*Intr
));