public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/57199] New: [4.8, 4.9] Bogus warning: iteration NNNN invokes undefined behavior -Waggressive-loop-optimizations
@ 2013-05-07 22:09 ppluzhnikov at google dot com
  2013-05-07 23:01 ` [Bug c++/57199] [4.8/4.9 Regression] " paolo.carlini at oracle dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: ppluzhnikov at google dot com @ 2013-05-07 22:09 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57199

             Bug #: 57199
           Summary: [4.8, 4.9] Bogus warning: iteration NNNN invokes
                    undefined behavior -Waggressive-loop-optimizations
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ppluzhnikov@google.com


Google ref: b/8856275

Test case:



typedef decltype(sizeof(0)) size_t;

struct Foo {
  ~Foo();     // comment out -> problem disappears
  int x[20];
};

template <typename T, int N>
struct InlinedVector {
  inline InlinedVector() : size_(0) { }
  inline size_t size() const {
    return size_;
  }
  T* mutable_array();
  void resize(size_t n) {
    size_t s = size();
    DestroyElements(mutable_array() + n, s - n);
  }
  static void DestroyElements(T* ptr, size_t num) {
    for (size_t i = 0; i < num; i++) {
      ptr[i].~T();
    }
  }

  size_t size_;
};

struct CP {
  void Add();
  InlinedVector<Foo,1> foo_;
};

void CP::Add() {
  foo_.resize(foo_.size() + 1);
}



Using trunk r198689:

g++ -std=c++11 -O2 -c tt.cc
tt.cc: In member function ‘void CP::Add()’:
tt.cc:21:7: warning: iteration 230584300921369396ul invokes undefined behavior
[-Waggressive-loop-optimizations]
       ptr[i].~T();
       ^
tt.cc:20:5: note: containing loop
     for (size_t i = 0; i < num; i++) {
     ^
>From gcc-bugs-return-421782-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue May 07 22:17:31 2013
Return-Path: <gcc-bugs-return-421782-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 32442 invoked by alias); 7 May 2013 22:17:30 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 32419 invoked by uid 48); 7 May 2013 22:17:27 -0000
From: "ppluzhnikov at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/57200] New: [4.8/4.9] aggressive-loop-optimizations notes appear even when warning itself is suppressed
Date: Tue, 07 May 2013 22:17:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: new
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c
X-Bugzilla-Keywords:
X-Bugzilla-Severity: normal
X-Bugzilla-Who: ppluzhnikov at google dot com
X-Bugzilla-Status: UNCONFIRMED
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Changed-Fields:
Message-ID: <bug-57200-4@http.gcc.gnu.org/bugzilla/>
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
X-SW-Source: 2013-05/txt/msg00455.txt.bz2
Content-length: 1054


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57200

             Bug #: 57200
           Summary: [4.8/4.9] aggressive-loop-optimizations notes appear
                    even when warning itself is suppressed
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ppluzhnikov@google.com


Test:

int x[10];

void fn ()
{
  int i;

  for (i = 0; i < 100; ++i)
    x[i] = 1;
}


Using trunk at r198689:

gcc -O2 -c t.c 
t.c: In function ‘fn’:
t.c:8:10: warning: iteration 10u invokes undefined behavior
[-Waggressive-loop-optimizations]
     x[i] = 1;
          ^
t.c:7:3: note: containing loop
   for (i = 0; i < 100; ++i)
   ^


Looks good. But this doesn't:

gcc -O2 -c t.c -w
t.c: In function ‘fn’:
t.c:7:3: note: containing loop
   for (i = 0; i < 100; ++i)
   ^

A note suddenly appears from "nowhere" :-(
>From gcc-bugs-return-421783-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue May 07 22:29:09 2013
Return-Path: <gcc-bugs-return-421783-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 4469 invoked by alias); 7 May 2013 22:29:09 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 4369 invoked by uid 48); 7 May 2013 22:28:57 -0000
From: "mpolacek at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/55278] [4.8/4.9 Regression] Botan performance regressions apparently due to LRA
Date: Tue, 07 May 2013 22:29:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: rtl-optimization
X-Bugzilla-Keywords: missed-optimization, ra
X-Bugzilla-Severity: normal
X-Bugzilla-Who: mpolacek at gcc dot gnu.org
X-Bugzilla-Status: NEW
X-Bugzilla-Priority: P2
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: 4.8.1
X-Bugzilla-Changed-Fields: CC
Message-ID: <bug-55278-4-wm1OtsU9Ln@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-55278-4@http.gcc.gnu.org/bugzilla/>
References: <bug-55278-4@http.gcc.gnu.org/bugzilla/>
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
X-SW-Source: 2013-05/txt/msg00456.txt.bz2
Content-length: 3377


http://gcc.gnu.org/bugzilla/show_bug.cgi?idU278

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> 2013-05-07 22:28:56 UTC ---
Reduced by Jakub to this:

unsigned char KASUMI_SBOX_S7[128];
unsigned short KASUMI_SBOX_S9[512];

static inline unsigned short
rotate_left (unsigned short input, unsigned long rot)
{
  return ((input << rot) | (input >> (8 * sizeof (unsigned short) - rot)));
}

static inline unsigned short
reverse_bytes (unsigned short val)
{
  return rotate_left (val, 8);
}

static inline unsigned short
load_be (const unsigned char in[], unsigned long off)
{
  return reverse_bytes (*((in) + off));
}

static inline void
store_be (unsigned short in, unsigned char out[2])
{
  *out = reverse_bytes (in);
}

static inline void
store_be4 (unsigned char out[], unsigned short x0, unsigned short x1,
      unsigned short x2, unsigned short x3)
{
  store_be (x0, out + (0 * sizeof (unsigned short)));
  store_be (x1, out + (1 * sizeof (unsigned short)));
  store_be (x2, out + (2 * sizeof (unsigned short)));
  store_be (x3, out + (3 * sizeof (unsigned short)));
}

unsigned short
FI (unsigned short I, unsigned short K)
{
  unsigned short D9 = (I >> 7);
  unsigned char D7 = (I & 0x7F);
  D9 = KASUMI_SBOX_S9[D9] ^ D7;
  D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F);

  D7 ^= (K >> 9);
  D9 = KASUMI_SBOX_S9[D9 ^ (K & 0x1FF)] ^ D7;
  D7 = KASUMI_SBOX_S7[D7] ^ (D9 & 0x7F);
  return (D7 << 9) | D9;
}

__attribute__((noinline, noclone))
void
encrypt_n (unsigned short **EK, const unsigned char in[], unsigned char out[],
       unsigned long blocks)
{
  unsigned long i, j;
  for (i = 0; i != blocks; ++i)
    {
      unsigned short B0 = load_be (in, 0);
      unsigned short B1 = load_be (in, 1);
      unsigned short B2 = load_be (in, 2);
      unsigned short B3 = load_be (in, 3);
      for (j = 0; j != 8; j += 2)
    {
      const unsigned short *K = &(*EK)[8 * j];
      unsigned short R = B1 ^ (rotate_left (B0, 1) & K[0]);
      unsigned short L = B0 ^ (rotate_left (R, 1) | K[1]);
      L = FI (L ^ K[2], K[3]) ^ R;
      R = FI (R ^ K[4], K[5]) ^ L;
      L = FI (L ^ K[6], K[7]) ^ R;
      R = B2 ^= R;
      L = B3 ^= L;
      R = FI (R ^ K[10], K[11]) ^ L;
      L = FI (L ^ K[12], K[13]) ^ R;
      R = FI (R ^ K[14], K[15]) ^ L;
      R ^= (rotate_left (L, 1) & K[8]);
      L ^= (rotate_left (R, 1) | K[9]);
      B0 ^= L;
      B1 ^= R;
    }
      store_be4 (out, B0, B1, B2, B3);
      in += 8;
      out += 8;
    }
}

unsigned char in[4096], out[4096];

int
main ()
{
  unsigned short EKb[64], *EK = EKb;
  __builtin_memset (EKb, 0, sizeof EKb);
  asm volatile ("" : : : "memory");
  int i;
  for (i = 0; i < 100000; i++)
    encrypt_n (&EK, in, out, 4096 / 8);
  return 0;
}

Using -O3  -finline-functions  -D_REENTRANT -Wno-long-long -W -Wall -fPIC
-fvisibility=hidden, then trunk gcc:
$ time ./a.out
real    0m15.727s
user    0m15.639s
sys    0m0.004s

while with trunk clang:
$ time ./a.out
real    0m7.864s
user    0m7.796s
sys    0m0.005s


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-03-26 20:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-07 22:09 [Bug c++/57199] New: [4.8, 4.9] Bogus warning: iteration NNNN invokes undefined behavior -Waggressive-loop-optimizations ppluzhnikov at google dot com
2013-05-07 23:01 ` [Bug c++/57199] [4.8/4.9 Regression] " paolo.carlini at oracle dot com
2013-05-08  8:58 ` [Bug tree-optimization/57199] " rguenth at gcc dot gnu.org
2013-05-08 12:55 ` ppluzhnikov at google dot com
2013-05-15 13:29 ` rguenth at gcc dot gnu.org
2013-05-20  8:12 ` jakub at gcc dot gnu.org
2013-05-20 14:10 ` ppluzhnikov at google dot com
2013-05-20 14:26 ` jakub at gcc dot gnu.org
2013-05-20 15:19 ` ppluzhnikov at google dot com
2014-03-26 20:39 ` dichlofos-mv at yandex dot ru
2014-03-26 20:56 ` ppluzhnikov at google dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).