Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050205155549

Diff

Diff from 1.20 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/ccom/optim.c

Annotated File View

ragge
1.20
1 /*      $Id: optim.c,v 1.20 2005/02/05 15:55:49 ragge Exp $     */
ragge
1.10
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.1
35
36 # include "pass1.h"
37
38 # define SWAP(p,q) {sp=p; p=q; q=sp;}
ragge
1.3
39 # define RCON(p) (p->n_right->n_op==ICON)
40 # define RO(p) p->n_right->n_op
41 # define RV(p) p->n_right->n_lval
42 # define LCON(p) (p->n_left->n_op==ICON)
43 # define LO(p) p->n_left->n_op
44 # define LV(p) p->n_left->n_lval
ragge
1.1
45
ragge
1.10
46 static int nncon(NODE *);
ragge
1.1
47
48 int oflag = 0;
49
ragge
1.2
50 /*
51  * fortran function arguments
52  */
ragge
1.9
53 static NODE *
ragge
1.2
54 fortarg(NODE *p)
55 {
ragge
1.3
56         ifp->n_op == CM ){
57                 p->n_left = fortargp->n_left );
58                 p->n_right = fortargp->n_right );
ragge
1.1
59                 return(p);
ragge
1.2
60         }
ragge
1.1
61
ragge
1.3
62         whileISPTR(p->n_type) ){
ragge
1.16
63                 p = buildtreeUMULpNIL );
ragge
1.2
64         }
ragge
1.1
65         returnoptim(p) );
ragge
1.2
66 }
ragge
1.1
67
68         /* mapping relationals when the sides are reversed */
69 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
70
71 /*
72  * local optimizations, most of which are probably
73  * machine independent
74  */
ragge
1.1
75 NODE *
ragge
1.2
76 optim(NODE *p)
77 {
78         int oty;
ragge
1.8
79         NODE *sp, *q;
ragge
1.1
80         int i;
81         TWORD t;
82
ragge
1.3
83         if( (t=BTYPE(p->n_type))==ENUMTY || t==MOETY ) econvert(p);
ragge
1.1
84         ifoflag ) return(p);
ragge
1.12
85         ty = coptype(o = p->n_op);
ragge
1.1
86         ifty == LTYPE ) return(p);
87
ragge
1.3
88         ifty == BITYPE ) p->n_right = optim(p->n_right);
89         p->n_left = optim(p->n_left);
ragge
1.1
90
91         /* collect constants */
92
93         switch(o){
94
95         case SCONV:
96         case PCONV:
97                 returnclocal(p) );
98
99         case FORTCALL:
ragge
1.3
100                 p->n_right = fortargp->n_right );
ragge
1.1
101                 break;
102
ragge
1.15
103         case ADDROF:
ragge
1.20
104                 if (LO(p) == TEMP)
105                         return p;
ragge
1.10
106                 ifLO(p) != NAME ) cerror"& error" );
107
108                 if( !andable(p->n_left) ) return(p);
ragge
1.1
109
110                 LO(p) = ICON;
111
112                 setuleft:
113                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
114                 p->n_left->n_type = p->n_type;
ragge
1.7
115                 p->n_left->n_df = p->n_df;
ragge
1.5
116                 p->n_left->n_sue = p->n_sue;
ragge
1.8
117                 q = p->n_left;
118                 nfree(p);
119                 return q;
ragge
1.1
120
ragge
1.16
121         case UMUL:
ragge
1.1
122                 ifLO(p) != ICON ) break;
123                 LO(p) = NAME;
124                 goto setuleft;
125
126         case MINUS:
ragge
1.19
127                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
128                         /* link-time constants, but both are the same */
129                         /* solve it now by forgetting the symbols */
130                         p->n_left->n_sp = p->n_right->n_sp = NULL;
131                 }
ragge
1.3
132                 if( !nncon(p->n_right) ) break;
ragge
1.1
133                 RV(p) = -RV(p);
ragge
1.3
134                 o = p->n_op = PLUS;
ragge
1.1
135
136         case MUL:
137         case PLUS:
138         case AND:
139         case OR:
140         case ER:
141                 /* commutative ops; for now, just collect constants */
142                 /* someday, do it right */
ragge
1.14
143                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
144                         SWAPp->n_leftp->n_right );
ragge
1.1
145                 /* make ops tower to the left, not the right */
146                 ifRO(p) == o ){
147                         NODE *t1, *t2, *t3;
ragge
1.3
148                         t1 = p->n_left;
149                         sp = p->n_right;
150                         t2 = sp->n_left;
151                         t3 = sp->n_right;
ragge
1.1
152                         /* now, put together again */
ragge
1.3
153                         p->n_left = sp;
154                         sp->n_left = t1;
155                         sp->n_right = t2;
156                         p->n_right = t3;
ragge
1.1
157                         }
ragge
1.3
158                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
159                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
160                         zapleft:
ragge
1.8
161
162                         q = p->n_left->n_left;
163                         nfree(p->n_left->n_right);
164                         nfree(p->n_left);
165                         p->n_left = q;
ragge
1.1
166                 }
ragge
1.14
167                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
168                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
169                         goto zapleft;
170                         }
ragge
1.10
171                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
172                         zapright:
ragge
1.8
173                         nfree(p->n_right);
ragge
1.14
174                         q = makety(p->n_leftp->n_typep->n_qual,
175                             p->n_dfp->n_sue);
ragge
1.8
176                         nfree(p);
177                         return clocal(q);
ragge
1.10
178                         }
ragge
1.1
179
ragge
1.10
180                 /* change muls to shifts */
ragge
1.1
181
ragge
1.10
182                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
183                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
184                                 goto zapright;
185                                 }
ragge
1.10
186                         o = p->n_op = LS;
187                         p->n_right->n_type = INT;
188                         p->n_right->n_df = NULL;
189                         RV(p) = i;
ragge
1.1
190                         }
191
192                 /* change +'s of negative consts back to - */
ragge
1.3
193                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
194                         RV(p) = -RV(p);
ragge
1.3
195                         o = p->n_op = MINUS;
ragge
1.1
196                         }
197                 break;
198
199         case DIV:
ragge
1.19
200                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
201                         goto zapright;
202                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
203                         goto zapright;
ragge
1.1
204                 break;
205
206         case EQ:
207         case NE:
208         case LT:
209         case LE:
210         case GT:
211         case GE:
212         case ULT:
213         case ULE:
214         case UGT:
215         case UGE:
216                 if( !LCON(p) ) break;
217
218                 /* exchange operands */
219
ragge
1.3
220                 sp = p->n_left;
221                 p->n_left = p->n_right;
222                 p->n_right = sp;
223                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
224                 break;
225
226                 }
227
228         return(p);
229         }
230
ragge
1.2
231 int
232 ispow2(CONSZ c)
233 {
234         int i;
ragge
1.1
235         ifc <= 0 || (c&(c-1)) ) return(-1);
236         fori=0c>1; ++ic >>= 1;
237         return(i);
ragge
1.2
238 }
ragge
1.10
239
240 int
241 nnconp ) NODE *p; {
242         /* is p a constant without a name */
243         returnp->n_op == ICON && p->n_sp == NULL );
244         }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-08-28 09:05 +0200