Bug 61673 - [4.9/4.10 Regression] Miscompilation of _gnutls_hostname_compare on s390
Summary: [4.9/4.10 Regression] Miscompilation of _gnutls_hostname_compare on s390
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: 4.9.1
: P2 normal
Target Milestone: 4.9.1
Assignee: Jakub Jelinek
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2014-07-02 14:53 UTC by Jakub Jelinek
Modified: 2014-07-08 15:55 UTC (History)
1 user (show)

See Also:
Host:
Target: s390-linux
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
gcc49-pr61673.patch (736 bytes, patch)
2014-07-02 15:19 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2014-07-02 14:53:20 UTC
char e;

__attribute__((noinline, noclone)) void
foo (const char *x)
{
  char d = x[0];
  int c = d;
  if ((c >= 0 && c <= 0x7f) == 0)
    e = d;
}

int
main ()
{
  const char c[] = { 0x54, 0x87 };
  e = 0x21;
  foo (c);
  if (e != 0x21)
    __builtin_abort ();
  foo (c + 1);
  if (e != 0x87)
    __builtin_abort ();
  return 0;
}

is miscompiled supposedly starting with r202393.  Before combine we have:
(insn 7 6 8 2 (parallel [
            (set (reg:SI 47 [ D.1404 ])
                (and:SI (subreg:SI (reg/v:QI 44 [ d ]) 0)
                    (const_int 255 [0xff])))
            (clobber (reg:CC 33 %cc))
        ]) gnutls2.c:8 455 {*andsi3_esa}
     (expr_list:REG_UNUSED (reg:CC 33 %cc)
        (nil)))
(insn 8 7 9 2 (set (reg:CCU 33 %cc)
        (compare:CCU (reg:SI 47 [ D.1404 ])
            (const_int 127 [0x7f]))) gnutls2.c:8 39 {*cmpsi_ccu}
     (expr_list:REG_DEAD (reg:SI 47 [ D.1404 ])
        (nil)))
(jump_insn 9 8 10 2 (set (pc)
        (if_then_else (leu (reg:CCU 33 %cc)
                (const_int 0 [0]))
            (label_ref:SI 15)
            (pc))) gnutls2.c:8 599 {*cjump_31}
     (expr_list:REG_DEAD (reg:CCU 33 %cc)
        (int_list:REG_BR_PROB 3900 (nil)))
 -> 15)
and combine turns this into:
(note 7 6 8 2 NOTE_INSN_DELETED)
(insn 8 7 9 2 (parallel [
            (set (reg:CCZ 33 %cc)
                (compare:CCZ (and:SI (subreg:SI (reg/v:QI 44 [ d ]) 0)
                        (const_int -128 [0xffffffffffffff80]))
                    (const_int 0 [0])))
            (clobber (scratch:SI))
        ]) gnutls2.c:8 453 {*andsi3_cconly}
     (nil))
(jump_insn 9 8 10 2 (set (pc)
        (if_then_else (eq (reg:CCZ 33 %cc)
                (const_int 0 [0]))
            (label_ref:SI 15)
            (pc))) gnutls2.c:8 599 {*cjump_31}
     (expr_list:REG_DEAD (reg:CCU 33 %cc)
        (int_list:REG_BR_PROB 3900 (nil)))
 -> 15)

The -128 should have been 128, we want to test the sign bit of QImode rather than also any of the higher bits that contain garbage.
Comment 1 Jakub Jelinek 2014-07-02 15:19:40 UTC
Created attachment 33050 [details]
gcc49-pr61673.patch

Untested fix.
Comment 2 Jakub Jelinek 2014-07-08 15:40:08 UTC
Author: jakub
Date: Tue Jul  8 15:39:36 2014
New Revision: 212364

URL: https://gcc.gnu.org/viewcvs?rev=212364&root=gcc&view=rev
Log:
	PR rtl-optimization/61673
	* combine.c (simplify_comparison): Test just mode's sign bit
	in tmode rather than the sign bit and any bits above it.

	* gcc.c-torture/execute/pr61673.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr61673.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/combine.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Jakub Jelinek 2014-07-08 15:44:13 UTC
Author: jakub
Date: Tue Jul  8 15:43:39 2014
New Revision: 212365

URL: https://gcc.gnu.org/viewcvs?rev=212365&root=gcc&view=rev
Log:
	PR rtl-optimization/61673
	* combine.c (simplify_comparison): Test just mode's sign bit
	in tmode rather than the sign bit and any bits above it.

	* gcc.c-torture/execute/pr61673.c: New test.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.c-torture/execute/pr61673.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/combine.c
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
Comment 4 Jakub Jelinek 2014-07-08 15:55:18 UTC
Fixed.