<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 - Miscompilation Removes Necessary Function Call in Clang 10.0.1"
   href="https://bugs.llvm.org/show_bug.cgi?id=47963">47963</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompilation Removes Necessary Function Call in Clang 10.0.1
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>10.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>gavin.d.howard@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=24097" name="attach_24097" title="bugpoint reduced test case">attachment 24097</a> <a href="attachment.cgi?id=24097&action=edit" title="bugpoint reduced test case">[details]</a></span>
bugpoint reduced test case

# Overview

Clang 10.0.1, as packaged by Gentoo on x86_64, miscompiles a library I develop
by removing a call to a necessary function and then optimizing to `return 0;`

The library in question is <a href="https://git.yzena.com/Yzena/Yc">https://git.yzena.com/Yzena/Yc</a>, specifically the
function `ymap_at()` at
<a href="https://git.yzena.com/Yzena/Yc/src/branch/master/src/map/map.c#L422">https://git.yzena.com/Yzena/Yc/src/branch/master/src/map/map.c#L422</a>.

When compiling using either CMAKE_BUILD_TYPE as Release (`-O3 -DNDEBUG`) or
RelWithDebInfo (`-O2 -g -DNDEBUG`), ymap_at() is compiled to:

```
00000000004066b0 <ymap_at>:
  4066b0:       31 c0                   xor    %eax,%eax
  4066b2:       c3                      retq   
  4066b3:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  4066ba:       00 00 00 
  4066bd:       0f 1f 00                nopl   (%rax)
```

as shown by `objdump -d`. As you can see, despite having a call to
`ymap_index()` and a check on the return value, clang compiled it as `return
0;`.

However, when compiling with CMAKE_BUILD_TYPE as Debug (`-g`), there is no
miscompilation.

# Steps to Reproduce

```
cd $ROOT
git clone <a href="https://git.yzena.com/Yzena/Yc.git">https://git.yzena.com/Yzena/Yc.git</a> yc
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/path/to/clang ../yc
make
./tests/map/map_all
```

## Actual Results

An abort(). This happens because of these lines:

<a href="https://git.yzena.com/Yzena/Yc/src/branch/master/tests/map/map_all.c#L56-L61">https://git.yzena.com/Yzena/Yc/src/branch/master/tests/map/map_all.c#L56-L61</a>

Basically, because `ymap_at()` is just returning 0 (NULL), the test thinks the
item requested from the map does not exist and aborts (fails) the test.

## Expected Results

Test case passes (with 0 exit code).

# Build Date and Hardware

Up-to-date Gentoo Linux x86_64.

Latest master compiled on 2020-10-25 and tested. Bug exists there as well.

# Additional Information

I have verified that gcc does *not* miscompile that code, and I have also
verified that valgrind returns clean when compiling with both gcc and clang.

Attached is the bugpoint reduced test case that I created with the following:

```
cd $ROOT/build
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS="-flto" \
   -DCMAKE_C_COMPILER=/path/to/clang \
   -DCMAKE_EXE_LINKER_FLAGS="-Wl,-plugin-opt=emit-llvm,-O0" \
   ../yc
make
cd tests/map
ln -s map_all map_all.bc
cd ../..
bugpoint --llc-safe tests/map/map_all.bc
```

For the commands above, bugpoint took 6 hours 26 minutes to run.

I am putting this bug in the `new-bugs` product because I am new and don't know
where to put this bug, though I will suspect it goes into libraries because the
bug is not in clang's frontend.</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>