<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jan 5, 2015 at 1:40 AM, Pete Cooper <span dir="ltr"><<a href="mailto:peter_cooper@apple.com" target="_blank">peter_cooper@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div>Hi lx, Philip</div><div><br></div><div>I've seen an instcombine which helps with this situation. It fires when the function types on both sides of the bitcast have the same number of operands and compatible types. It then adds bitcasts on the arguments and removes the one on the called function.</div></div></blockquote><div><br></div><div>It indeed does, InstCombiner::transformConstExprCastCall.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br></div><div>I don't have IR to hand, but it would be worth passing your IR through instcombine to see if that helps you.</div></div></blockquote><div><br></div><div>The following should be a sufficiently workable example of what we would hope to transform.</div><div><br></div><div><div>target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"</div><div>target triple = "x86_64-unknown-linux-gnu"</div><div><br></div><div>define i32 @g(i32* %a) {</div><div>entry:</div><div>  %call = tail call i32 bitcast (i32 (i64)* @f to i32 (i32*)*)(i32* %a)</div><div>  ret i32 %call</div><div>}</div><div><br></div><div>declare i32 @f(i64)</div><div><br></div><div>define i32 @h(i64 %a) {</div><div>entry:</div><div>  %call = tail call i32 bitcast (i32 (i32*)* @g to i32 (i64)*)(i64 %a)</div><div>  ret i32 %call</div><div>}</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br></div><div>The idea of improving the inliner is also great, but you may find that it's needed for cases other than this one if i'm right about the instcombine.</div></div></blockquote><div><br></div><div>Sadly, the combine fails because InstCombine queries CastInst::isBitCastable to determine the castable-ness of the parameter type and the argument type.  It isn't bitcastable though, it's ptrtoint/inttoptr castable.</div><div><br></div><div>The following patch opens up the optimization:</div><div><div>--- a/lib/Transforms/InstCombine/InstCombineCalls.cpp</div><div>+++ b/lib/Transforms/InstCombine/InstCombineCalls.cpp</div><div>@@ -1456,7 +1456,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {</div><div>     Type *ParamTy = FT->getParamType(i);</div><div>     Type *ActTy = (*AI)->getType();</div><div> </div><div>-    if (!CastInst::isBitCastable(ActTy, ParamTy))</div><div>+    if (!CastInst::isBitOrNoopPointerCastable(ActTy, ParamTy, DL))</div><div>       return false;   // Cannot transform this parameter value.</div><div> </div><div>     if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1).</div><div>@@ -1551,7 +1551,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) {</div><div>     if ((*AI)->getType() == ParamTy) {</div><div>       Args.push_back(*AI);</div><div>     } else {</div><div>-      Args.push_back(Builder->CreateBitCast(*AI, ParamTy));</div><div>+      Args.push_back(Builder->CreateBitOrPointerCast(*AI, ParamTy));</div><div>     }</div><div> </div><div>     // Add any parameter attributes.</div></div><div><br></div><div>Running opt -instcombine -inline -instcombine with this patch results in:</div><div><div>define i32 @g(i32* %a) {</div><div>entry:</div><div>  %0 = ptrtoint i32* %a to i64</div><div>  %call = tail call i32 @f(i64 %0)</div><div>  ret i32 %call</div><div>}</div><div><br></div><div>declare i32 @f(i64)</div><div><br></div><div>define i32 @h(i64 %a) {</div><div>entry:</div><div>  %call.i = tail call i32 @f(i64 %a)</div><div>  ret i32 %call.i</div><div>}</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="auto"><div><br></div><div>Thanks</div><div>Pete<br><br>Sent from my iPhone</div><div><div class="h5"><div><br>On Jan 5, 2015, at 3:16 AM, Liu Xin <<a href="mailto:navy.xliu@gmail.com" target="_blank">navy.xliu@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Philip, <div><br></div><div>I post here because I think AlwaysInliner should inline it. I want to detect the indirect calls for Inliner, and I want to hear inputs. </div><div><br></div><div>let me define indirect call first in my idea. In one single expression, one function may be subject to bitcast more than one time. we can detect this situation and treat it as a regular call of last function, is that okay?</div><div><br></div><div>thanks,</div><div>--lx</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 5, 2015 at 7:32 AM, Philip Reames <span dir="ltr"><<a href="mailto:listmail@philipreames.com" target="_blank">listmail@philipreames.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">On 01/04/2015 12:04 AM, Liu Xin wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
%294= callfloatbitcast (float(float, float*)* @__gpu_modff to float(float, i64)*)(float%293, i64 %preg.212.addr.0)<span><br>
<br>
as you may know, some gpu backends don't support function call. we need to make sure to inline all functions here. however, Inliner can not figure out that this is a valid callsite in this form. actually, it is.  in C words, cast a function and then call should be treat as callsite, right?<br>
<br>
</span></blockquote>
Generally, the inliner doesn't do much with indirect calls, but given there is no simpler canonical case here, I expect we'll have to.<br>
<br>
Its possible we might even want to define this as a direct call. I'm not sure what the expectations are with regards to the type of the function being called and the type of the callsite.  I suspect a lot of code would get confused if getCalledFunction returned __gpu_modff with it's unbitcast type.  That's possibly something we should fix though.<br>
<br>
We'll want to get other folks input here, but a small patch to the inliner to handle this case would seem reasonable to me.<span><font color="#888888"><br>
<br>
Philip<br>
</font></span></blockquote></div><br></div>
</div></blockquote></div></div><span class=""><blockquote type="cite"><div><span>_______________________________________________</span><br><span>LLVM Developers mailing list</span><br><span><a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a></span><br><span><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a></span><br></div></blockquote></span></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div>