<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Semantics of address taken for Global Variables (GV)"
   href="https://bugs.llvm.org/show_bug.cgi?id=33143">33143</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Semantics of address taken for Global Variables (GV)
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Interprocedural Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>davide@freebsd.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>filcab@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, rafael.espindola@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Interprocedural optimizations sometimes leverage that a function or a GVs has
not its address taken to perform more aggressive optimizations, e.g. SCCP keeps
track of internal GVs with addres not taken to propagate constants.

Some passes, SCCP in particular, roll their own checking for `AddressIsTaken`.
For functions, we should just use `F.hasAddressTaken()`, but there seems to not
be an equivalent for GVs.
I think the semantics of "addressTaken" for GVs need to be clarified, and
`hasAddressTaken` should a member variable of GV.

This would allow us to remove the following hack:

static bool AddressIsTaken(const GlobalValue *GV) {
  // Delete any dead constantexpr klingons.
  GV->removeDeadConstantUsers();

  for (const Use &U : GV->uses()) {
    const User *UR = U.getUser();
    if (const auto *SI = dyn_cast<StoreInst>(UR)) {
      if (SI->getOperand(0) == GV || SI->isVolatile())
        return true;  // Storing addr of GV.
    } else if (isa<InvokeInst>(UR) || isa<CallInst>(UR)) {
      // Make sure we are calling the function, not passing the address.
      ImmutableCallSite CS(cast<Instruction>(UR));
      if (!CS.isCallee(&U))
        return true;
    } else if (const auto *LI = dyn_cast<LoadInst>(UR)) {
      if (LI->isVolatile())
        return true;
    } else if (isa<BlockAddress>(UR)) {
      // blockaddress doesn't take the address of the function, it takes addr
      // of label.
    } else {
      return true;
    }
  }
  return false;
}

and use the function in `GV` instead.</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>