[Bug c/88481] New: -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

Bug ID: 88481
   Summary: -O1 causes optimizer to drop 'then' clause in
conditional
   Product: gcc
   Version: 8.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: heinrich.seebauer at swistec dot de
  Target Milestone: ---

Compiling with option -O1 causes the optimizer to drop code for the 'then'
branch.
Verified for targets
  gnueabi-linux (host=x86/linux, target=arm/linux), and
  x86 (native)

Compilation produces correct results with options -O0, -O2, -O3, -Os.

If -O1 options
-fdefer-pop 
-fdelayed-branch 
-fguess-branch-probability 
-fcprop-registers 
-floop-optimize 
-fif-conversion 
-fif-conversion2 
-ftree-ccp 
-ftree-dce 
-ftree-dominator-opts 
-ftree-dse 
-ftree-ter 
-ftree-lrs 
-ftree-sra 
-ftree-copyrename 
-ftree-fre 
-ftree-ch 
-funit-at-a-time 
-fmerge-constants
are used, the result is correct. However, adding -O1 to this list of options
results in the missing code.

-
the exact version of GCC:
gcc --version
gcc (SUSE Linux) 8.2.1 20181108 [gcc-8-branch revision 265914]

the options given when GCC was configured/built:
unknown

the complete command line that triggers the bug:
gcc -D_GNU_SOURCE=1 -D_POSIX_SOURCE=1 -DDEBUG=1 -DWITH_MBEDTLS=1 -DTARGET=PC
-I.././inc -I../modelSource -I../../libiec61850/config
-I../../libiec61850/src/logging -I../../libiec61850/src/hal/inc
-I../../libiec61850/src/common/inc -I../../libiec61850/src/mms/inc
-I../../libiec61850/src/iec61850/inc
-I../../libiec61850/src/iec61850/inc_private
-I../../libiec61850/src/mms/inc_private -I../../libiec61850/third_party/sqlite
-I../../libiec61850/third_party/mbedtls/include -I../../libiec61850/src/tls
-I../../libiec61850/src/tls/mbedtls -I../../libiec61850/src/mms/iso_mms/asn1c
-O1 -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -g -Wall -c
-fmessage-length=0 -MMD -MP -MF"src/runtime/actPow_ggioRt.d"
-MT"src/runtime/actPow_ggioRt.o" -o "src/runtime/actPow_ggioRt.o"
"../src/runtime/actPow_ggioRt.c"

the compiler output (error messages, warnings, etc.):
gcc -D_GNU_SOURCE=1 -D_POSIX_SOURCE=1 -DDEBUG=1 -DWITH_MBEDTLS=1 -DTARGET=PC
-I.././inc -I../modelSource -I../../libiec61850/config
-I../../libiec61850/src/logging -I../../libiec61850/src/hal/inc
-I../../libiec61850/src/common/inc -I../../libiec61850/src/mms/inc
-I../../libiec61850/src/iec61850/inc
-I../../libiec61850/src/iec61850/inc_private
-I../../libiec61850/src/mms/inc_private -I../../libiec61850/third_party/sqlite
-I../../libiec61850/third_party/mbedtls/include -I../../libiec61850/src/tls
-I../../libiec61850/src/tls/mbedtls -I../../libiec61850/src/mms/iso_mms/asn1c
-O1 -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -g -Wall -c
-fmessage-length=0 -MMD -MP -MF"src/iec61850/fsch.d" -MT"src/iec61850/fsch.o"
-o "src/iec61850/fsch.o" "../src/iec61850/fsch.c"
Finished building: ../src/iec61850/fsch.c

the preprocessed file: see attachment
-

source code snippet:
...
fsch.c:423: if (forwardBackwardDiff < 0)
fsch.c:427:   timeMatchedSec += SECONDS_PER_HOUR;
...

if condition (forwardBackwardDiff < 0) is true, the addition is not executed.
Disassembly shows that no code has been generated for line 427. Debugging shows
that the code line is effectively skipped, though the condition is true.

gdb disassembly:
423 if (forwardBackwardDiff < 0)
00409111:   lea 0xe10(%r12),%rax
00409119:   pxor%xmm1,%xmm1
0040911d:   comisd  %xmm0,%xmm1
00409121:   cmova   %rax,%r12
429 strTmRt->nextStartTimeSec=timeMatchedSec;
00409125:   mov %r12,0x28(%rbx)
430
strTmRt->prevStartTimeSec=timeMatchedSec-SECONDS_PER_HOUR;
00409129:   lea -0xe10(%r12),%rdi
00409131:   mov %rdi,0x20(%rbx)

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

--- Comment #2 from Heinrich Seebauer  ---
Created attachment 45227
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45227&action=edit
precompiled source of fsch.c

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

--- Comment #3 from Heinrich Seebauer  ---
Thanks, Alexander, for your advice, I will try to isolate this symptom further.
Added the attachment ...

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

--- Comment #4 from Heinrich Seebauer  ---
To isolate the cause, I suppressed several optimization options from thge -O1
group. Adding -fno-if-conversion to the options got the code correct, other -O1
options didn't change anything.
I think, this is some strong evidence that -fif-conversion causes the bug.

Proposed workaround: use option "-fno-if-conversion" in combination with -O1.

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

--- Comment #7 from Heinrich Seebauer  ---
Created attachment 45232
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45232&action=edit
zipped asm files for three cases

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-13 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

--- Comment #8 from Heinrich Seebauer  ---
Sorry, my comment was lost completely after uploading the attachment.
Try again...

So I hope, I got this right.

-fdbg-cnt=if_conversion:xx,if_after_combine:yy

xx  yy  result
---
20  20  success
20  21  fail
0   21  fail
21  20  fail

tested with -fdbg-cnt=if_conversion:20,if_after_combine:21 -fno-if-conversion
and got functioning code.

I did gdb stepping in the source file where I observed, that the condition was
true, but the branch was not executed. Same with the tests here - they were all
strepped via source code.

[Bug c/88481] -O1 causes optimizer to drop 'then' clause in conditional

2018-12-14 Thread heinrich.seebauer at swistec dot de
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88481

Heinrich Seebauer  changed:

   What|Removed |Added

 Status|WAITING |RESOLVED
 Resolution|--- |INVALID

--- Comment #10 from Heinrich Seebauer  ---
Alexander, you are right, it is only strange behaviour. Using -O1 drops the
'then' clause, but the results are the same, if one takes a broader view.

The program deals with controlling schedules using actual time. Each test run
has different input values (current UTC time). I set up a situation where I was
able to control system time, and the outcomes were consistent, either using -O0
oe -O1.

What confused me was the fact that -O2/-Os did NOT drop the then clause. So -O2
ist not just optimizing the -O1 outcome, but different from the beginning of
the optimizer pass.

I apologize for not getting this point quickly.
Thank you very much for your support and your work for the commuinty,

kind regards
Heinrich