https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64491
Bug ID: 64491 Summary: warning: loop exit may only be reached after undefined behavior Product: gcc Version: 5.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: stuwph at live dot de gcc -fno-exceptions -Wno-long-long -fno-rtti -ansi -pedantic -DNDEBUG -O3 -DIS_64BIT -DUSE_BSFQ -DUSE_PREFETCH -static -s In function 'void Bitboards::init()': warning: loop exit may only be reached after undefined behavior [-Waggressive-loop-optimizations] for (File f = FILE_A; f <= FILE_H; ++f) ^ note: possible undefined statement is here AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0); ^ /// Bitboards::init() initializes various bitboard tables. It is called at /// startup and relies on global objects to be already zero-initialized. void Bitboards::init() { for (Square s = SQ_A1; s <= SQ_H8; ++s) { SquareBB[s] = 1ULL << s; BSFTable[bsf_index(SquareBB[s])] = s; } for (Bitboard b = 1; b < 256; ++b) MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b); for (File f = FILE_A; f <= FILE_H; ++f) FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB; for (Rank r = RANK_1; r <= RANK_8; ++r) RankBB[r] = r > RANK_1 ? RankBB[r - 1] << 8 : Rank1BB; /// warning here: for (File f = FILE_A; f <= FILE_H; ++f) AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f] : 0); for (Rank r = RANK_1; r < RANK_8; ++r) InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]); ... *** definitely it is a gcc bug, the warning is wrong. My guess is that gcc consider that FileBB[f + 1] could reach out of bound when f == FILE_H, ignoring that out of bound access is guarded by the condition (f < FILE_H) *** solution to let the warning disappear: for (File f = FILE_A; f <= FILE_H; ++f) AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f] : 0);