Skip to content

[BOLT] [PowerPC] Port #140894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bolt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ endif() # standalone

# Determine default set of targets to build -- the intersection of
# those BOLT supports and those LLVM is targeting.
set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV;PowerPC")
set(BOLT_TARGETS_TO_BUILD_default)
foreach (tgt ${BOLT_TARGETS_TO_BUILD_all})
if (tgt IN_LIST LLVM_TARGETS_TO_BUILD)
Expand Down
5 changes: 5 additions & 0 deletions bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,11 @@ MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *,
const MCRegisterInfo *,
const MCSubtargetInfo *);

MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *,
const MCInstrInfo *,
const MCRegisterInfo *,
const MCSubtargetInfo *);

} // namespace bolt
} // namespace llvm

Expand Down
17 changes: 17 additions & 0 deletions bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "bolt/Core/MCPlusBuilder.h"

namespace llvm {
namespace bolt {

class PPCMCPlusBuilder : public MCPlusBuilder {
public:
using MCPlusBuilder::MCPlusBuilder;

static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1,
MCPhysReg Reg2);
};

} // namespace bolt
} // namespace llvm
5 changes: 5 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
return createRISCVMCPlusBuilder(Analysis, Info, RegInfo, STI);
#endif

#ifdef POWERPC_AVAILABLE
if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
return createPowerPCMCPlusBuilder(Analysis, Info, RegInfo, STI);
#endif

llvm_unreachable("architecture unsupported by MCPlusBuilder");
}

Expand Down
4 changes: 3 additions & 1 deletion bolt/lib/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
foreach (tgt ${BOLT_TARGETS_TO_BUILD})
add_subdirectory(${tgt})
endforeach()
string(TOUPPER ${tgt} TGT_UPPER)
add_definitions(-D${TGT_UPPER}_AVAILABLE)
endforeach()
29 changes: 29 additions & 0 deletions bolt/lib/Target/PowerPC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
add_llvm_library(LLVMBOLTTargetPowerPC
PPCMCPlusBuilder.cpp
)

target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
${LLVM_BINARY_DIR}/include
${LLVM_SOURCE_DIR}/include
)

file(MAKE_DIRECTORY "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC")

foreach(incfile IN ITEMS
PPCGenInstrInfo.inc
PPCGenRegisterInfo.inc
)
add_custom_command(
OUTPUT "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
"${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
DEPENDS "${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
COMMENT "Copying ${incfile} to include directory"
)
add_custom_target(
"BoltCopy${incfile}" ALL
DEPENDS "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
)
add_dependencies(LLVMBOLTTargetPowerPC "BoltCopy${incfile}")
endforeach()
49 changes: 49 additions & 0 deletions bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===- bolt/Target/PowerPC/PPCMCPlusBuilder.cpp -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides PowerPC-specific MCPlus builder.
//
//===----------------------------------------------------------------------===//

#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
#include "bolt/Core/MCPlusBuilder.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCRegisterInfo.h"
#define GET_INSTRINFO_ENUM
#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
#define GET_REGINFO_ENUM
#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"

namespace llvm {
namespace bolt {

// Create instructions to push two registers onto the stack
void PPCMCPlusBuilder::createPushRegisters(MCInst &Inst1, MCInst &Inst2,
MCPhysReg Reg1, MCPhysReg /*Reg2*/) {
Inst1.clear();
Inst1.setOpcode(PPC::STDU);
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
Inst1.addOperand(MCOperand::createImm(-16)); // offset

Inst2.clear();
Inst2.setOpcode(PPC::STD);
Inst2.addOperand(MCOperand::createReg(Reg1)); // source register
Inst2.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
Inst2.addOperand(MCOperand::createImm(0)); // offset
}

MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
const MCInstrInfo *Info,
const MCRegisterInfo *RegInfo,
const MCSubtargetInfo *STI) {
return new PPCMCPlusBuilder(Analysis, Info, RegInfo, STI);
}

} // namespace bolt
} // namespace llvm
1 change: 1 addition & 0 deletions bolt/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ endfunction()

add_subdirectory(Core)
add_subdirectory(Profile)
add_subdirectory(Target)
1 change: 1 addition & 0 deletions bolt/unittests/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(PowerPC)
20 changes: 20 additions & 0 deletions bolt/unittests/Target/PowerPC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
set(BOLTTargetPowerPCTestsSources
PPCMCPlusBuilderTest.cpp)

add_bolt_unittest(BOLTTargetPowerPCTests
${BOLTTargetPowerPCTestsSources}
)

target_link_libraries(BOLTTargetPowerPCTests PRIVATE
LLVMBOLTTargetPowerPC
LLVMBOLTCore
LLVMCore
)

target_include_directories(BOLTTargetPowerPCTests PRIVATE
${LLVM_BINARY_DIR}/include
${LLVM_SOURCE_DIR}/include
${LLVM_SOURCE_DIR}/bolt/include
${LLVM_BINARY_DIR}/tools/bolt/include
${CMAKE_SOURCE_DIR}
)
53 changes: 53 additions & 0 deletions bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===- bolt/unittest/Target/PowerPC/PPCMCPlusBuilderTest.cpp
//-------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
#include "bolt/Core/MCPlusBuilder.h"
#include "llvm/MC/MCInst.h"
#include "gtest/gtest.h"
#define GET_INSTRINFO_ENUM
#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
#define GET_REGINFO_ENUM
#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"

using namespace llvm;
using namespace bolt;

namespace {

TEST(PPCMCPlusBuilderTest, CreatePushRegisters) {
// Set up dummy input registers
MCInst Inst1, Inst2;
MCPhysReg Reg1 = PPC::R3; // Arbitary register

// Call the method under test
PPCMCPlusBuilder::createPushRegisters(Inst1, Inst2, Reg1, /*Reg2=*/PPC::R4);

// Check Inst1 is STDU R1, R1, -16
EXPECT_EQ(Inst1.getOpcode(), PPC::STDU);
ASSERT_EQ(Inst1.getNumOperands(), 3u);
EXPECT_TRUE(Inst1.getOperand(0).isReg());
EXPECT_EQ(Inst1.getOperand(0).getReg(), PPC::R1);
EXPECT_TRUE(Inst1.getOperand(1).isReg());
EXPECT_EQ(Inst1.getOperand(1).getReg(), PPC::R1);
EXPECT_TRUE(Inst1.getOperand(2).isImm());
EXPECT_EQ(Inst1.getOperand(2).getImm(), -16);

// Check Inst2 is STD Reg1, R1, 0
EXPECT_EQ(Inst2.getOpcode(), PPC::STD);
ASSERT_EQ(Inst2.getNumOperands(), 3u);
EXPECT_TRUE(Inst2.getOperand(0).isReg());
EXPECT_EQ(Inst2.getOperand(0).getReg(), Reg1);
EXPECT_TRUE(Inst2.getOperand(1).isReg());
EXPECT_EQ(Inst2.getOperand(1).getReg(), PPC::R1);
EXPECT_TRUE(Inst2.getOperand(2).isImm());
EXPECT_EQ(Inst2.getOperand(2).getImm(), 0);
}

} // end anonymous namespace
Loading