[llvm] [EarlyIfConversion] Extend data dependent analysis across multiple blocks (PR #180623)

Jon Roelofs via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 13:02:33 PDT 2026


================
@@ -925,31 +939,132 @@ static bool isConstantPoolLoad(const MachineInstr *MI) {
          });
 }
 
-/// Check if there are any calls in the range (From, To].
-static bool callInRange(const MachineInstr *From, const MachineInstr *To) {
-  constexpr int MaxInstructionsToCheck = 64;
-  int Count = 0;
-  auto InstrRange =
-      make_range(std::next(From->getIterator()), To->getIterator());
-  return any_of(InstrRange, [&](const MachineInstr &MI) {
-    return ++Count > MaxInstructionsToCheck || MI.isCall();
-  });
+/// Check whether the load in From and the condition in To are far apart, i.e.
+/// whether a call or a loop can be executed between them. This is done by first
+/// scanning the instructions within From and To MBBs. If no call is found, we
+/// then scan all blocks which are dominated by From (the load) and can reach To
+/// (the condition), looking for calls and for blocks belonging to a loop the
+/// condition is not part of.
+bool EarlyIfConverter::hasCallOrLoopInRange(const MachineInstr *From,
+                                            const MachineInstr *To) {
+  if (From == To)
+    return false;
+
+  LLVM_DEBUG(dbgs() << "  checking for a call or loop between " << *From
+                    << "  and " << *To);
+  assert(DomTree->dominates(From, To) && "From is expected to dominate To");
+
+  const MachineBasicBlock *FromBB = From->getParent();
+  const MachineBasicBlock *ToBB = To->getParent();
+
+  unsigned NumScanned = 0;
+  auto HitSearchLimit = [&](unsigned N) {
+    NumScanned += N;
+    if (NumScanned <= MaxRegionInstrs)
+      return false;
+    LLVM_DEBUG(dbgs() << "  hasCallOrLoopInRange scanned more than "
+                      << MaxRegionInstrs << " instructions\n");
+    return true;
+  };
+  auto FoundCall = [](const MachineInstr &MI) {
+    LLVM_DEBUG(dbgs() << "  found a call before the condition: " << MI);
+    return true;
+  };
+  auto IsCallOrHitSearchLimit = [&](const MachineInstr &MI) {
+    if (HitSearchLimit(1))
+      return true;
+    if (!MI.isCall())
----------------
jroelofs wrote:

I think you also need `MI.hasUnmodeledSideEffects() && MI.isInlineAsm()` checks, since I think what you're really checking for here is whether something might clobber a flags register.

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


More information about the llvm-commits mailing list