[llvm] Add option for two-way branch optimization. (PR #161419)

Ellis Hoag via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 3 16:07:55 PST 2025


================
@@ -153,6 +153,37 @@ static cl::opt<unsigned> MisfetchCost(
 static cl::opt<unsigned> JumpInstCost("jump-inst-cost",
                                       cl::desc("Cost of jump instructions."),
                                       cl::init(1), cl::Hidden);
+
+// This enum controls how to optimize two-way branches (a conditional branch
+// immediately followed by an unconditional one). The goal is to optimize for
+// branch prediction and instruction cache efficiency.
+enum class TwoWayBranchOptStrategy {
+  // Do not reverse the condition. Leave the branch code as is.
+  None,
+  // For a two-way branch, make the hot path the fallthrough path. This is more
+  // friendly to static branch prediction (predict not-taken).
+  HotPathFallthrough,
+  // For a two-way branch, make the cold path the fallthrough path. This
+  // improves i-cache efficiency as the unconditional branch is fetched less
+  // often.
+  ColdPathFallthrough
+};
+
+static cl::opt<TwoWayBranchOptStrategy> TwoWayBranchOpt(
+    "two-way-branch-opt", cl::Hidden,
----------------
ellishg wrote:

Without context I would not be able to guess what this option does. I also prefer to prefix pass options with their pass name. What do you think about `-block-placement-two-way-branch-reorder`?

https://github.com/llvm/llvm-project/pull/161419


More information about the llvm-commits mailing list