Skip to content

Commit cca66a2

Browse files
authored
[BOLT][BTI] Add MCPlusBuilder::updateBTIVariant (llvm#167308)
Checks if an instruction is BTI, and updates the immediate value to the newly requested variant. This can be used in situations when the compiler already inserted a BTI landing pad to a location, but BOLT needs to update it to a different variant. Example: br x0 to a location with a BTI c.
1 parent e99029e commit cca66a2

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
@@ -1888,6 +1888,12 @@ class MCPlusBuilder {
18881888
llvm_unreachable("not implemented");
18891889
}
18901890

1891+
/// Update operand of BTI instruction.
1892+
virtual void updateBTIVariant(MCInst &Inst, bool CallTarget,
1893+
bool JumpTarget) const {
1894+
llvm_unreachable("not implemented");
1895+
}
1896+
18911897
/// Store \p Target absolute address to \p RegName
18921898
virtual InstructionListType materializeAddress(const MCSymbol *Target,
18931899
MCContext *Ctx,

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,14 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
28002800
Inst.getOpcode() == AArch64::PACIBSP;
28012801
}
28022802

2803+
void updateBTIVariant(MCInst &Inst, bool CallTarget,
2804+
bool JumpTarget) const override {
2805+
assert(Inst.getOpcode() == AArch64::HINT && "Not a BTI instruction.");
2806+
unsigned HintNum = getBTIHintNum(CallTarget, JumpTarget);
2807+
Inst.clear();
2808+
Inst.addOperand(MCOperand::createImm(HintNum));
2809+
}
2810+
28032811
InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx,
28042812
MCPhysReg RegName,
28052813
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
#ifndef NDEBUG
175181
MCInst BTIinvalid;

0 commit comments

Comments
 (0)