<div dir="ltr"><div>Hey all - me again,</div><div><br></div><div>So I'm looking at llvm.expect specifically for branch hints. In the following example LLVM will hoist the pow/cos calls into the entry block even though I've used the llvm.expect intrinsic to make it clear that one of the calls is unlikely to occur.</div><div><br></div><div>target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"<br>target triple = "x86_64-pc-windows-msvc-coff"<br><br>define dllexport double @foo(i32 %val) {<br>entry:<br> %0 = icmp slt i32 %val, 42<br> %1 = call i1 @llvm.expect.i1(i1 %0, i1 false)<br> %2 = sitofp i32 %val to double<br> br i1 %1, label %true, label %false<br><br>true:<br> %3 = call double @llvm.cos.f64(double %2)<br> br label %merge<br><br>false:<br> %4 = call double @llvm.pow.f64(double %2, double 4.242000e+01)<br> br label %merge<br><br>merge:<br> %var.1.0 = phi double [ %4, %false ], [ %3, %true ]<br> ret double %var.1.0<br>}<br><br>declare i1 @llvm.expect.i1(i1, i1)<br>declare double @llvm.cos.f64(double)<br>declare double @llvm.pow.f64(double, double)</div><div><br></div><div>This seems counter intuitive to me - I've told LLVM that one of the calls will probably not happen, and I expected it to preserve the call in the unlikely branch so we don't pay the cost for something unlikely to be used.</div><div><br></div><div>I also injected a pass locally that adds, for branches whose condition is llvm.expect, the branch weight metadata - but LLVM will still always fold the branch away ensuring that the expensive call is always called.<br></div><div><br></div><div>The part of SimplifyCFG that does this is FoldTwoEntryPHINode from what I can tell.</div><div><br></div><div>So is there anything I can do here without modifying LLVM? Have I missed something?</div><div><br></div><div>Otherwise I guess I'd have to change
FoldTwoEntryPHINode to not do this in the presence of branch weights / expect?</div><div><br></div><div>Thanks for any help,</div><div><br></div><div>Cheers,</div><div>-Neil.<br></div><div><br>-- <br><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><table style="border-collapse:collapse;border-spacing:0px;color:rgb(90,90,91);font-size:13px;margin:0px 0px 20px;padding:0px" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px 0px 20px;vertical-align:top" align="left"><table style="border-collapse:collapse;border-spacing:0px;margin:0px;padding:0px" cellspacing="0" cellpadding="0" border="0" align="left"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:1.12em;line-height:1.5em;padding:0px;vertical-align:top;width:64px"><img style="border:medium none currentcolor;border-radius:0px;display:block;font-size:13px;height:auto;line-height:100%;margin:0px;max-width:100%;outline-style:none;outline-width:medium;padding:20px 0px 0px;width:100%" alt="" src="https://unity3d.com/profiles/unity3d/themes/unity/images/ui/other/unity-logo-dark-email.png" width="64" height="auto"></td></tr></tbody></table></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;font-weight:600;line-height:15px;margin:0px;padding:0px">Neil Henning</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;line-height:15px;margin:0px;padding:0px 0px 10px">Senior Software Engineer Compiler</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:12px;line-height:15px;margin:0px;padding:0px"><a href="http://unity.com" target="_blank">unity.com</a></div></td></tr></tbody></table></div></div></div></div>