<html>
    <head>
      <base href="https://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 --- - There is a bug in clang when we try to make our own implementation for the "new" operator with side effects."
   href="https://llvm.org/bugs/show_bug.cgi?id=25173">25173</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>There is a bug in clang when we try to make our own implementation for the "new" operator with side effects.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>troshkovdanil@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hi all!

There is a bug in clang when we try to make our own implementation for the
"new" operator with side effects.
How to reproduce:

Test t.cpp:

#include <new>
#include <cstdlib>
#include <cassert>

extern int new_called;

struct A{};

int main()
{
    new_called = 0;
    A* ap = new A;
    assert(new_called);
}

Test t2.cpp:

#include <new>
#include <cstdlib>

int new_called = 0;

void* operator new(std::size_t s) throw(std::bad_alloc)
{
    ++new_called;
    return std::malloc(s);
}

Compile with O0 and run:

clang++ t.cpp t2.cpp -O0 -o O0.out
./O0.out

Ok.

Compile with O1 and run:

clang++ t.cpp t2.cpp -O1 -o O1.out
./O1.out

Fail:

O1.out: t.cpp:13: int main(): Assertion `new_called' failed.
Aborted (core dumped)

1) I've tried to find out the reason:

clang++ t.cpp t2.cpp -O1 -o O1.out -mllvm -print-after-all 2> outpO1

outpO1:

define i32 @main() #0 {
  store i32 0, i32* @new_called, align 4, !tbaa !1
  %1 = call noalias i8* @_Znwm(i64 1) #4
  %2 = load i32, i32* @new_called, align 4, !tbaa !1
  %3 = icmp ne i32 %2, 0
  br i1 %3, label %5, label %4
...
*** IR Dump After Combine redundant instructions ***
; Function Attrs: uwtable
define i32 @main() #0 {
  store i32 0, i32* @new_called, align 4, !tbaa !1
  br i1 false, label %2, label %1

As far as I can understand we removed _Znwm in InstCombiner::visitCallSite
because isKnownNonNullAt->isKnownNonNull->isOperatorNewLikeFn is true.
But we used our own version of _Znwm with side effects and we should not have
removed it!

2) O0 does not do "Combine redundant instructions" so the program works
properly.

3) The reason why we use t.cpp and t2.cpp because when the whole code is in one
file clang does inline (increment of "new_called") before "Combine redundant
instructions", and the program works properly.

4) cnag++ -v:

clang version 3.8.0 (trunk 250272)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/dtroshkov/LLVM/bug/targetlibinfo/../../install/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.1.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.1.0
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Candidate multilib: .;@m64
Selected multilib: .;@m64</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>