1 //===- IntrinsicInstTest.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/SandboxIR/IntrinsicInst.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/BasicBlock.h"
12 #include "llvm/IR/Function.h"
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/Value.h"
16 #include "llvm/SandboxIR/Context.h"
17 #include "llvm/SandboxIR/Function.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "gtest/gtest.h"
23 struct IntrinsicInstTest
: public testing::Test
{
25 std::unique_ptr
<Module
> M
;
27 void parseIR(LLVMContext
&C
, const char *IR
) {
29 M
= parseAssemblyString(IR
, Err
, C
);
31 Err
.print("SandboxIRTest", errs());
33 BasicBlock
*getBasicBlockByName(Function
&F
, StringRef Name
) {
34 for (BasicBlock
&BB
: F
)
35 if (BB
.getName() == Name
)
37 llvm_unreachable("Expected to find basic block!");
41 TEST_F(IntrinsicInstTest
, Basic
) {
43 declare void @llvm.sideeffect()
44 declare void @llvm.assume(i1)
45 declare i8 @llvm.uadd.sat.i8(i8, i8)
46 declare i8 @llvm.smax.i8(i8, i8)
48 define void @foo(i8 %v1, i1 %cond) {
49 call void @llvm.sideeffect()
50 call void @llvm.assume(i1 %cond)
51 call i8 @llvm.uadd.sat.i8(i8 %v1, i8 %v1)
52 call i8 @llvm.smax.i8(i8 %v1, i8 %v1)
57 llvm::Function
*LLVMF
= &*M
->getFunction("foo");
58 auto *LLVMBB
= &*LLVMF
->begin();
59 auto LLVMIt
= LLVMBB
->begin();
61 sandboxir::Context
Ctx(C
);
62 sandboxir::Function
*F
= Ctx
.createFunction(LLVMF
);
63 auto *BB
= &*F
->begin();
64 auto It
= BB
->begin();
65 auto ItE
= BB
->getTerminator()->getIterator();
66 for (; It
!= ItE
; ++It
, ++LLVMIt
) {
68 auto *LLVMI
= &*LLVMIt
;
70 EXPECT_TRUE(isa
<sandboxir::IntrinsicInst
>(I
));
71 // Check getIntrinsicID().
72 EXPECT_EQ(cast
<sandboxir::IntrinsicInst
>(I
)->getIntrinsicID(),
73 cast
<llvm::IntrinsicInst
>(LLVMI
)->getIntrinsicID());
74 // Check isAssociative().
75 EXPECT_EQ(cast
<sandboxir::IntrinsicInst
>(I
)->isAssociative(),
76 cast
<llvm::IntrinsicInst
>(LLVMI
)->isAssociative());
77 // Check isCommutative().
78 EXPECT_EQ(cast
<sandboxir::IntrinsicInst
>(I
)->isCommutative(),
79 cast
<llvm::IntrinsicInst
>(LLVMI
)->isCommutative());
80 // Check isAssumeLikeIntrinsic().
81 EXPECT_EQ(cast
<sandboxir::IntrinsicInst
>(I
)->isAssumeLikeIntrinsic(),
82 cast
<llvm::IntrinsicInst
>(LLVMI
)->isAssumeLikeIntrinsic());
83 // Check mayLowerToFunctionCall().
84 auto ID
= cast
<sandboxir::IntrinsicInst
>(I
)->getIntrinsicID();
85 EXPECT_EQ(sandboxir::IntrinsicInst::mayLowerToFunctionCall(ID
),
86 llvm::IntrinsicInst::mayLowerToFunctionCall(ID
));