Skip to content

Commit 4533699

Browse files
authored
[BOLT][BTI] Add MCPlusBuilder::isBTILandingPad (#167306)
- takes both implicit and explicit BTIs into account - fix related comment in llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
1 parent 1441f04 commit 4533699

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

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

1872+
/// Check if an Instruction is a BTI landing pad with the required properties.
1873+
/// Takes both explicit and implicit BTIs into account.
1874+
virtual bool isBTILandingPad(MCInst &Inst, bool CallTarget,
1875+
bool JumpTarget) const {
1876+
llvm_unreachable("not implemented");
1877+
return false;
1878+
}
1879+
1880+
/// Check if an Instruction is an implicit BTI c landing pad.
1881+
virtual bool isImplicitBTIC(MCInst &Inst) const {
1882+
llvm_unreachable("not implemented");
1883+
return false;
1884+
}
1885+
18721886
/// Create a BTI landing pad instruction.
18731887
virtual void createBTI(MCInst &Inst, bool CallTarget, bool JumpTarget) const {
18741888
llvm_unreachable("not implemented");

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,24 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27822782
Inst.addOperand(MCOperand::createImm(HintNum));
27832783
}
27842784

2785+
bool isBTILandingPad(MCInst &Inst, bool CallTarget,
2786+
bool JumpTarget) const override {
2787+
unsigned HintNum = getBTIHintNum(CallTarget, JumpTarget);
2788+
bool IsExplicitBTI =
2789+
Inst.getOpcode() == AArch64::HINT && Inst.getNumOperands() == 1 &&
2790+
Inst.getOperand(0).isImm() && Inst.getOperand(0).getImm() == HintNum;
2791+
2792+
bool IsImplicitBTI = HintNum == 34 && isImplicitBTIC(Inst);
2793+
return IsExplicitBTI || IsImplicitBTI;
2794+
}
2795+
2796+
bool isImplicitBTIC(MCInst &Inst) const override {
2797+
// PACI[AB]SP are always implicitly BTI C, independently of
2798+
// SCTLR_EL1.BT[01].
2799+
return Inst.getOpcode() == AArch64::PACIASP ||
2800+
Inst.getOpcode() == AArch64::PACIBSP;
2801+
}
2802+
27852803
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
27862804
MCPhysReg RegName,
27872805
int64_t Addend = 0) const override {

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,39 @@ TEST_P(MCPlusBuilderTester, AArch64_BTI) {
155155
auto II = BB->begin();
156156
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
157157
ASSERT_EQ(II->getOperand(0).getImm(), 38);
158+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, true));
158159

159160
MCInst BTIj;
160161
BC->MIB->createBTI(BTIj, false, true);
161162
II = BB->addInstruction(BTIj);
162163
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
163164
ASSERT_EQ(II->getOperand(0).getImm(), 36);
165+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, false, true));
164166

165167
MCInst BTIc;
166168
BC->MIB->createBTI(BTIc, true, false);
167169
II = BB->addInstruction(BTIc);
168170
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
169171
ASSERT_EQ(II->getOperand(0).getImm(), 34);
172+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, false));
170173

171174
MCInst BTIinvalid;
172175
ASSERT_DEATH(BC->MIB->createBTI(BTIinvalid, false, false),
173176
"No target kinds!");
177+
178+
MCInst Paciasp = MCInstBuilder(AArch64::PACIASP);
179+
II = BB->addInstruction(Paciasp);
180+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, false));
181+
ASSERT_FALSE(BC->MIB->isBTILandingPad(*II, true, true));
182+
ASSERT_FALSE(BC->MIB->isBTILandingPad(*II, false, true));
183+
ASSERT_TRUE(BC->MIB->isImplicitBTIC(*II));
184+
185+
MCInst Pacibsp = MCInstBuilder(AArch64::PACIBSP);
186+
II = BB->addInstruction(Pacibsp);
187+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, false));
188+
ASSERT_FALSE(BC->MIB->isBTILandingPad(*II, true, true));
189+
ASSERT_FALSE(BC->MIB->isBTILandingPad(*II, false, true));
190+
ASSERT_TRUE(BC->MIB->isImplicitBTIC(*II));
174191
}
175192

176193
TEST_P(MCPlusBuilderTester, AArch64_CmpJNE) {

llvm/lib/Target/AArch64/AArch64BranchTargets.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, bool CouldCall,
150150
++MBBI)
151151
;
152152

153-
// SCTLR_EL1.BT[01] is set to 0 by default which means
154-
// PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
153+
// PACI[AB]SP are implicitly BTI c so insertion of a BTI can be skipped in
154+
// this case. Depending on the runtime value of SCTLR_EL1.BT[01], they are not
155+
// equivalent to a BTI jc, which still requires an additional BTI.
155156
if (MBBI != MBB.end() && ((HintNum & BTIMask) == BTIC) &&
156157
(MBBI->getOpcode() == AArch64::PACIASP ||
157158
MBBI->getOpcode() == AArch64::PACIBSP))

0 commit comments

Comments
 (0)