Bug 11373 - [tree-ssa] asm generated lhs expressions are discarded
Summary: [tree-ssa] asm generated lhs expressions are discarded
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: rtl-optimization (show other bugs)
Version: tree-ssa
: P2 normal
Target Milestone: tree-ssa
Assignee: Diego Novillo
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2003-06-29 14:32 UTC by marcus
Modified: 2003-08-03 19:29 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: *-*-*
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-07-15 02:04:53


Attachments
testcase for 11373 (122 bytes, text/plain)
2003-06-29 14:33 UTC, marcus
Details

Note You need to log in before you can comment on or make changes to this bug.
Description marcus 2003-06-29 14:32:57 UTC
Attached testcase reduces to f() { return; } instead of generating 
a memory reference: (omit-frame-pointer is not the culprit, breaks with -O2 
too) 
 
gcc -c -O2  -fomit-frame-pointer xx.i ; objdump -d xx.o 
00000000 <f>: 
   0:   c3                      ret 
 
(This is reduced from WINE, TlsSetValue())
Comment 1 marcus 2003-06-29 14:33:45 UTC
Created attachment 4302 [details]
testcase for 11373

compile with -O2
Comment 2 Andrew Pinski 2003-06-29 14:49:47 UTC
confirmed on tree-ssa (20030628). DCE is removing a store which is wrong.
This can reproduced without using inline-asm (at least on powerpc-apple-darwin6.6):
static inline int* k()
{
        return (int*)(0xFFFF0001);
}

void g()
{
        *k() = 0;
}

So somehow DCE is removing *-65535B = 0 or *t_2 = 0 where t is set by a asm.
Comment 3 Andrew Pinski 2003-06-29 14:51:48 UTC
Here is a sample without the inlining:
void h()
{
        int *t;
        __asm__(".byte 0x64\n\tmovl (0x18),%0" : "=r" (t));
        *t = 0;
}

The store is not dead code but it is mark as so.
Comment 4 marcus 2003-06-29 14:55:53 UTC
The 0 used is just a sample, it works as any constant or with function 
argument. 
Comment 5 Andrew Pinski 2003-07-05 15:59:38 UTC
Still happens (20030704).
Comment 6 Andrew Pinski 2003-07-15 01:56:32 UTC
Still happens in 20030715. Now the store is removed by mustalias.
Comment 7 Diego Novillo 2003-07-15 02:04:04 UTC
Subject: Re:  [tree-ssa] asm generated lhs
	expressions are discarded


You need to specify volatile to let the compiler know you're doing
something odd.

Both test cases work if volatile is used together with a patch to
volatile support that I'm going to be committing momentarily.


Diego.

Comment 8 marcus 2003-07-15 05:50:33 UTC
Why is an asm() statement with a pointer as output something special?   Its just like any other function that returns a pointer, or? 
Comment 9 Diego Novillo 2003-07-15 12:19:07 UTC
Subject: Re:  [tree-ssa] asm generated lhs
	expressions are discarded

On Tue, 2003-07-15 at 01:50, marcus at jet dot franken dot de wrote:

> Why is an asm() statement with a pointer as output something
> special?   Its just like any other function that returns a pointer,
> or?
>
Ah, yes, now I see.  I had missed the assignment to 't' inside the asm
statement.  I'll fix that.

The second test case seems borderline to me.  I'm not a language lawyer,
but I don't know what are the rules wrt assigning using random integer
values to a pointer.  We can mark the pointer with the 'may point to
global memory' attribute.


Diego.

Comment 10 marcus 2003-07-16 20:17:07 UTC
My asm() testcase is fixed, Andrews testcase is not. 
Comment 11 Andrew Pinski 2003-07-17 13:54:28 UTC
Assigning to who thought he assigned to himself.
Comment 12 Diego Novillo 2003-07-25 23:51:22 UTC
Subject: Re:  [tree-ssa] asm generated lhs
	expressions are discarded


This patch should fix the problem.  I will commit it in my next round of
patches.


Diego.

	PR optimization/11373
	* tree-ssa-dce.c (stmt_useful_p): Scan operands before checking
	for volatile operands.
	* tree-dfa.c (get_expr_operands): If a nonzero constant is used in
	an INDIRECT_REF expression, mark the statement as having volatile
	operands.
	(may_access_global_mem_p): Return true if the pointer is a nonzero
	constant.

Index: tree-ssa-dce.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-ssa-dce.c,v
retrieving revision 1.1.2.47
diff -d -u -p -r1.1.2.47 tree-ssa-dce.c
--- tree-ssa-dce.c	22 Jul 2003 02:50:15 -0000	1.1.2.47
+++ tree-ssa-dce.c	25 Jul 2003 23:48:44 -0000
@@ -283,12 +283,12 @@ stmt_useful_p (tree stmt)
 	    return true;
     }
 
+  /* Examine all the stores in this statement.  */
+  get_stmt_operands (stmt);
+
   /* If the statement has volatile operands, it needs to be preserved.  */
   if (stmt_ann (stmt)->has_volatile_ops)
     return true;
-
-  /* Examine all the stores in this statement.  */
-  get_stmt_operands (stmt);
 
   ops = def_ops (stmt);
   for (i = 0; ops && i < VARRAY_ACTIVE_SIZE (ops); i++)
Index: tree-dfa.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/tree-dfa.c,v
retrieving revision 1.1.4.135
diff -d -u -p -r1.1.4.135 tree-dfa.c
--- tree-dfa.c	25 Jul 2003 03:13:07 -0000	1.1.4.135
+++ tree-dfa.c	25 Jul 2003 23:48:44 -0000
@@ -377,6 +377,15 @@ get_expr_operands (tree stmt, tree *expr
 	  add_stmt_operand (&TREE_OPERAND (ptr, 0), stmt, flags, prev_vops);
 	  return;
 	}
+      else if (TREE_CONSTANT (ptr) && !integer_zerop (ptr))
+	{
+	  /* If a constant is used as a pointer, we can't generate a real
+	     operand for it but we mark the statement volatile to prevent
+	     optimizations from messing things up.  */
+	  stmt_ann (stmt)->has_volatile_ops = true;
+	  return;
+	}
+
 
       /* Add a USE operand for the base pointer.  */
       get_expr_operands (stmt, &TREE_OPERAND (expr, 0), opf_none, prev_vops);
@@ -2103,6 +2112,10 @@ may_access_global_mem_p (tree expr)
 
   /* Call expressions that return pointers may point to global memory.  */
   if (TREE_CODE (expr) == CALL_EXPR)
+    return true;
+
+  /* A non-NULL constant used as a pointer points to global memory.  */
+  if (TREE_CONSTANT (expr) && !integer_zerop (expr))
     return true;
 
   /* Recursively check the expression's operands.  */


Comment 13 GCC Commits 2003-07-31 16:33:30 UTC
Subject: Bug 11373

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	tree-ssa-20020619-branch
Changes by:	dnovillo@gcc.gnu.org	2003-07-31 16:33:27

Modified files:
	gcc            : ChangeLog.tree-ssa Makefile.in builtins.c 
	                 expr.h tree-dfa.c tree-flow.h tree-ssa-ccp.c 
	                 tree-ssa-dce.c tree-ssa-dom.c tree.h 

Log message:
	2003-07-31  Diego Novillo  <dnovillo@redhat.com>
	
	* Makefile.in (tree-ssa-ccp.o): Depend on $(EXPR_H).
	* builtins.c (c_strlen): Remove static declaration.
	(simplify_builtin_fputs): Remove static declaration.
	(simplify_builtin_sprintf): New local function.
	(expand_builtin_sprintf): Remove by surrounding with #if 0.
	(expand_builtin): Add BUILT_IN_SPRINTF to the list of built-ins
	handed over to simplify_builtin.
	(validate_arglist): Do not allow arguments with TREE_SIDE_EFFECTS.
	(simplify_builtin_fputs): Add new argument KNOWN_LEN.  If it's set,
	use it instead of trying to compute the length of the string.
	Update all callers.
	* expr.h (simplify_builtin_fputs): Declare.
	* tree-flow.h (fold_stmt): Change argument type to tree *.  Update
	all users.
	* tree-ssa-ccp.c: Include expr.h.
	(replace_uses_in): If the statement makes a call to some selected
	built-ins, mark it for folding.
	(get_strlen): New local function.
	(ccp_fold_builtin): New local function.
	(fold_stmt): Call it.
	(set_rhs): Fix if-else-if chaining.  Handle cases where the whole
	statement needs to be replaced.
	* tree.h (c_strlen): Declare.
	
	2003-07-31  Diego Novillo  <dnovillo@redhat.com>
	
	Fix PR optimization/11373
	* tree-ssa-dce.c (stmt_useful_p): Get statement operands before
	checking for volatile operands.
	* tree-dfa.c (get_expr_operands): If a constant is dereferenced as a
	pointer, mark the statement as having volatile operands.
	(may_access_global_mem_p): If a non-NULL constant is used as a
	pointer, consider it as pointing to global memory.
	* tree-ssa-dom.c (optimize_stmt): Set addr_expr_propagated_p when
	propagating pointers that are integer constants.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.tree-ssa.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.544&r2=1.1.2.545
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/Makefile.in.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.903.2.104&r2=1.903.2.105
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/builtins.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.152.2.27&r2=1.152.2.28
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/expr.h.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.117.2.15&r2=1.117.2.16
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-dfa.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.4.137&r2=1.1.4.138
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-flow.h.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.4.97&r2=1.1.4.98
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-ccp.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.80&r2=1.1.2.81
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-dce.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.48&r2=1.1.2.49
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-dom.c.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.1.2.14&r2=1.1.2.15
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree.h.diff?cvsroot=gcc&only_with_tag=tree-ssa-20020619-branch&r1=1.342.2.85&r2=1.342.2.86

Comment 14 Andrew Pinski 2003-08-03 19:29:58 UTC
These have been fixed now in the tree-ssa branch.