Skip to content

Commit af2af77

Browse files
bgergely0aadeshps-mcw
authored andcommitted
[BOLT][BTI] Add MCPlusBuilder::createBTI (llvm#167305)
- 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 7c8f989 commit af2af77

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,11 @@ class MCPlusBuilder {
18691869
llvm_unreachable("not implemented");
18701870
}
18711871

1872+
/// Create a BTI landing pad instruction.
1873+
virtual void createBTI(MCInst &Inst, bool CallTarget, bool JumpTarget) const {
1874+
llvm_unreachable("not implemented");
1875+
}
1876+
18721877
/// Store \p Target absolute address to \p RegName
18731878
virtual InstructionListType materializeAddress(const MCSymbol *Target,
18741879
MCContext *Ctx,

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,13 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27752775
return Insts;
27762776
}
27772777

2778+
void createBTI(MCInst &Inst, bool CallTarget,
2779+
bool JumpTarget) const override {
2780+
Inst.setOpcode(AArch64::HINT);
2781+
unsigned HintNum = getBTIHintNum(CallTarget, JumpTarget);
2782+
Inst.addOperand(MCOperand::createImm(HintNum));
2783+
}
2784+
27782785
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
27792786
MCPhysReg RegName,
27802787
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
@@ -1003,6 +1003,16 @@ AArch64StringToPACKeyID(StringRef Name) {
10031003
return std::nullopt;
10041004
}
10051005

1006+
inline static unsigned getBTIHintNum(bool CallTarget, bool JumpTarget) {
1007+
unsigned HintNum = 32;
1008+
if (CallTarget)
1009+
HintNum |= 2;
1010+
if (JumpTarget)
1011+
HintNum |= 4;
1012+
assert(HintNum != 32 && "No target kinds!");
1013+
return HintNum;
1014+
}
1015+
10061016
namespace AArch64 {
10071017
// The number of bits in a SVE register is architecturally defined
10081018
// to be a multiple of this value. If <M x t> has this number of bits,

0 commit comments

Comments
 (0)