From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 96352 invoked by alias); 19 May 2015 15:12:21 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 81212 invoked by uid 48); 19 May 2015 15:12:15 -0000 From: "gil.hur at sf dot snu.ac.kr" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/65752] Too strong optimizations int -> pointer casts Date: Tue, 19 May 2015 15:12:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 4.9.2 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gil.hur at sf dot snu.ac.kr X-Bugzilla-Status: UNCONFIRMED 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: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-05/txt/msg01524.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65752 --- Comment #22 from Chung-Kil Hur --- (In reply to Chung-Kil Hur from comment #21) > (In reply to Marek Polacek from comment #20) > > (In reply to Chung-Kil Hur from comment #19) > > > (In reply to rguenther@suse.de from comment #18) > > > > On Tue, 19 May 2015, gil.hur at sf dot snu.ac.kr wrote: > > > > > > > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65752 > > > > > > > > > > --- Comment #17 from Chung-Kil Hur --- > > > > > Hi Richard, > > > > > > > > > > I modified the example further. > > > > > > > > > > #include > > > > > > > > > > int main() { > > > > > int x = 0; > > > > > uintptr_t xp = (uintptr_t) &x; > > > > > uintptr_t i, j; > > > > > > > > > > for (i = 0; i < xp; i++) { } > > > > > j = i; > > > > > /* The following "if" statement is never executed because j == xp */ > > > > > if (j != xp) { > > > > > printf("hello\n"); > > > > > j = xp; > > > > > } > > > > > > > > Here j is always xp and thus ... > > > > > > > > > > Why is "j" always "xp"? > > > Since "hello" is not printed, "j = xp;" is not executed. > > > > Because that "if (j != xp)" guarantees it. > > OK. here is another modification. > > #include > > int main() { > int x = 0; > uintptr_t xp = (uintptr_t) &x; > uintptr_t i, j; > > for (i = 0; i < xp; i++) { } > j = i; > > *(int*)j = 15; > > /* The following "if" statement is never executed because j == xp */ > if (j != xp) { > printf("hello\n"); > j = xp; > } > > *(int*)((xp+i)-j) = 15; > > printf("%d\n", x); > } > > This program just prints "0". > > So we know that "*(int*)j = 15;" is not executed and thus "j == xp" is not > true. > > Then, can the following statement change "j" even if the printf is not > executed? > > if (j != xp) { > printf("hello\n"); > j = xp; > } > > If not, how can "j == xp" suddenly hold? One more thing. If you remove the if-statement, then it prints "15" with GCC -O2. Since "hello" is not printed, I think the if-statement is the same as no-op. Thus, removing the if-statement should not change the behavior of the program according to ISO C11. But, they print different values. Can you explain this?