Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

Dead code not optimized away by the compiler?

$
0
0

Hi,

With the Intel Compiler version 16.0.1 on OSX, the following code

#include <cstdio>
#include <cstdlib>
#include <new>
#include <type_traits>

template <typename T>
class Test {
 private:
  T* data_;

 public:
  Test(int n);
  ~Test();
};

template <typename T>
Test<T>::Test(int n) {
  if (std::is_pod<T>::value) {
    data_ = new T[n];
  } else {
    data_ = static_cast<T*>(::operator new(n * sizeof(T)));
    for (int i{0}; i < n; ++i) {
      new (data_ + i) T{};
    }
  }
};

template <typename T>
Test<T>::~Test() {
  if (std::is_pod<double>::value) {
    delete[] data_;
  } else {
    ::operator delete(data_);
  }
};

int main(int argc, const char* argv[]) {
  Test<double> x{10};

  return 0;
}

compiled with

icpc -std=c++11 -O3 -xHost -ansi-alias -fno-exceptions -DNDEBUG -opt-report=3 main.cpp -o main

produces an optimization report where it is clear that the call to ::operator new is still present in the optimization report although it is clear that at compile time, we now that this code is dead code.

INLINE REPORT: (main(int, const char **)) [1] main.cpp(37,40)
  -> INLINE (MANUAL): (38,18) Test<double>::Test(Test<double> *, int)
    -> EXTERN: (19,20) operator new[](size_t)
    -> INLINE (MANUAL): (23,12) operator new(size_t, void *)
  -> INLINE (MANUAL): (40,3) Test<double>::~Test(Test<double> *)
    -> EXTERN: (31,5) operator delete[](void *)

I suspect this is an optimization bug of the Intel compiler. But it seems that the assembly does not contain any code related to operator new.

Best regards,

Francois


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>