IR: de-duplicate two CmpInst routines (NFC) (#116866)
[llvm-project.git] / llvm / unittests / SandboxIR / IntrinsicInstTest.cpp
blob2272cfc1146169238fbd1b356143cf72e1f1c7f5
1 //===- IntrinsicInstTest.cpp ----------------------------------------------===//
2 //
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
6 //
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"
21 using namespace llvm;
23 struct IntrinsicInstTest : public testing::Test {
24 LLVMContext C;
25 std::unique_ptr<Module> M;
27 void parseIR(LLVMContext &C, const char *IR) {
28 SMDiagnostic Err;
29 M = parseAssemblyString(IR, Err, C);
30 if (!M)
31 Err.print("SandboxIRTest", errs());
33 BasicBlock *getBasicBlockByName(Function &F, StringRef Name) {
34 for (BasicBlock &BB : F)
35 if (BB.getName() == Name)
36 return &BB;
37 llvm_unreachable("Expected to find basic block!");
41 TEST_F(IntrinsicInstTest, Basic) {
42 parseIR(C, R"IR(
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)
53 ret void
55 )IR");
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) {
67 auto *I = &*It;
68 auto *LLVMI = &*LLVMIt;
69 // Check classof().
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));