Skip to content

Commit 6c5b62b

Browse files
authored
[BOLT][NFC] Separate isReversibleBranch's 2 semantics (#95572)
`isUnsupportedBranch` was renamed (and inverted) to `isReversibleBranch`, as that was how it was being used. But one use in `BinaryFunction::disassemble` was using the original meaning to detect unsupported branches, and the `isUnsupportedBranch` had 2 separate semantic checks. Move the unsupported branch check from `isReversibleBranch` to a new entry point: `isUnsupportedInstruction`. Call that from `BinaryFunction::disassemble`. Move the dynamic branch check from X86's isReversibleBranch to the base class, as it is not an architecture-specific check. Remove unnecessary `isReversibleBranch` calls from Instrumentation and X86 MCPlusBuilder.
1 parent 69c99ad commit 6c5b62b

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,20 @@ class MCPlusBuilder {
439439
}
440440

441441
/// Check whether this conditional branch can be reversed
442-
virtual bool isReversibleBranch(const MCInst &Inst) const { return true; }
442+
virtual bool isReversibleBranch(const MCInst &Inst) const {
443+
assert(!isUnsupportedInstruction(Inst) && isConditionalBranch(Inst) &&
444+
"Instruction is not known conditional branch");
445+
446+
if (isDynamicBranch(Inst))
447+
return false;
448+
return true;
449+
}
450+
451+
/// Return true if this instruction inhibits analysis of the containing
452+
/// function.
453+
virtual bool isUnsupportedInstruction(const MCInst &Inst) const {
454+
return false;
455+
}
443456

444457
/// Return true of the instruction is of pseudo kind.
445458
virtual bool isPseudo(const MCInst &Inst) const {

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,10 @@ Error BinaryFunction::disassemble() {
12761276
}
12771277
}
12781278

1279+
bool IsUnsupported = BC.MIB->isUnsupportedInstruction(Instruction);
1280+
if (IsUnsupported)
1281+
setIgnored();
1282+
12791283
if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
12801284
uint64_t TargetAddress = 0;
12811285
if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
@@ -1289,12 +1293,10 @@ Error BinaryFunction::disassemble() {
12891293
const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
12901294
MCSymbol *TargetSymbol = nullptr;
12911295

1292-
if (!BC.MIB->isReversibleBranch(Instruction)) {
1293-
setIgnored();
1294-
if (BinaryFunction *TargetFunc =
1296+
if (IsUnsupported)
1297+
if (auto *TargetFunc =
12951298
BC.getBinaryFunctionContainingAddress(TargetAddress))
12961299
TargetFunc->setIgnored();
1297-
}
12981300

12991301
if (IsCall && containsAddress(TargetAddress)) {
13001302
if (TargetAddress == getAddress()) {

bolt/lib/Passes/Instrumentation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
479479
HasJumpTable = true;
480480
else if (BC.MIB->isUnconditionalBranch(Inst))
481481
HasUnconditionalBranch = true;
482-
else if ((!BC.MIB->isCall(Inst) && !BC.MIB->isConditionalBranch(Inst)) ||
483-
!BC.MIB->isReversibleBranch(Inst))
482+
else if (!(BC.MIB->isCall(Inst) || BC.MIB->isConditionalBranch(Inst)))
484483
continue;
485484

486485
const uint32_t FromOffset = *BC.MIB->getOffset(Inst);

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,19 @@ class X86MCPlusBuilder : public MCPlusBuilder {
328328
return false;
329329
}
330330

331-
bool isReversibleBranch(const MCInst &Inst) const override {
332-
if (isDynamicBranch(Inst))
333-
return false;
334-
331+
bool isUnsupportedInstruction(const MCInst &Inst) const override {
335332
switch (Inst.getOpcode()) {
336333
default:
337-
return true;
334+
return false;
335+
338336
case X86::LOOP:
339337
case X86::LOOPE:
340338
case X86::LOOPNE:
341339
case X86::JECXZ:
342340
case X86::JRCXZ:
343-
return false;
341+
// These have a short displacement, and therefore (often) break after
342+
// basic block relayout.
343+
return true;
344344
}
345345
}
346346

@@ -1879,11 +1879,9 @@ class X86MCPlusBuilder : public MCPlusBuilder {
18791879
continue;
18801880
}
18811881

1882-
// Handle conditional branches and ignore indirect branches
1883-
if (isReversibleBranch(*I) && getCondCode(*I) == X86::COND_INVALID) {
1884-
// Indirect branch
1882+
// Ignore indirect branches
1883+
if (getCondCode(*I) == X86::COND_INVALID)
18851884
return false;
1886-
}
18871885

18881886
if (CondBranch == nullptr) {
18891887
const MCSymbol *TargetBB = getTargetSymbol(*I);

0 commit comments

Comments
 (0)