[llvm] [CodeGen] [EarlyIfConversion] Prioritize conversion of hard to predict branches (PR #174457)
Jonathan Cohen via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 27 12:12:57 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).
----------------
jcohen-apple wrote:
We could restrict it to the same basic block as the condition. For the follow up PR dealing with cascading conditions that would need to be expanded to support fallthrough blocks, but it would be doable. Once the condition and the source are in the same block, I can iterate between the instructions and look for function calls.
https://github.com/llvm/llvm-project/pull/174457
More information about the llvm-commits
mailing list