Skip to content

Commit 04e648b

Browse files
committed
[BOLT][PPC] Add unit test.
1 parent efd806e commit 04e648b

File tree

6 files changed

+108
-21
lines changed

6 files changed

+108
-21
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "bolt/Core/MCPlusBuilder.h"
4+
5+
namespace llvm {
6+
namespace bolt {
7+
8+
class PPCMCPlusBuilder : public MCPlusBuilder {
9+
public:
10+
using MCPlusBuilder::MCPlusBuilder;
11+
12+
static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1,
13+
MCPhysReg Reg2);
14+
};
15+
16+
} // namespace bolt
17+
} // namespace llvm

bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
1314
#include "bolt/Core/MCPlusBuilder.h"
1415
#include "llvm/MC/MCInst.h"
1516
#include "llvm/MC/MCRegisterInfo.h"
@@ -21,27 +22,21 @@
2122
namespace llvm {
2223
namespace bolt {
2324

24-
class PPCMCPlusBuilder : public MCPlusBuilder {
25-
public:
26-
using MCPlusBuilder::MCPlusBuilder;
27-
28-
// Create instructions to push two registers onto the stack
29-
static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1,
30-
MCPhysReg /*Reg2*/) {
31-
32-
Inst1.clear();
33-
Inst1.setOpcode(PPC::STDU);
34-
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
35-
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
36-
Inst1.addOperand(MCOperand::createImm(-16)); // offset
37-
38-
Inst2.clear();
39-
Inst2.setOpcode(PPC::STD);
40-
Inst2.addOperand(MCOperand::createReg(Reg1)); // source register
41-
Inst2.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
42-
Inst2.addOperand(MCOperand::createImm(0)); // offset
43-
}
44-
};
25+
// Create instructions to push two registers onto the stack
26+
void PPCMCPlusBuilder::createPushRegisters(MCInst &Inst1, MCInst &Inst2,
27+
MCPhysReg Reg1, MCPhysReg /*Reg2*/) {
28+
Inst1.clear();
29+
Inst1.setOpcode(PPC::STDU);
30+
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
31+
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
32+
Inst1.addOperand(MCOperand::createImm(-16)); // offset
33+
34+
Inst2.clear();
35+
Inst2.setOpcode(PPC::STD);
36+
Inst2.addOperand(MCOperand::createReg(Reg1)); // source register
37+
Inst2.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
38+
Inst2.addOperand(MCOperand::createImm(0)); // offset
39+
}
4540

4641
MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
4742
const MCInstrInfo *Info,

bolt/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ endfunction()
77

88
add_subdirectory(Core)
99
add_subdirectory(Profile)
10+
add_subdirectory(Target)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(PowerPC)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set(BOLTTargetPowerPCTestsSources
2+
PPCMCPlusBuilderTest.cpp)
3+
4+
add_bolt_unittest(BOLTTargetPowerPCTests
5+
${BOLTTargetPowerPCTestsSources}
6+
)
7+
8+
target_link_libraries(BOLTTargetPowerPCTests PRIVATE
9+
LLVMBOLTTargetPowerPC
10+
LLVMBOLTCore
11+
LLVMCore
12+
)
13+
14+
target_include_directories(BOLTTargetPowerPCTests PRIVATE
15+
${LLVM_BINARY_DIR}/include
16+
${LLVM_SOURCE_DIR}/include
17+
${LLVM_SOURCE_DIR}/bolt/include
18+
${LLVM_BINARY_DIR}/tools/bolt/include
19+
${CMAKE_SOURCE_DIR}
20+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===- bolt/unittest/Target/PowerPC/PPCMCPlusBuilderTest.cpp
2+
//-------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
11+
#include "bolt/Core/MCPlusBuilder.h"
12+
#include "llvm/MC/MCInst.h"
13+
#include "gtest/gtest.h"
14+
#define GET_INSTRINFO_ENUM
15+
#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
16+
#define GET_REGINFO_ENUM
17+
#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"
18+
19+
using namespace llvm;
20+
using namespace bolt;
21+
22+
namespace {
23+
24+
TEST(PPCMCPlusBuilderTest, CreatePushRegisters) {
25+
// Set up dummy input registers
26+
MCInst Inst1, Inst2;
27+
MCPhysReg Reg1 = PPC::R3; // Arbitary register
28+
29+
// Call the method under test
30+
PPCMCPlusBuilder::createPushRegisters(Inst1, Inst2, Reg1, /*Reg2=*/PPC::R4);
31+
32+
// Check Inst1 is STDU R1, R1, -16
33+
EXPECT_EQ(Inst1.getOpcode(), PPC::STDU);
34+
ASSERT_EQ(Inst1.getNumOperands(), 3u);
35+
EXPECT_TRUE(Inst1.getOperand(0).isReg());
36+
EXPECT_EQ(Inst1.getOperand(0).getReg(), PPC::R1);
37+
EXPECT_TRUE(Inst1.getOperand(1).isReg());
38+
EXPECT_EQ(Inst1.getOperand(1).getReg(), PPC::R1);
39+
EXPECT_TRUE(Inst1.getOperand(2).isImm());
40+
EXPECT_EQ(Inst1.getOperand(2).getImm(), -16);
41+
42+
// Check Inst2 is STD Reg1, R1, 0
43+
EXPECT_EQ(Inst2.getOpcode(), PPC::STD);
44+
ASSERT_EQ(Inst2.getNumOperands(), 3u);
45+
EXPECT_TRUE(Inst2.getOperand(0).isReg());
46+
EXPECT_EQ(Inst2.getOperand(0).getReg(), Reg1);
47+
EXPECT_TRUE(Inst2.getOperand(1).isReg());
48+
EXPECT_EQ(Inst2.getOperand(1).getReg(), PPC::R1);
49+
EXPECT_TRUE(Inst2.getOperand(2).isImm());
50+
EXPECT_EQ(Inst2.getOperand(2).getImm(), 0);
51+
}
52+
53+
} // end anonymous namespace

0 commit comments

Comments
 (0)