Skip to content

Commit 54d7f90

Browse files
committed
[BOLT][BTI] Add MCPlusBuilder::createBTI
- creates a bti j|c landing pad MCInst. - create getBTIHintNum utility in AArch64/Utils, to make sure BOLT generates BTI immediates the same way as LLVM. - add MCPlusBuilder unittests to cover new function.
1 parent dd14eb8 commit 54d7f90

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,11 @@ class MCPlusBuilder {
18651865
llvm_unreachable("not implemented");
18661866
}
18671867

1868+
/// Create a BTI landing pad instruction.
1869+
virtual void createBTI(MCInst &Inst, bool CouldCall, bool CouldJump) const {
1870+
llvm_unreachable("not implemented");
1871+
}
1872+
18681873
/// Store \p Target absolute address to \p RegName
18691874
virtual InstructionListType materializeAddress(const MCSymbol *Target,
18701875
MCContext *Ctx,

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,12 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27052705
return Insts;
27062706
}
27072707

2708+
void createBTI(MCInst &Inst, bool CouldCall, bool CouldJump) const override {
2709+
Inst.setOpcode(AArch64::HINT);
2710+
unsigned HintNum = getBTIHintNum(CouldCall, CouldJump);
2711+
Inst.addOperand(MCOperand::createImm(HintNum));
2712+
}
2713+
27082714
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
27092715
MCPhysReg RegName,
27102716
int64_t Addend = 0) const override {

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,36 @@ TEST_P(MCPlusBuilderTester, AArch64_CmpJE) {
143143
ASSERT_EQ(Label, BB->getLabel());
144144
}
145145

146+
TEST_P(MCPlusBuilderTester, AArch64_BTI) {
147+
if (GetParam() != Triple::aarch64)
148+
GTEST_SKIP();
149+
BinaryFunction *BF = BC->createInjectedBinaryFunction("BF", true);
150+
std::unique_ptr<BinaryBasicBlock> BB = BF->createBasicBlock();
151+
152+
MCInst BTIjc;
153+
BC->MIB->createBTI(BTIjc, true, true);
154+
BB->addInstruction(BTIjc);
155+
auto II = BB->begin();
156+
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
157+
ASSERT_EQ(II->getOperand(0).getImm(), 38);
158+
159+
MCInst BTIj;
160+
BC->MIB->createBTI(BTIj, false, true);
161+
II = BB->addInstruction(BTIj);
162+
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
163+
ASSERT_EQ(II->getOperand(0).getImm(), 36);
164+
165+
MCInst BTIc;
166+
BC->MIB->createBTI(BTIc, true, false);
167+
II = BB->addInstruction(BTIc);
168+
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
169+
ASSERT_EQ(II->getOperand(0).getImm(), 34);
170+
171+
MCInst BTIinvalid;
172+
ASSERT_DEATH(BC->MIB->createBTI(BTIinvalid, false, false),
173+
"No target kinds!");
174+
}
175+
146176
TEST_P(MCPlusBuilderTester, AArch64_CmpJNE) {
147177
if (GetParam() != Triple::aarch64)
148178
GTEST_SKIP();

llvm/lib/Target/AArch64/AArch64BranchTargets.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "AArch64MachineFunctionInfo.h"
2020
#include "AArch64Subtarget.h"
21+
#include "Utils/AArch64BaseInfo.h"
2122
#include "llvm/CodeGen/MachineFunctionPass.h"
2223
#include "llvm/CodeGen/MachineInstrBuilder.h"
2324
#include "llvm/CodeGen/MachineJumpTableInfo.h"
@@ -135,13 +136,7 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
135136
<< (CouldCall ? "c" : "") << " to " << MBB.getName()
136137
<< "\n");
137138

138-
unsigned HintNum = 32;
139-
if (CouldCall)
140-
HintNum |= 2;
141-
if (CouldJump)
142-
HintNum |= 4;
143-
assert(HintNum != 32 && "No target kinds!");
144-
139+
unsigned HintNum = getBTIHintNum(CouldCall, CouldJump);
145140
auto MBBI = MBB.begin();
146141

147142
// If the block starts with EH_LABEL(s), skip them first.

llvm/lib/Target/AArch64/Utils/AArch64BaseInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,16 @@ AArch64StringToPACKeyID(StringRef Name) {
987987
return std::nullopt;
988988
}
989989

990+
inline static unsigned getBTIHintNum(bool CouldCall, bool CouldJump) {
991+
unsigned HintNum = 32;
992+
if (CouldCall)
993+
HintNum |= 2;
994+
if (CouldJump)
995+
HintNum |= 4;
996+
assert(HintNum != 32 && "No target kinds!");
997+
return HintNum;
998+
}
999+
9901000
namespace AArch64 {
9911001
// The number of bits in a SVE register is architecturally defined
9921002
// to be a multiple of this value. If <M x t> has this number of bits,

0 commit comments

Comments
 (0)