[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).
+ MachineLoop *BranchLoop = Loops->getLoopFor(IfConv.Head);
+
+ Worklist.push_back(DefMI);
+ VisitedRegs.push_back(Reg);
+
+ while (!Worklist.empty() && VisitedInstrs.size() < MaxNumSteps) {
+ const MachineInstr *MI = Worklist.pop_back_val();
+ if (!VisitedInstrs.insert(MI).second)
+ continue;
+
+ // Check if this instruction is a load.
+ if (MI->mayLoad() && !isConstantPoolLoad(MI) &&
+ !MI->isDereferenceableInvariantLoad()) {
+ // Only consider the load as data-dependent if it's in the same loop
+ // as the branch.
----------------
fhahn wrote:
```suggestion
// Only consider the load as data-dependent if it's in the same loop
// as the branch or both are outside loops
```
https://github.com/llvm/llvm-project/pull/174457
More information about the llvm-commits
mailing list