[llvm] [CodeGen] [EarlyIfConversion] Prioritize conversion of hard to predict branches (PR #174457)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 27 06:16:44 PST 2026


================
@@ -896,6 +916,109 @@ void EarlyIfConverter::invalidateTraces() {
   Traces->verifyAnalysis();
 }
 
+static bool isConstantPoolLoad(const MachineInstr *MI) {
+  if (!MI->mayLoad())
+    return false;
+
+  return any_of(MI->memoperands(), [](MachineMemOperand *MOp) {
+    const PseudoSourceValue *PSV = MOp->getPseudoValue();
+    return PSV && PSV->isConstantPool();
+  });
+
+  return false;
+}
+
+/// Check if a register's value comes from a memory load by walking the
+/// def-use chain. We want to prioritize converting branches which
+/// depend on values loaded from memory (unless they are loop invariant,
+/// or come from a constant pool).
+bool EarlyIfConverter::doOperandsComeFromMemory(Register Reg) {
+  if (!Reg.isVirtual())
+    return false;
+
+  // Walk the def-use chain.
+  SmallPtrSet<const MachineInstr *, 8> VisitedInstrs;
+  SmallVector<const MachineInstr *> Worklist;
+  SmallVector<Register, 16> VisitedRegs;
+
+  MachineInstr *DefMI = MRI->getVRegDef(Reg);
+  // The operand is defined outside of the function - it does not
+  // come from memory access.
+  if (!DefMI)
+    return false;
+
+  // Get the loop containing the branch to ensure we only consider loads
+  // that are "recent" in program time (i.e., in the same loop iteration).
----------------
fhahn wrote:

might be good to clarify that this just approximate 'recent', as there could be many ohter cases where the load is not recent (e.g. irreducible loops, calling large functions in between), but I think there's no easy way to further restrict this?>

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


More information about the llvm-commits mailing list