[BOLT][AArch64] Add a unittest for compare-and-branch inversion. - #181177
Merged
Merged
Conversation
Checks that isReversibleBranch() returns false - when the immediate value is 63 and needs +1 adjustment - when the immediate value is 0 and needs -1 adjustment Checks that reverseBranchCondition() adjusts - the opcode - the immediate operand if necessary (+/-1) - the register operands if necessary (swap)
labrinea
requested review from
aaupov,
ayermolo,
maksfb,
paschalis-mpeis,
rafaelauler,
yavtuk,
yota9 and
yozhu
as code owners
February 12, 2026 16:37
labrinea
temporarily deployed
to
main-branch-only
February 12, 2026 16:37 — with
GitHub Actions
Inactive
llvmbot
temporarily deployed
to
main-branch-only
February 12, 2026 16:37 — with
GitHub Actions
Inactive
Member
|
@llvm/pr-subscribers-bolt Author: Alexandros Lamprineas (labrinea) ChangesChecks that isReversibleBranch() returns false
Checks that reverseBranchCondition() adjusts
Full diff: https://github.com/llvm/llvm-project/pull/181177.diff 1 Files Affected:
diff --git a/bolt/unittests/Core/MCPlusBuilder.cpp b/bolt/unittests/Core/MCPlusBuilder.cpp
index a8d25f3323b38..e11347a8c4c94 100644
--- a/bolt/unittests/Core/MCPlusBuilder.cpp
+++ b/bolt/unittests/Core/MCPlusBuilder.cpp
@@ -119,6 +119,63 @@ TEST_P(MCPlusBuilderTester, AliasSmallerX0) {
/*OnlySmaller=*/true);
}
+TEST_P(MCPlusBuilderTester, AArch64_ReverseCompAndBranch) {
+ if (GetParam() != Triple::aarch64)
+ GTEST_SKIP();
+
+ BinaryFunction *BF = BC->createInjectedBinaryFunction("BF", true);
+ std::unique_ptr<BinaryBasicBlock> BB = BF->createBasicBlock();
+ std::unique_ptr<BinaryBasicBlock> TargetBB = BF->createBasicBlock();
+ BB->addSuccessor(TargetBB.get());
+
+ // cbgt x0, #0, target
+ MCInst NeedsImmInc = MCInstBuilder(AArch64::CBGTXri)
+ .addReg(AArch64::X0)
+ .addImm(0)
+ .addExpr(MCSymbolRefExpr::create(TargetBB->getLabel(), *BC->Ctx.get()));
+ BB->addInstruction(NeedsImmInc);
+ // cblo x0, #1, target
+ MCInst NeedsImmDec = MCInstBuilder(AArch64::CBLOXri)
+ .addReg(AArch64::X0)
+ .addImm(1)
+ .addExpr(MCSymbolRefExpr::create(TargetBB->getLabel(), *BC->Ctx.get()));
+ BB->addInstruction(NeedsImmDec);
+ // cbge x0, x1, target
+ MCInst NeedsRegSwap = MCInstBuilder(AArch64::CBGEXrr)
+ .addReg(AArch64::X0)
+ .addReg(AArch64::X1)
+ .addExpr(MCSymbolRefExpr::create(TargetBB->getLabel(), *BC->Ctx.get()));
+ BB->addInstruction(NeedsRegSwap);
+ // cbgt x0, #63, target
+ MCInst Irreversible = MCInstBuilder(AArch64::CBGTXri)
+ .addReg(AArch64::X0)
+ .addImm(63)
+ .addExpr(MCSymbolRefExpr::create(TargetBB->getLabel(), *BC->Ctx.get()));
+ BB->addInstruction(Irreversible);
+
+ auto II = BB->begin();
+ ASSERT_TRUE(BC->MIB->isReversibleBranch(*II));
+ BC->MIB->reverseBranchCondition(*II, TargetBB->getLabel(), BC->Ctx.get());
+ // cblt x0, #1, target
+ ASSERT_EQ(II->getOpcode(), AArch64::CBLTXri);
+ ASSERT_EQ(II->getOperand(1).getImm(), 1);
+ II++;
+ ASSERT_TRUE(BC->MIB->isReversibleBranch(*II));
+ BC->MIB->reverseBranchCondition(*II, TargetBB->getLabel(), BC->Ctx.get());
+ // cbhi x0, #0, target
+ ASSERT_EQ(II->getOpcode(), AArch64::CBHIXri);
+ ASSERT_EQ(II->getOperand(1).getImm(), 0);
+ II++;
+ ASSERT_TRUE(BC->MIB->isReversibleBranch(*II));
+ BC->MIB->reverseBranchCondition(*II, TargetBB->getLabel(), BC->Ctx.get());
+ // cbgt x1, x0, target
+ ASSERT_EQ(II->getOpcode(), AArch64::CBGTXrr);
+ ASSERT_EQ(II->getOperand(0).getReg(), AArch64::X1);
+ ASSERT_EQ(II->getOperand(1).getReg(), AArch64::X0);
+ II++;
+ ASSERT_FALSE(BC->MIB->isReversibleBranch(*II));
+}
+
TEST_P(MCPlusBuilderTester, AArch64_CmpJE) {
if (GetParam() != Triple::aarch64)
GTEST_SKIP();
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
labrinea
temporarily deployed
to
main-branch-only
February 12, 2026 16:40 — with
GitHub Actions
Inactive
paschalis-mpeis
left a comment
Member
There was a problem hiding this comment.
Hey Alexandre,
Thanks for the unit tests.
Would you mind restructuring it a bit so we list and test each case individually?
reordered assertions added comments
sujianIBM
pushed a commit
to sujianIBM/llvm-project
that referenced
this pull request
Mar 5, 2026
…m#181177) Checks that isReversibleBranch() returns false - when the immediate value is 63 and needs +1 adjustment - when the immediate value is 0 and needs -1 adjustment Checks that reverseBranchCondition() adjusts - the opcode - the immediate operand if necessary (+/-1) - the register operands if necessary (swap)
This was referenced Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checks that isReversibleBranch() returns false
Checks that reverseBranchCondition() adjusts