<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - CallSite::getCalledFunction returns null if the function callee pointer is a bitcast"
   href="http://llvm.org/bugs/show_bug.cgi?id=15334">15334</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CallSite::getCalledFunction returns null if the function callee pointer is a bitcast
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Support Libraries
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>khilan.gudka@cl.cam.ac.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>csdavec@swan.ac.uk, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=10055" name="attach_10055" title="proposed patch">attachment 10055</a> <a href="attachment.cgi?id=10055&action=edit" title="proposed patch">[details]</a></span>
proposed patch

Dear all

I think there is a bug in CallSite.getCalledFunction() whereby if the callee
function pointer is bitcast'd, getCalledFunction() will return null even if it
is still a direct call. I came across this when trying to generate a callgraph
for the freebsd version of gzip linked together with libz (the linking is
performed using llvm-link). The generated callgraph for gzip+libz missed the
call from gz_compress to deflate.

Digging into why this edge wasn't generated, I narrowed it down to this portion
in lib/Analysis/IPA/CallGraph.cpp: 

144 if (CS) {
145   const Function *Callee = CS.getCalledFunction();
146   if (!Callee)
147     // Indirect calls of intrinsics are not allowed so no need to check.
148     Node->addCalledFunction(CS, CallsExternalNode);
149   else if (!Callee->isIntrinsic())
150     Node->addCalledFunction(CS, getOrInsertFunction(Callee));
151 }

And in particular that CS.getCalledFunction() was returning null even though
the call was a direct one. The reason for this is because prior to linking gzip
with libz, gz_compress contained the call:

%call69 = call i32 @deflate(%struct.z_stream_s* %z, i32 4) nounwind

However, after linking, gz_compress now contained this call instead:

%call69 = call i32 bitcast (i32 (%struct.z_stream_s.1*, i32)* @deflate to i32
(%struct.z_stream_s*, i32)*)      (%struct.z_stream_s* %z, i32 4) nounwind

I.e. a bitcast is introduced to handle renaming of types during linking. As a
result, when getCalledFunction casts the called value to a Function, it returns
null.

The fix seems to remove the cast inside getCalledFunction as follows:

return dyn_cast<FunTy>(getCalledValue()->stripPointerCasts());

I've attached this proposed patch.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>