Bug 34768 - [4.1 Regression] Wrong code with conditional function invocation
Summary: [4.1 Regression] Wrong code with conditional function invocation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.3.0
: P1 normal
Target Milestone: 4.2.3
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
: 28289 (view as bug list)
Depends on:
Blocks: 29382 32139
  Show dependency treegraph
 
Reported: 2008-01-13 13:22 UTC by Richard Biener
Modified: 2008-07-04 16:17 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work: 3.4.6 4.2.3 4.3.0
Known to fail: 4.0.4 4.1.3
Last reconfirmed: 2008-01-13 13:23:55


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2008-01-13 13:22:41 UTC
int x;

void __attribute__((noinline)) foo (void)
{
  x = -x;
}
void __attribute__((const,noinline)) bar (void)
{
}

int __attribute__((noinline))
test (int c)
{
  int tmp = x;
  (c ? foo : bar) ();
  return tmp + x;
}

extern void abort (void);
int main()
{
  x = 1;
  if (test (1) != 0)
    abort ();
  return 0;
}

creates wrong code because the side-effect of the call to foo() is not
accounted for.

Since the merge of tree-ssa already gimplification removes the function call.
Thus, the above fails with -O0.


If you modify the testcase to use the return-value, you see that wrong
alias information is created because we appearantly use the const attribute
for the indirect call.

int x;

int __attribute__((noinline)) foo (void)
{
  x = -x;
  return 0;
}
int __attribute__((const,noinline)) bar (void)
{
  return 0;
}

int __attribute__((noinline))
test (int c)
{
  int tmp = x;
  int res = (c ? foo : bar) ();
  return tmp + x + res;
}

extern void abort (void);
int main()
{
  x = 1;
  if (test (1) != 0)
    abort ();
  return 0;
}

alias produced is:

test (c)
{
  int res;
  int tmp;
  int D.1207;
  int x.3;
  int D.1205;
  int (*<T240>) (void) iftmp.2;

<bb 2>:
  # VUSE <x_11(D)>
  tmp_2 = x;
  if (c_3(D) != 0)
    goto <bb 4>;
  else
    goto <bb 3>;

<bb 3>:

<bb 4>:
  # iftmp.2_1 = PHI <foo(2), bar(3)>
  res_6 = iftmp.2_1 ();
  # VUSE <x_11(D)>
  x.3_7 = x;
  D.1207_8 = tmp_2 + x.3_7;
  D.1205_9 = D.1207_8 + res_6;
  return D.1205_9;

}

and we happily CSE the load from x.  This testcase requires -O to fail.
Comment 1 Richard Biener 2008-01-13 13:23:55 UTC
Mine.  Probably also causes PR32139.
Comment 2 Richard Biener 2008-01-13 13:25:56 UTC
Um, this is a FE bug.
Comment 3 Richard Biener 2008-01-13 13:50:45 UTC
The bug is in common_pointer_type (), where we merge the function type
qualifiers (TYPE_READONLY is used for 'const' functions):

  else if (TYPE_P (exp) && TYPE_READONLY (exp) && ! TREE_THIS_VOLATILE (exp))
    flags |= ECF_CONST;

volatile is used as 'noreturn'.
Comment 4 Andrew Pinski 2008-01-13 17:49:49 UTC
THis is related to PR 29382 and PR 13519.  And I think this is exactly the same issue as PR 28289.
Comment 5 Richard Biener 2008-01-13 18:17:01 UTC
*** Bug 28289 has been marked as a duplicate of this bug. ***
Comment 6 Richard Biener 2008-01-16 09:45:07 UTC
Subject: Bug 34768

Author: rguenth
Date: Wed Jan 16 09:44:23 2008
New Revision: 131568

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131568
Log:
2008-01-16  Richard Guenther  <rguenther@suse.de>

	PR c/34768
	* c-typeck.c (common_pointer_type): Do not merge inconsistent
	type qualifiers for function types.

	* gcc.c-torture/execute/pr34768-1.c: New testcase.
	* gcc.c-torture/execute/pr34768-2.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-1.c
    trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-typeck.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Richard Biener 2008-01-16 09:47:03 UTC
Fixed on the trunk.
Comment 8 Richard Biener 2008-01-22 14:46:45 UTC
Subject: Bug 34768

Author: rguenth
Date: Tue Jan 22 14:45:56 2008
New Revision: 131723

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131723
Log:
2008-01-22  Richard Guenther  <rguenther@suse.de>

	PR middle-end/34739
	Backport from mainline
	2008-01-16  Richard Guenther  <rguenther@suse.de>

	PR c/34768
	* c-typeck.c (common_pointer_type): Do not merge inconsistent
	type qualifiers for function types.

	2007-11-12  Richard Guenther  <rguenther@suse.de>

	PR middle-end/34070
	* fold-const.c (fold_binary): If testing for non-negative
	operands with tree_expr_nonnegative_warnv_p make sure to
	use op0 which has all (sign) conversions retained.

	2006-10-24  Richard Guenther  <rguenther@suse.de>

	PR middle-end/28796
	* builtins.c (fold_builtin_classify): Use HONOR_INFINITIES
	and HONOR_NANS instead of MODE_HAS_INFINITIES and MODE_HAS_NANS
	for deciding optimizations in consistency with fold-const.c
	(fold_builtin_unordered_cmp): Likewise.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34070-1.c
      - copied unchanged from r130098, trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-1.c
    branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34070-2.c
      - copied unchanged from r130098, trunk/gcc/testsuite/gcc.c-torture/execute/pr34070-2.c
    branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34768-1.c
      - copied unchanged from r131568, trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-1.c
    branches/gcc-4_2-branch/gcc/testsuite/gcc.c-torture/execute/pr34768-2.c
      - copied unchanged from r131568, trunk/gcc/testsuite/gcc.c-torture/execute/pr34768-2.c
    branches/gcc-4_2-branch/gcc/testsuite/gcc.dg/pr28796-1.c
      - copied unchanged from r118001, trunk/gcc/testsuite/gcc.dg/pr28796-1.c
    branches/gcc-4_2-branch/gcc/testsuite/gcc.dg/pr28796-2.c
      - copied unchanged from r118001, trunk/gcc/testsuite/gcc.dg/pr28796-2.c
Modified:
    branches/gcc-4_2-branch/gcc/ChangeLog
    branches/gcc-4_2-branch/gcc/builtins.c
    branches/gcc-4_2-branch/gcc/c-typeck.c
    branches/gcc-4_2-branch/gcc/fold-const.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 9 Richard Biener 2008-01-22 14:51:47 UTC
Fixed on the 4.2 branch.
Comment 10 Joseph S. Myers 2008-07-04 16:17:25 UTC
Closing 4.1 branch.