Index: osprey/be/com/wn_lower.cxx
===================================================================
--- osprey/be/com/wn_lower.cxx	(revision 3320)
+++ osprey/be/com/wn_lower.cxx	(working copy)
@@ -2168,6 +2168,8 @@
       WN	*rz, *iz;
 
       lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+      WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+      WN_kid0(tree)=NULL;
       *realpart = WN_Neg( type, rz);
       *imagpart = WN_Neg( type, iz);
 
@@ -2180,11 +2182,14 @@
      /*
       *    z + w = (R(z) + R(w)) + (I(z) + I(w))i
       */
-
       {
 	TYPE_ID ltype = WN_rtype(WN_kid0(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
-          lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+        {
+	  lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+          WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid0(tree)=NULL;
+	}
 	else
 	{
 	  rz = WN_kid0(tree);
@@ -2194,7 +2199,11 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid1(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
-          lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);
+        {  
+	  lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);
+	  WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid1(tree)=NULL;
+        }
 	else
 	{
 	  rw = WN_kid1(tree);
@@ -2227,11 +2236,14 @@
       *    z - w = (R(z) - R(w)) + (I(z) - I(w))i
       */
       WN	*rz, *rw, *iz, *iw;
-
       {
 	TYPE_ID ltype = WN_rtype(WN_kid0(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
+	{
           lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+	  WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid0(tree)=NULL;
+	}
 	else
 	{
 	  rz = WN_kid0(tree);
@@ -2241,14 +2253,17 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid1(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
+	{
           lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);
+	  WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid1(tree)=NULL;
+	}
 	else
 	{
 	  rw = WN_kid1(tree);
 	  iw = NULL;
 	}
       }
- 
       if (rz != NULL && rw != NULL)
         *realpart = WN_Sub( type, rz, rw);
       else if (rz != NULL)
@@ -2280,7 +2295,11 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid0(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
+	{
           lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+	  WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid0(tree)=NULL;
+	}
 	else
 	{
 	  rz = WN_kid0(tree);
@@ -2290,7 +2309,11 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid1(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
+	{
           lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);
+          WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid1(tree)=NULL;
+ 	}
 	else
 	{
 	  rw = WN_kid1(tree);
@@ -2362,7 +2385,8 @@
       TYPE_ID	desc = WN_desc(tree);
 
       lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
- 
+      WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered 
+      WN_kid0(tree)=NULL;
       *realpart = WN_Cvt( type, Mtype_complex_to_real(desc), rz);
       *imagpart = WN_Cvt( type, Mtype_complex_to_real(desc), iz);
     }
@@ -2400,8 +2424,10 @@
 		   WN_Sqrt(desc, WN_kid0(tree)));
 
       lower_complex_expr(block, div, actions, realpart, imagpart);
+      WN_Delete( div); //fix bug591, delete tree after it has been lowered
+      div=NULL;
 
-      WN_Delete(tree);
+      //WN_Delete(tree);
     }
     break;
 
@@ -2413,10 +2439,12 @@
   case OPR_PAREN:
     {
       lower_complex_expr(block, WN_kid0(tree), actions, realpart, imagpart);
+      WN_Delete( WN_kid0(tree));//fix bug591, delete tree after it has been lowered
+      WN_kid0(tree)=NULL;
 
       *realpart = WN_Paren(type, *realpart);
       *imagpart = WN_Paren(type, *imagpart);
-      WN_Delete(tree);
+      //WN_Delete(tree);
     }
     break;
 
@@ -2440,7 +2468,8 @@
 	WN	*rz, *iz; 
     
 	lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
-      
+ 	WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered     
+	WN_kid0(tree)=NULL;
 	rzN = Make_Leaf(block, rz, type);
 	izN = Make_Leaf(block, iz, type);
       }
@@ -2589,7 +2618,7 @@
 	*realpart = WN_LdidPreg(type, realpartN);
 	*imagpart = WN_LdidPreg(type, imagpartN);
       }
-      WN_Delete(tree);
+      //WN_Delete(tree);
     }
     break;
 
@@ -2605,7 +2634,11 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid0(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
+        {
           lower_complex_expr(block, WN_kid0(tree), actions, &rz, &iz);
+          WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid0(tree)=NULL;
+        }
 	else
 	{
 	  rz = WN_kid0(tree);
@@ -2615,14 +2648,18 @@
       {
 	TYPE_ID ltype = WN_rtype(WN_kid1(tree));
 	if (ltype == MTYPE_V16C8 || MTYPE_is_complex(ltype))
-          lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);
+        {
+          lower_complex_expr(block, WN_kid1(tree), actions, &rw, &iw);	  
+          WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+          WN_kid1(tree)=NULL;
+        }
 	else
 	{
 	  rw = WN_kid1(tree);
 	  iw = NULL;
 	}
       }
-  
+      
       rzN = Make_Leaf(block, rz, type);
       if (iz != NULL)
 	izN = Make_Leaf(block, iz, type);
@@ -2817,7 +2854,7 @@
 	*realpart = WN_LdidPreg(type, realpartN);
 	*imagpart = WN_LdidPreg(type, imagpartN);
       }
-      WN_Delete(tree);
+      //WN_Delete(tree);
     }
     break;
 
@@ -2844,10 +2881,12 @@
 
 	  lower_complex_expr(block, WN_actual(tree, 0), actions,
 			     realpart, &iz);
+ 	  WN_Delete( WN_actual(tree, 0)); //fix bug591, delete tree after it has been lowered
+	  WN_actual(tree, 0)==NULL;
 
 	  *imagpart = WN_Neg(type, iz);
 
-	  WN_Delete(tree);
+	  //WN_Delete(tree);
 	}
         break;
       case INTRN_V16C8MPY_ADDSUB:
@@ -2893,8 +2932,11 @@
        cond = Make_Leaf(block, WN_kid0(tree), WN_rtype(WN_kid0(tree)));
        
        lower_complex_expr(block, WN_kid1(tree), actions, &r1, &i1);
+       WN_Delete( WN_kid1(tree));//fix bug591, delete tree after it has been lowered
+       WN_kid1(tree)==NULL;
        lower_complex_expr(block, WN_kid2(tree), actions, &r2, &i2);
-       
+       WN_Delete( WN_kid2(tree));//fix bug591, delete tree after it has been lowered
+       WN_kid2(tree)==NULL;
        *realpart = WN_Select(type, Load_Leaf(cond), r1, r2);
        *imagpart = WN_Select(type, Load_Leaf(cond), i1, i2);
     }
@@ -2902,14 +2944,20 @@
 
   case OPR_PARM:
     lower_complex_expr(block, WN_kid0(tree), actions, realpart, imagpart);
+    WN_Delete( WN_kid0(tree));//fix bug591, delete tree after it has been lowered
+    WN_kid0(tree)==NULL;
     break;
   case OPR_FIRSTPART:
     lower_complex_expr(block, WN_kid0(tree), actions, realpart, imagpart);
+    WN_Delete( WN_kid0(tree));//fix bug591, delete tree after it has been lowered
+    WN_kid0(tree)==NULL;
     //WN_Delete(*imagpart);
     *imagpart = NULL;
     break;
   case OPR_SECONDPART:
     lower_complex_expr(block, WN_kid0(tree), actions, realpart, imagpart);
+    WN_Delete( WN_kid0(tree));//fix bug591, delete tree after it has been lowered
+    WN_kid0(tree)==NULL;
     //WN_Delete(*realpart);
     *realpart = *imagpart;
     *imagpart = NULL;
@@ -6338,6 +6386,8 @@
       {
 #endif
         lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);
+        WN_Delete(WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+        WN_kid0(tree)==NULL;
 	if (OPR_REALPART == WN_operator(tree)){ 
           WN_Delete(tree);
           tree = lower_expr(block, realexp, actions);
@@ -6393,8 +6443,11 @@
       TYPE_ID	realTY = Mtype_complex_to_real( WN_desc(tree));
 
       lower_complex_expr(block, WN_kid0(tree), actions, &rx, &ix);
+      WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+      WN_kid0(tree)==NULL;
       lower_complex_expr(block, WN_kid1(tree), actions, &ry, &iy);
- 
+      WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+      WN_kid1(tree)==NULL;
       tree = WN_LAND(WN_EQ(realTY, rx, ry),
 		     WN_EQ(realTY, ix, iy));
 
@@ -6414,8 +6467,11 @@
       TYPE_ID	realTY = Mtype_complex_to_real( WN_desc(tree));
 
       lower_complex_expr(block, WN_kid0(tree), actions, &rx, &ix);
+      WN_Delete(WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+      WN_kid0(tree)==NULL;
       lower_complex_expr(block, WN_kid1(tree), actions, &ry, &iy);
- 
+      WN_Delete( WN_kid1(tree)); //fix bug591, delete tree after it has been lowered
+      WN_kid1(tree)==NULL;
       tree = WN_LNOT(WN_LAND(WN_EQ(realTY, rx, ry),
 			     WN_EQ(realTY, ix, iy)));
 
@@ -7685,7 +7741,8 @@
 	WN	*realexp, *imagexp;
 
 	lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);
-	
+        WN_Delete(WN_kid0(tree));  //fix bug591, delete tree after it has been lowered
+	WN_kid0(tree)=NULL;
 	realexpN = Make_Leaf(block, realexp, realTY);
 	imagexpN = Make_Leaf(block, imagexp, realTY);
       }
@@ -8035,7 +8092,6 @@
       TYPE_ID	realTY;
 
       realTY =	Mtype_complex_to_real( WN_desc(tree));
-      lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);
       // if realTY != WN_rtype(WN_kid0(tree)) a C4 is packed into a F8
       // it is a special case ignored here
       if (imagexp == NULL && realTY == WN_rtype(WN_kid0(tree)))
@@ -8045,6 +8101,7 @@
 			realexp, imagexp);
       }
 
+
 #ifdef TARG_X8664 // MTYPE_C4 is returned in one SSE register
       if (WN_rtype(WN_kid0(tree)) == MTYPE_F8) {
 	// the kid is xmm0
@@ -8055,6 +8112,10 @@
       }
       else if (WN_desc(tree) == MTYPE_C4 && WN_st(tree) == Float_Preg && 
 	       WN_load_offset(tree) == First_Float_Preg_Return_Offset) {
+	
+        lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);
+        WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+        WN_kid0(tree)=NULL;
 	// the store target is xmm0
 	ST *c4temp_st = Gen_Temp_Symbol(MTYPE_To_TY(MTYPE_F8), ".c4");
 
@@ -8084,8 +8145,6 @@
 	stid = WN_Stid(WN_rtype(imagexp), 4, c4temp_st, realTY, imagexp_copy);
         WN_Set_Linenum (stid, WN_Get_Linenum(tree));
         WN_INSERT_BlockLast(block, stid);
-
-        WN_Delete(WN_kid0(tree));
 	WN_kid0(tree) = WN_Ldid(MTYPE_F8, 0, c4temp_st, MTYPE_To_TY(MTYPE_F8));
 	WN_set_desc(tree, MTYPE_F8);
 	return tree;
@@ -8103,7 +8162,10 @@
 		   PU_cxx_lang(Get_Current_PU()) 
 #endif /* defined(BUILD_OS_DARWIN) */
 		   ) ){
-	/* For C/C++ under -m32, if the return value is type
+       	lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);
+	WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+        WN_kid0(tree)=NULL;
+		/* For C/C++ under -m32, if the return value is type
 	   "__complex__ float", the real part will be stored at %eax, and
 	   the imag part will be stored at %edx.  (bug#2842)
 	 */
@@ -8139,8 +8201,6 @@
 	stid = WN_Stid( WN_rtype(imagexp), 4, c4temp_st, realTY, imagexp_copy );
         WN_Set_Linenum (stid, WN_Get_Linenum(tree));
         WN_INSERT_BlockLast(block, stid);
-
-        WN_Delete(WN_kid0(tree));
 	WN_Delete( tree );
 
 	ldid = WN_Ldid( mtype, 4, c4temp_st, MTYPE_To_TY(mtype) );
@@ -8156,12 +8216,15 @@
       * load the temporary values into a preg before the store (pv314583)
       * as the store may interfere with the expression.
       */
-      {
+      { 
+   
         WN_OFFSET	offset = WN_store_offset(tree);
         PREG_NUM	realexpN, imagexpN;
         WN		*wn;
 	WN *realexp_copy,*imagexp_copy;
-
+        lower_complex_expr(block, WN_kid0(tree), actions, &realexp, &imagexp);           
+	WN_Delete( WN_kid0(tree)); //fix bug591, delete tree after it has been lowered
+        WN_kid0(tree)=NULL;
 	if (WN_operator(realexp) == OPR_CONST) {
 	   realexp_copy = realexp;
 	} else {
@@ -8218,7 +8281,6 @@
         imagexp = lower_store(block, wn, actions);
 
         lower_complex_maps(tree, realexp, imagexp, actions);
-
         WN_Delete(tree);
 
         return imagexp;
@@ -8462,7 +8524,8 @@
     WN	*realexp, *imagexp, *eval;
 
     lower_complex_expr(block, child, actions, &realexp, &imagexp);
-
+    WN_Delete(child); //fix bug591, delete tree after it has been lowered
+    child=NULL;
     realexp = lower_expr(block, realexp, actions);
     eval = WN_CreateEval(realexp);
     WN_Set_Linenum(eval, current_srcpos);  // Bug 1268
@@ -10429,6 +10492,8 @@
 //    }
 //#endif
     lower_complex_expr(block, wn, actions, realpart, imagpart);
+    WN_Delete(wn); //fix bug591, delete tree after it has been lowered
+    wn=NULL;
   }
 }
 
