[Clang][MIPS] Send correct architecture for MinGW toolchains (#121042)
[llvm-project.git] / llvm / unittests / Target / RISCV / MCInstrAnalysisTest.cpp
blob2ef92c7f30f68b4fad2fcddac611d25344ad1456
1 //===- MCInstrAnalysisTest.cpp - RISCVMCInstrAnalysis 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 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCInstrAnalysis.h"
10 #include "MCTargetDesc/RISCVMCTargetDesc.h"
11 #include "llvm/MC/MCInstBuilder.h"
12 #include "llvm/MC/TargetRegistry.h"
13 #include "llvm/Support/TargetSelect.h"
15 #include "gtest/gtest.h"
17 #include <memory>
19 using namespace llvm;
21 namespace {
23 class InstrAnalysisTest : public testing::TestWithParam<const char *> {
24 protected:
25 std::unique_ptr<const MCInstrInfo> Info;
26 std::unique_ptr<const MCInstrAnalysis> Analysis;
28 static void SetUpTestSuite() {
29 LLVMInitializeRISCVTargetInfo();
30 LLVMInitializeRISCVTarget();
31 LLVMInitializeRISCVTargetMC();
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()));
44 } // namespace
46 static MCInst jal(unsigned RD) {
47 return MCInstBuilder(RISCV::JAL).addReg(RD).addImm(16);
50 static MCInst jalr(unsigned RD, unsigned RS1 = RISCV::X10) {
51 return MCInstBuilder(RISCV::JALR).addReg(RD).addReg(RS1).addImm(16);
54 static MCInst cjr(unsigned RS1) {
55 return MCInstBuilder(RISCV::C_JR).addReg(RS1);
58 static MCInst cj() { return MCInstBuilder(RISCV::C_J).addImm(16); }
59 static MCInst cjal() { return MCInstBuilder(RISCV::C_JAL).addImm(16); }
61 static MCInst cjalr(unsigned RS1) {
62 return MCInstBuilder(RISCV::C_JALR).addReg(RS1);
65 static MCInst beq() {
66 return MCInstBuilder(RISCV::BEQ)
67 .addReg(RISCV::X0)
68 .addReg(RISCV::X1)
69 .addImm(32);
72 static MCInst cbeqz() {
73 return MCInstBuilder(RISCV::C_BEQZ).addReg(RISCV::X1).addImm(32);
76 TEST_P(InstrAnalysisTest, IsTerminator) {
77 EXPECT_TRUE(Analysis->isTerminator(beq()));
78 EXPECT_TRUE(Analysis->isTerminator(cbeqz()));
79 EXPECT_TRUE(Analysis->isTerminator(jal(RISCV::X0)));
80 EXPECT_FALSE(Analysis->isTerminator(jal(RISCV::X5)));
81 EXPECT_TRUE(Analysis->isTerminator(jalr(RISCV::X0)));
82 EXPECT_FALSE(Analysis->isTerminator(jalr(RISCV::X5)));
83 EXPECT_TRUE(Analysis->isTerminator(cj()));
84 EXPECT_FALSE(Analysis->isTerminator(cjal()));
87 TEST_P(InstrAnalysisTest, IsCall) {
88 EXPECT_FALSE(Analysis->isCall(beq()));
89 EXPECT_FALSE(Analysis->isCall(cbeqz()));
90 EXPECT_FALSE(Analysis->isCall(jal(RISCV::X0)));
91 EXPECT_TRUE(Analysis->isCall(jal(RISCV::X1)));
92 EXPECT_TRUE(Analysis->isCall(jalr(RISCV::X1, RISCV::X1)));
93 EXPECT_FALSE(Analysis->isCall(jalr(RISCV::X0, RISCV::X5)));
94 EXPECT_FALSE(Analysis->isCall(cj()));
95 EXPECT_FALSE(Analysis->isCall(cjr(RISCV::X5)));
96 EXPECT_TRUE(Analysis->isCall(cjal()));
97 EXPECT_TRUE(Analysis->isCall(cjalr(RISCV::X5)));
100 TEST_P(InstrAnalysisTest, IsReturn) {
101 EXPECT_FALSE(Analysis->isReturn(beq()));
102 EXPECT_FALSE(Analysis->isReturn(cbeqz()));
103 EXPECT_FALSE(Analysis->isReturn(jal(RISCV::X0)));
104 EXPECT_TRUE(Analysis->isReturn(jalr(RISCV::X0, RISCV::X1)));
105 EXPECT_FALSE(Analysis->isReturn(jalr(RISCV::X1, RISCV::X1)));
106 EXPECT_TRUE(Analysis->isReturn(jalr(RISCV::X0, RISCV::X5)));
107 EXPECT_FALSE(Analysis->isReturn(cj()));
108 EXPECT_TRUE(Analysis->isReturn(cjr(RISCV::X1)));
109 EXPECT_FALSE(Analysis->isReturn(cjr(RISCV::X2)));
110 EXPECT_TRUE(Analysis->isReturn(cjr(RISCV::X5)));
111 EXPECT_FALSE(Analysis->isReturn(cjal()));
112 EXPECT_FALSE(Analysis->isReturn(cjalr(RISCV::X1)));
113 EXPECT_FALSE(Analysis->isReturn(cjalr(RISCV::X5)));
116 TEST_P(InstrAnalysisTest, IsBranch) {
117 EXPECT_TRUE(Analysis->isBranch(beq()));
118 EXPECT_TRUE(Analysis->isBranch(cbeqz()));
119 EXPECT_TRUE(Analysis->isBranch(jal(RISCV::X0)));
120 EXPECT_FALSE(Analysis->isBranch(jal(RISCV::X1)));
121 EXPECT_FALSE(Analysis->isBranch(jal(RISCV::X5)));
122 EXPECT_TRUE(Analysis->isBranch(jalr(RISCV::X0)));
123 EXPECT_FALSE(Analysis->isBranch(jalr(RISCV::X1)));
124 EXPECT_FALSE(Analysis->isBranch(jalr(RISCV::X5)));
125 EXPECT_FALSE(Analysis->isBranch(jalr(RISCV::X0, RISCV::X1)));
126 EXPECT_FALSE(Analysis->isBranch(jalr(RISCV::X0, RISCV::X5)));
127 EXPECT_TRUE(Analysis->isBranch(cj()));
128 EXPECT_TRUE(Analysis->isBranch(cjr(RISCV::X2)));
129 EXPECT_FALSE(Analysis->isBranch(cjr(RISCV::X1)));
130 EXPECT_FALSE(Analysis->isBranch(cjr(RISCV::X5)));
131 EXPECT_FALSE(Analysis->isBranch(cjal()));
132 EXPECT_FALSE(Analysis->isBranch(cjalr(RISCV::X6)));
133 EXPECT_FALSE(Analysis->isBranch(cjalr(RISCV::X1)));
134 EXPECT_FALSE(Analysis->isBranch(cjalr(RISCV::X5)));
137 TEST_P(InstrAnalysisTest, IsUnconditionalBranch) {
138 EXPECT_FALSE(Analysis->isUnconditionalBranch(beq()));
139 EXPECT_FALSE(Analysis->isUnconditionalBranch(cbeqz()));
140 EXPECT_TRUE(Analysis->isUnconditionalBranch(jal(RISCV::X0)));
141 EXPECT_FALSE(Analysis->isUnconditionalBranch(jal(RISCV::X1)));
142 EXPECT_FALSE(Analysis->isUnconditionalBranch(jal(RISCV::X5)));
143 EXPECT_TRUE(Analysis->isUnconditionalBranch(jalr(RISCV::X0)));
144 EXPECT_FALSE(Analysis->isUnconditionalBranch(jalr(RISCV::X1)));
145 EXPECT_FALSE(Analysis->isUnconditionalBranch(jalr(RISCV::X5)));
146 EXPECT_FALSE(Analysis->isUnconditionalBranch(jalr(RISCV::X0, RISCV::X1)));
147 EXPECT_FALSE(Analysis->isUnconditionalBranch(jalr(RISCV::X0, RISCV::X5)));
148 EXPECT_TRUE(Analysis->isUnconditionalBranch(cj()));
149 EXPECT_TRUE(Analysis->isUnconditionalBranch(cjr(RISCV::X2)));
150 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjr(RISCV::X1)));
151 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjr(RISCV::X5)));
152 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjal()));
153 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjalr(RISCV::X6)));
154 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjalr(RISCV::X1)));
155 EXPECT_FALSE(Analysis->isUnconditionalBranch(cjalr(RISCV::X5)));
158 TEST_P(InstrAnalysisTest, IsIndirectBranch) {
159 EXPECT_FALSE(Analysis->isIndirectBranch(beq()));
160 EXPECT_FALSE(Analysis->isIndirectBranch(cbeqz()));
161 EXPECT_FALSE(Analysis->isIndirectBranch(jal(RISCV::X0)));
162 EXPECT_FALSE(Analysis->isIndirectBranch(jal(RISCV::X1)));
163 EXPECT_TRUE(Analysis->isIndirectBranch(jalr(RISCV::X0)));
164 EXPECT_FALSE(Analysis->isIndirectBranch(jalr(RISCV::X1)));
165 EXPECT_FALSE(Analysis->isIndirectBranch(cj()));
166 EXPECT_TRUE(Analysis->isIndirectBranch(cjr(RISCV::X10)));
167 EXPECT_FALSE(Analysis->isIndirectBranch(cjr(RISCV::X1)));
168 EXPECT_FALSE(Analysis->isIndirectBranch(cjr(RISCV::X5)));
169 EXPECT_FALSE(Analysis->isIndirectBranch(cjal()));
170 EXPECT_FALSE(Analysis->isIndirectBranch(cjalr(RISCV::X5)));
173 INSTANTIATE_TEST_SUITE_P(RV32And64, InstrAnalysisTest,
174 testing::Values("riscv32", "riscv64"));