[llvm] [SimplifyCFG] Treat umul + extract pattern as cheap single instruction. (PR #124933)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 31 15:17:40 PST 2025


================
@@ -3329,20 +3331,29 @@ bool SimplifyCFGOpt::speculativelyExecuteBB(BranchInst *BI,
     else
       ++SpeculatedInstructions;
 
-    if (SpeculatedInstructions > 1)
-      return false;
-
     // Don't hoist the instruction if it's unsafe or expensive.
     if (!IsSafeCheapLoadStore &&
         !isSafeToSpeculativelyExecute(&I, BI, Options.AC) &&
         !(HoistCondStores && !SpeculatedStoreValue &&
           (SpeculatedStoreValue =
                isSafeToSpeculateStore(&I, BB, ThenBB, EndBB))))
       return false;
-    if (!IsSafeCheapLoadStore && !SpeculatedStoreValue &&
-        computeSpeculationCost(&I, TTI) >
+
+    if (match(&I,
+              m_ExtractValue<1>(m_OneUse(
+                  m_Intrinsic<Intrinsic::umul_with_overflow>(m_Value())))) &&
+        ThenBB->size() <= 3)
+      PatternFound = true;
+
+    BlockCostSoFar += computeSpeculationCost(&I, TTI);
+    if (!PatternFound && !IsSafeCheapLoadStore && !SpeculatedStoreValue &&
+        BlockCostSoFar >
             PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
       return false;
+    // If we don't find any pattern, that must be cheap, then only speculatively
+    // execute a single instruction (not counting the terminator).
+    if (!PatternFound && SpeculatedInstructions > 1)
+      return false;
----------------
goldsteinn wrote:

Err, I was thinking something a bit different, particularly I don't really see the reason for accumulating `BlockCostSoFar` for the `extract` and `mul.overflow` if the expressed intent is to treat it as a single instruction.

What about something like:
``` 
    bool PartialInst = false;
    ...
    // Not count load/store into cost if target supports conditional faulting
    // b/c it's cheap to speculate it.
    if (IsSafeCheapLoadStore)
      SpeculatedConditionalLoadsStores.push_back(&I);
    else if (match(&I, m_ExtractValue<1>(
                           m_OneUse(m_WithOverflowInst(m_Value())))) &&
             I.getOperand(0)->getParent() == ThenBB)
      // We are treating (extract (mul/sub/add.overflow), 1) as a single
      // instruction
      PartialInst = true;
    else
      ++SpeculatedInstructions;
     ...
    if (!IsSafeCheapLoadStore && !SpeculatedStoreValue && !PartialInst &&
        computeSpeculationCost(&I, TTI) >
            PHINodeFoldingThreshold * TargetTransformInfo::TCC_Basic)
      return false;
```

Although I'm not really sure if the `PartialInst` part is necessary at all in fact.

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


More information about the llvm-commits mailing list