| 1 |
//===- MCInstrAnalysisTest.cpp - LoongArchMCInstrAnalysis unit tests ------===// |
| 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/MC/MCInstrAnalysis.h" |
| 10 |
#include "MCTargetDesc/LoongArchMCTargetDesc.h" |
| 11 |
#include "llvm/MC/MCInstBuilder.h" |
| 12 |
#include "llvm/MC/TargetRegistry.h" |
| 13 |
#include "llvm/Support/TargetSelect.h" |
| 14 |
|
| 15 |
#include "gtest/gtest.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
|
| 19 |
using namespace llvm; |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
class InstrAnalysisTest : public testing::TestWithParam<const char *> { |
| 24 |
protected: |
| 25 |
std::unique_ptr<const MCInstrInfo> Info; |
| 26 |
std::unique_ptr<const MCInstrAnalysis> Analysis; |
| 27 |
|
| 28 |
static void SetUpTestSuite() { |
| 29 |
LLVMInitializeLoongArchTargetInfo(); |
| 30 |
LLVMInitializeLoongArchTarget(); |
| 31 |
LLVMInitializeLoongArchTargetMC(); |
| 32 |
} |
| 33 |
|
| 34 |
InstrAnalysisTest() { |
| 35 |
std::string Error; |
| 36 |
const Target *TheTarget = |
| 37 |
TargetRegistry::lookupTarget(Triple::normalize(GetParam()), Error); |
| 38 |
Info = std::unique_ptr<const MCInstrInfo>(TheTarget->createMCInstrInfo()); |
| 39 |
Analysis = std::unique_ptr<const MCInstrAnalysis>( |
| 40 |
TheTarget->createMCInstrAnalysis(Info.get())); |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
} // namespace |
| 45 |
|
| 46 |
static MCInst beq() { |
| 47 |
return MCInstBuilder(LoongArch::BEQ) |
| 48 |
.addReg(LoongArch::R0) |
| 49 |
.addReg(LoongArch::R1) |
| 50 |
.addImm(32); |
| 51 |
} |
| 52 |
|
| 53 |
static MCInst b() { return MCInstBuilder(LoongArch::B).addImm(32); } |
| 54 |
|
| 55 |
static MCInst bl() { return MCInstBuilder(LoongArch::BL).addImm(32); } |
| 56 |
|
| 57 |
static MCInst jirl(unsigned RD, unsigned RJ = LoongArch::R10) { |
| 58 |
return MCInstBuilder(LoongArch::JIRL).addReg(RD).addReg(RJ).addImm(16); |
| 59 |
} |
| 60 |
|
| 61 |
TEST_P(InstrAnalysisTest, IsTerminator) { |
| 62 |
EXPECT_TRUE(Analysis->isTerminator(beq())); |
| 63 |
EXPECT_TRUE(Analysis->isTerminator(b())); |
| 64 |
EXPECT_FALSE(Analysis->isTerminator(bl())); |
| 65 |
EXPECT_TRUE(Analysis->isTerminator(jirl(LoongArch::R0))); |
| 66 |
EXPECT_FALSE(Analysis->isTerminator(jirl(LoongArch::R5))); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_P(InstrAnalysisTest, IsCall) { |
| 70 |
EXPECT_FALSE(Analysis->isCall(beq())); |
| 71 |
EXPECT_FALSE(Analysis->isCall(b())); |
| 72 |
EXPECT_TRUE(Analysis->isCall(bl())); |
| 73 |
EXPECT_TRUE(Analysis->isCall(jirl(LoongArch::R1))); |
| 74 |
EXPECT_FALSE(Analysis->isCall(jirl(LoongArch::R0))); |
| 75 |
} |
| 76 |
|
| 77 |
TEST_P(InstrAnalysisTest, IsReturn) { |
| 78 |
EXPECT_FALSE(Analysis->isReturn(beq())); |
| 79 |
EXPECT_FALSE(Analysis->isReturn(b())); |
| 80 |
EXPECT_FALSE(Analysis->isReturn(bl())); |
| 81 |
EXPECT_TRUE(Analysis->isReturn(jirl(LoongArch::R0, LoongArch::R1))); |
| 82 |
EXPECT_FALSE(Analysis->isReturn(jirl(LoongArch::R0))); |
| 83 |
EXPECT_FALSE(Analysis->isReturn(jirl(LoongArch::R1))); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_P(InstrAnalysisTest, IsBranch) { |
| 87 |
EXPECT_TRUE(Analysis->isBranch(beq())); |
| 88 |
EXPECT_TRUE(Analysis->isBranch(b())); |
| 89 |
EXPECT_FALSE(Analysis->isBranch(bl())); |
| 90 |
EXPECT_TRUE(Analysis->isBranch(jirl(LoongArch::R0))); |
| 91 |
EXPECT_FALSE(Analysis->isBranch(jirl(LoongArch::R1))); |
| 92 |
EXPECT_FALSE(Analysis->isBranch(jirl(LoongArch::R0, LoongArch::R1))); |
| 93 |
} |
| 94 |
|
| 95 |
TEST_P(InstrAnalysisTest, IsConditionalBranch) { |
| 96 |
EXPECT_TRUE(Analysis->isConditionalBranch(beq())); |
| 97 |
EXPECT_FALSE(Analysis->isConditionalBranch(b())); |
| 98 |
EXPECT_FALSE(Analysis->isConditionalBranch(bl())); |
| 99 |
} |
| 100 |
|
| 101 |
TEST_P(InstrAnalysisTest, IsUnconditionalBranch) { |
| 102 |
EXPECT_FALSE(Analysis->isUnconditionalBranch(beq())); |
| 103 |
EXPECT_TRUE(Analysis->isUnconditionalBranch(b())); |
| 104 |
EXPECT_FALSE(Analysis->isUnconditionalBranch(bl())); |
| 105 |
EXPECT_TRUE(Analysis->isUnconditionalBranch(jirl(LoongArch::R0))); |
| 106 |
EXPECT_FALSE(Analysis->isUnconditionalBranch(jirl(LoongArch::R1))); |
| 107 |
EXPECT_FALSE( |
| 108 |
Analysis->isUnconditionalBranch(jirl(LoongArch::R0, LoongArch::R1))); |
| 109 |
} |
| 110 |
|
| 111 |
TEST_P(InstrAnalysisTest, IsIndirectBranch) { |
| 112 |
EXPECT_FALSE(Analysis->isIndirectBranch(beq())); |
| 113 |
EXPECT_FALSE(Analysis->isIndirectBranch(b())); |
| 114 |
EXPECT_FALSE(Analysis->isIndirectBranch(bl())); |
| 115 |
EXPECT_TRUE(Analysis->isIndirectBranch(jirl(LoongArch::R0))); |
| 116 |
EXPECT_FALSE(Analysis->isIndirectBranch(jirl(LoongArch::R1))); |
| 117 |
EXPECT_FALSE(Analysis->isIndirectBranch(jirl(LoongArch::R0, LoongArch::R1))); |
| 118 |
} |
| 119 |
|
| 120 |
INSTANTIATE_TEST_SUITE_P(LA32And64, InstrAnalysisTest, |
| 121 |
testing::Values("loongarch32", "loongarch64")); |