From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 49EA93858028; Mon, 12 Apr 2021 17:37:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 49EA93858028 From: "msebor at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/100038] -Warray-bound triggers false positives Date: Mon, 12 Apr 2021 17:37:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: msebor at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: 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: bug_status see_also cc 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, 12 Apr 2021 17:37:58 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100038 Martin Sebor changed: What |Removed |Added ---------------------------------------------------------------------------- Status|WAITING |NEW See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=3D98465 CC| |msebor at gcc dot gnu.org --- Comment #7 from Martin Sebor --- Confirmed with attachment 50560 and the output/IL below. The problem is ca= used by the test (presumably for self-insertion) in SmallVectorImpl::insert(iterator, const T&): const T *EltPtr =3D &Elt; if (I <=3D EltPtr && EltPtr < this->end()) ++EltPtr; I don't see anything in the IL to avoid it without compromising it at the s= ame time. We suppressed a similar false positive in std::string::insert() in pr98465. It was done by keeping the equivalent test from being inlined bec= ause the usual suppression mechanism, #pragma GCC diagnostic, doesn't work relia= bly with inlining (something that will hopefully be fixed in GCC 12). Another alternative to consider is inserting an optimization barrier after the block above: asm volatile("": : :"memory"); Another (future) alternative is to annotate the SmallVector::BeginX with so= me new attribute telling the optimizer it doesn't alias unrelated objects (like val). This doesn't exist but it would help not just avoid warnings but also emit more optimal code. (As an aside, the pointer test above is strictly unspecified because it compares the addresses of unrelated objects, It should convert the pointer= s to integers first but that wouldn't prevent the warning.) In file included from mwe_smallvector.cpp:2: SmallVector.h: In function =E2=80=98int main()=E2=80=99: SmallVector.h:537:7: warning: array subscript 1 is outside array bounds of = =E2=80=98int [1]=E2=80=99 [-Warray-bounds] mwe_smallvector.cpp:12:11: note: while referencing =E2=80=98=E2= =80=99 In file included from mwe_smallvector.cpp:2: SmallVector.h:566:7: warning: array subscript 1 is outside array bounds of = =E2=80=98int [1]=E2=80=99 [-Warray-bounds] mwe_smallvector.cpp:13:6: note: while referencing =E2=80=98val=E2=80=99 ... int main () { ... int D.55243; ... _21 =3D VS.D.54770.BeginX; _1 =3D _21 + 4; ... [local count: 32183167]: _52 =3D MEM[(struct SmallVectorTemplateCommon *)&VS].D.54770.BeginX; I_54 =3D _52 + 4; [local count: 97524748]: # I_69 =3D PHI <_1(8), I_54(10)> ... [local count: 97524748]: _115 =3D (long unsigned int) _111; _116 =3D -_115; _117 =3D _66 + _116; _70 =3D MEM[(const struct SmallVectorBase *)&VS].Size; _71 =3D _70 + 1; MEM[(struct SmallVectorBase *)&VS].Size =3D _71; if (&D.55243 >=3D I_69) goto ; [50.00%] else goto ; [50.00%] [local count: 48762374]: _72 =3D MEM[(struct SmallVectorTemplateCommon *)&VS].D.54770.BeginX; _73 =3D (long unsigned int) _71; _74 =3D _73 * 4; _75 =3D _72 + _74; if (&D.55243 < _75) goto ; [50.00%] else goto ; [50.00%] [local count: 24381187]: _50 =3D MEM[(type &)&D.55243 + 4]; <<< -Warray-bounds=