Skip to content

Commit f26be06

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 a51c1f8 commit f26be06

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
@@ -1863,6 +1863,11 @@ class MCPlusBuilder {
18631863
llvm_unreachable("not implemented");
18641864
}
18651865

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

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,12 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27482748
return Insts;
27492749
}
27502750

2751+
void createBTI(MCInst &Inst, bool CouldCall, bool CouldJump) const override {
2752+
Inst.setOpcode(AArch64::HINT);
2753+
unsigned HintNum = getBTIHintNum(CouldCall, CouldJump);
2754+
Inst.addOperand(MCOperand::createImm(HintNum));
2755+
}
2756+
27512757
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
27522758
MCPhysReg RegName,
27532759
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)