<div dir="ltr"><div class="gmail_extra"><span style="font-size:12.8px">Hi all,</span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">I came across an issue caused by the Call-Graph Simplify Pass. Here is a a small repro:</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">```</div><div style="font-size:12.8px"><div>define double @foo(double %a1, double %a2, double %a3) #0 {</div><div>entry:</div><div>  %a_mul = fmul double %a1, %a2</div><div>  %a_cmp = fcmp ogt double %a3, %a_mul</div><div>  br i1 %a_cmp, label %a.then, label %a.end</div><div><br></div><div>a.then:                                          </div><div>  %a_div = fdiv double %a_mul, %a3</div><div>  br label %a.end</div><div><br></div><div>a.end:</div><div>  %a_factor = phi double [ %a_div, %a.then ], [ 1.000000e+00, %entry ]</div><div>  ret double %a_factor</div><div>}</div></div><div style="font-size:12.8px">```</div><div style="font-size:12.8px">Here, the conditional is guarding a possible division by zero. However if I run CGSimplify on this I get:</div><div style="font-size:12.8px">```</div><div style="font-size:12.8px"><div>define double @foo(double %a1, double %a2, double %a3) local_unnamed_addr #0 {</div><div>entry:</div><div>  %a_mul = fmul double %a1, %a2</div><div>  %a_cmp = fcmp olt double %a_mul, %a3</div><div>  %a_div = fdiv double %a_mul, %a3</div><div>  %a_factor = select i1 %a_cmp, double %a_div, double 1.000000e+00</div><div>  ret double %a_factor</div><div>}</div></div><div style="font-size:12.8px">``` </div><div style="font-size:12.8px">This will cause a FP arithmetic exception, and the application will get a SIGFPE signal. The code that drives the change in the optimizer relies on `llvm::<wbr>isSafeToSpeculativelyExecute` <wbr>to decide whether the division should be performed speculatively. Right now, this function returns true. One could do something similar to integer divisions and add a case there (this solved the issue for me):</div><div style="font-size:12.8px">```</div><div style="font-size:12.8px"><div>diff --git a/lib/Analysis/ValueTracking.<wbr>cpp b/lib/Analysis/ValueTracking.<wbr>cpp</div><div>index 1761dac..c61fefd 100644</div><div>--- a/lib/Analysis/ValueTracking.<wbr>cpp</div><div>+++ b/lib/Analysis/ValueTracking.<wbr>cpp</div><div>@@ -3352,6 +3352,21 @@ bool llvm::<wbr>isSafeToSpeculativelyExecute(<wbr>const Value *V,</div><div>     // The numerator *might* be MinSignedValue.</div><div>     return false;</div><div>   }</div><div>+  case Instruction::FDiv:</div><div>+  case Instruction::FRem:{</div><div>+    const Value *Denominator = Inst->getOperand(1);</div><div>+    // x / y is undefined if y == 0</div><div>+    // The denominator is not a constant, so there is nothing we can do to prove</div><div>+    // it is non-zero.</div><div>+    if (auto *VV = dyn_cast<ConstantVector>(<wbr>Denominator))</div><div>+      Denominator = VV->getSplatValue();</div><div>+    if (!isa<ConstantFP>(Denominator)<wbr>)</div><div>+      return false;</div><div>+    // The denominator is a zero constant - we can't speculate here.</div><div>+    if (m_AnyZero().match(<wbr>Denominator))</div><div>+      return false;</div><div>+    return true;</div><div>+  }</div><div>   case Instruction::Load: {</div><div>     const LoadInst *LI = cast<LoadInst>(Inst);</div><div>     if (!LI->isUnordered() ||</div></div><div style="font-size:12.8px">```</div><div style="font-size:12.8px">I did my tests using a powerpc64le target, but I couldn't find any target specific login involved in this transform. In any case, I wanted to drop the questions: </div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">- is there any target that would benefit from speculative fp divisions? </div><div style="font-size:12.8px">- is there any target for which fp division does not have side effects? </div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">If not, I can go ahead and post the patch above for review.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Many thanks!</div><div style="font-size:12.8px">Sam</div></div></div>