Skip to content

[BOLT] Add createCondBranch() and createLongUncondBranch() #85315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,13 @@ class MCPlusBuilder {
llvm_unreachable("not implemented");
}

/// Create a version of unconditional jump that has the largest span for a
/// single instruction with direct target.
virtual void createLongUncondBranch(MCInst &Inst, const MCSymbol *Target,
MCContext *Ctx) const {
llvm_unreachable("not implemented");
}

/// Creates a new call instruction in Inst and sets its operand to
/// Target.
virtual void createCall(MCInst &Inst, const MCSymbol *Target,
Expand Down Expand Up @@ -1675,6 +1682,12 @@ class MCPlusBuilder {
return Inst.getOpcode() == TargetOpcode::CFI_INSTRUCTION;
}

/// Create a conditional branch with a target-specific conditional code \p CC.
virtual void createCondBranch(MCInst &Inst, const MCSymbol *Target,
unsigned CC, MCContext *Ctx) const {
llvm_unreachable("not implemented");
}

/// Reverses the branch condition in Inst and update its taken target to TBB.
///
/// Returns true on success.
Expand Down
17 changes: 17 additions & 0 deletions bolt/lib/Target/X86/X86MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,14 @@ class X86MCPlusBuilder : public MCPlusBuilder {
MCSymbolRefExpr::create(TBB, MCSymbolRefExpr::VK_None, *Ctx)));
}

void createLongUncondBranch(MCInst &Inst, const MCSymbol *Target,
MCContext *Ctx) const override {
Inst.setOpcode(X86::JMP_4);
Inst.clear();
Inst.addOperand(MCOperand::createExpr(
MCSymbolRefExpr::create(Target, MCSymbolRefExpr::VK_None, *Ctx)));
}

void createCall(MCInst &Inst, const MCSymbol *Target,
MCContext *Ctx) override {
Inst.setOpcode(X86::CALL64pcrel32);
Expand All @@ -2759,6 +2767,15 @@ class X86MCPlusBuilder : public MCPlusBuilder {
Inst.setOpcode(X86::TRAP);
}

void createCondBranch(MCInst &Inst, const MCSymbol *Target, unsigned CC,
MCContext *Ctx) const override {
Inst.setOpcode(X86::JCC_1);
Inst.clear();
Inst.addOperand(MCOperand::createExpr(
MCSymbolRefExpr::create(Target, MCSymbolRefExpr::VK_None, *Ctx)));
Inst.addOperand(MCOperand::createImm(CC));
}

bool reverseBranchCondition(MCInst &Inst, const MCSymbol *TBB,
MCContext *Ctx) const override {
unsigned InvCC = getInvertedCondCode(getCondCode(Inst));
Expand Down