From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 679AE398580D; Mon, 21 Sep 2020 15:13:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 679AE398580D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600701197; bh=xnN7kRmCUHBf2niS5TCeOzqsgW2xNUjLxgFgX1FGtxM=; h=From:To:Subject:Date:From; b=tsCfX/iqL2hYCZCkUAHfrYIzGY4mD2I41vsrODS5DBhKHmc7V1qfTXP3x/2RnNnqz u6GNybIPRSj6n7bh3EOtAj8Vj8E9kdxL6fPljjIU3TQqcyhMHQ+UH/AYWQZcWRsJTJ 8H4wKh03HMO/1cY3D9L7YgFPx50enYml5tJ49/08= From: "marat at slonopotamus dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/97151] New: GCC fails to optimize away uselessly allocated arrays (C++) Date: Mon, 21 Sep 2020 15:13:17 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: marat at slonopotamus dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Sep 2020 15:13:17 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97151 Bug ID: 97151 Summary: GCC fails to optimize away uselessly allocated arrays (C++) Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: marat at slonopotamus dot org Target Milestone: --- A test program: int main() { int *a =3D new int[10](); delete[] a; return 0; } Tested against: gcc 10.2.0 and gcc trunk as of 20200920 on codebolt.org with -O2, -O3 and -Ofast Expected: new/delete are optimized away under -O2 and higher because "a" is never used for anything useful (like, side effects or whatever). Actual: new/delete are present in assembly: main: sub rsp, 8 mov edi, 40 call operator new[](unsigned long) pxor xmm0, xmm0 mov QWORD PTR [rax+32], 0 mov rdi, rax movups XMMWORD PTR [rax], xmm0 movups XMMWORD PTR [rax+16], xmm0 call operator delete[](void*) xor eax, eax add rsp, 8 ret Interesting observations: 1. if "delete[]" is commented out, whole program is optimized away 2. if "new int[10]()" is replaced with "new int[10]", whole program is optimized away 3. if new/delete is replaced with matching malloc/free, whole program is optimized away If someone wants to play with this example online, here it is: https://godbolt.org/z/YoGzxo=