From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 6A6113858004; Mon, 22 Mar 2021 09:43:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A6113858004 From: "rafal at bursig dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/99693] -O2 not move 'if' checks on const data outside the loops Date: Mon, 22 Mar 2021 09:43:33 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed 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: rafal at bursig dot org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: WONTFIX 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: Message-ID: In-Reply-To: References: 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, 22 Mar 2021 09:43:33 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99693 --- Comment #5 from rafal at bursig dot org --- Additional when I use c++ variant of this code and throw exception in else = then in -O2 level the 'if' is removed outside loop: typedef struct Update { int m_update; //... } Update; extern void antagonizer( Update * ); void antagonize(Update *data, unsigned int n) { int _update =3D data->m_update; while(--n) { if (_update) { antagonizer( data ); } else throw std::runtime_error("error"); } } The asm looks like: .LC0: .string "error" antagonize(Update*, unsigned int): mov eax, DWORD PTR [rdi] sub esi, 1 je .L12 push r12 push rbp mov rbp, rdi push rbx mov ebx, esi test eax, eax je .L3 .L4: mov rdi, rbp call antagonizer(Update*) sub ebx, 1 jne .L4 pop rbx pop rbp pop r12 ret .L12: ret antagonize(Update*, unsigned int) [clone .cold]: .L3: mov edi, 16 call __cxa_allocate_exception mov esi, OFFSET FLAT:.LC0 mov rdi, rax mov rbp, rax call std::runtime_error::runtime_error(char const*) [complete ob= ject constructor] mov edx, OFFSET FLAT:_ZNSt13runtime_errorD1Ev mov esi, OFFSET FLAT:_ZTISt13runtime_error mov rdi, rbp call __cxa_throw mov r12, rax mov rdi, rbp call __cxa_free_exception mov rdi, r12 call _Unwind_Resume when I comment the "else throw ..." then the if check return int loop=