Skip to content

Commit

Permalink
[BOLT][NFC] Separate isReversibleBranch's 2 semantics (llvm#95572)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
urnathan authored Jun 28, 2024
1 parent 69c99ad commit 6c5b62b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
15 changes: 14 additions & 1 deletion bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,20 @@ class MCPlusBuilder {
}

/// Check whether this conditional branch can be reversed
virtual bool isReversibleBranch(const MCInst &Inst) const { return true; }
virtual bool isReversibleBranch(const MCInst &Inst) const {
assert(!isUnsupportedInstruction(Inst) && isConditionalBranch(Inst) &&
"Instruction is not known conditional branch");

if (isDynamicBranch(Inst))
return false;
return true;
}

/// Return true if this instruction inhibits analysis of the containing
/// function.
virtual bool isUnsupportedInstruction(const MCInst &Inst) const {
return false;
}

/// Return true of the instruction is of pseudo kind.
virtual bool isPseudo(const MCInst &Inst) const {
Expand Down
10 changes: 6 additions & 4 deletions bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,10 @@ Error BinaryFunction::disassemble() {
}
}

bool IsUnsupported = BC.MIB->isUnsupportedInstruction(Instruction);
if (IsUnsupported)
setIgnored();

if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
uint64_t TargetAddress = 0;
if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
Expand All @@ -1289,12 +1293,10 @@ Error BinaryFunction::disassemble() {
const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
MCSymbol *TargetSymbol = nullptr;

if (!BC.MIB->isReversibleBranch(Instruction)) {
setIgnored();
if (BinaryFunction *TargetFunc =
if (IsUnsupported)
if (auto *TargetFunc =
BC.getBinaryFunctionContainingAddress(TargetAddress))
TargetFunc->setIgnored();
}

if (IsCall && containsAddress(TargetAddress)) {
if (TargetAddress == getAddress()) {
Expand Down
3 changes: 1 addition & 2 deletions bolt/lib/Passes/Instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
HasJumpTable = true;
else if (BC.MIB->isUnconditionalBranch(Inst))
HasUnconditionalBranch = true;
else if ((!BC.MIB->isCall(Inst) && !BC.MIB->isConditionalBranch(Inst)) ||
!BC.MIB->isReversibleBranch(Inst))
else if (!(BC.MIB->isCall(Inst) || BC.MIB->isConditionalBranch(Inst)))
continue;

const uint32_t FromOffset = *BC.MIB->getOffset(Inst);
Expand Down
18 changes: 8 additions & 10 deletions bolt/lib/Target/X86/X86MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ class X86MCPlusBuilder : public MCPlusBuilder {
return false;
}

bool isReversibleBranch(const MCInst &Inst) const override {
if (isDynamicBranch(Inst))
return false;

bool isUnsupportedInstruction(const MCInst &Inst) const override {
switch (Inst.getOpcode()) {
default:
return true;
return false;

case X86::LOOP:
case X86::LOOPE:
case X86::LOOPNE:
case X86::JECXZ:
case X86::JRCXZ:
return false;
// These have a short displacement, and therefore (often) break after
// basic block relayout.
return true;
}
}

Expand Down Expand Up @@ -1879,11 +1879,9 @@ class X86MCPlusBuilder : public MCPlusBuilder {
continue;
}

// Handle conditional branches and ignore indirect branches
if (isReversibleBranch(*I) && getCondCode(*I) == X86::COND_INVALID) {
// Indirect branch
// Ignore indirect branches
if (getCondCode(*I) == X86::COND_INVALID)
return false;
}

if (CondBranch == nullptr) {
const MCSymbol *TargetBB = getTargetSymbol(*I);
Expand Down

0 comments on commit 6c5b62b

Please sign in to comment.