| 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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 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" |
| 20 |
|
| 21 |
using namespace llvm; |
| 22 |
|
| 23 |
struct IntrinsicInstTest : public testing::Test { |
| 24 |
LLVMContext C; |
| 25 |
std::unique_ptr<Module> M; |
| 26 |
|
| 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()); |
| 32 |
} |
| 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!"); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 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) |
| 47 |
|
| 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 |
| 54 |
} |
| 55 |
)IR"); |
| 56 |
|
| 57 |
llvm::Function *LLVMF = &*M->getFunction("foo"); |
| 58 |
auto *LLVMBB = &*LLVMF->begin(); |
| 59 |
auto LLVMIt = LLVMBB->begin(); |
| 60 |
|
| 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)); |
| 87 |
} |
| 88 |
} |