Skip to content

Commit c668bf8

Browse files
committed
[BOLT][BTI] Add MCPlusBuilder::updateBTIVariant
Checks if an instruction is BTI, and updates the immediate value to the newly requested variant.
1 parent ee348f3 commit c668bf8

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,12 @@ class MCPlusBuilder {
18821882
llvm_unreachable("not implemented");
18831883
}
18841884

1885+
/// Update operand of BTI instruction.
1886+
virtual void updateBTIVariant(MCInst &Inst, bool CallTarget,
1887+
bool JumpTarget) const {
1888+
llvm_unreachable("not implemented");
1889+
}
1890+
18851891
/// Store \p Target absolute address to \p RegName
18861892
virtual InstructionListType materializeAddress(const MCSymbol *Target,
18871893
MCContext *Ctx,

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,14 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
27742774
}
27752775

27762776

2777+
void updateBTIVariant(MCInst &Inst, bool CallTarget,
2778+
bool JumpTarget) const override {
2779+
assert(Inst.getOpcode() == AArch64::HINT && "Not a BTI instruction.");
2780+
unsigned HintNum = getBTIHintNum(CallTarget, JumpTarget);
2781+
Inst.clear();
2782+
Inst.addOperand(MCOperand::createImm(HintNum));
2783+
}
2784+
27772785
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
27782786
MCPhysReg RegName,
27792787
int64_t Addend = 0) const override {

bolt/unittests/Core/MCPlusBuilder.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,26 @@ TEST_P(MCPlusBuilderTester, AArch64_BTI) {
156156
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
157157
ASSERT_EQ(II->getOperand(0).getImm(), 38);
158158
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, true));
159+
BC->MIB->updateBTIVariant(*II, true, false);
160+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, false));
159161

160162
MCInst BTIj;
161163
BC->MIB->createBTI(BTIj, false, true);
162164
II = BB->addInstruction(BTIj);
163165
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
164166
ASSERT_EQ(II->getOperand(0).getImm(), 36);
165167
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, false, true));
168+
BC->MIB->updateBTIVariant(*II, true, true);
169+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, true));
166170

167171
MCInst BTIc;
168172
BC->MIB->createBTI(BTIc, true, false);
169173
II = BB->addInstruction(BTIc);
170174
ASSERT_EQ(II->getOpcode(), AArch64::HINT);
171175
ASSERT_EQ(II->getOperand(0).getImm(), 34);
172176
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, true, false));
177+
BC->MIB->updateBTIVariant(*II, false, true);
178+
ASSERT_TRUE(BC->MIB->isBTILandingPad(*II, false, true));
173179

174180
MCInst BTIinvalid;
175181
ASSERT_DEATH(BC->MIB->createBTI(BTIinvalid, false, false),

0 commit comments

Comments
 (0)