Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040621081946

Diff

Diff from 1.19 to:

Annotations

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

Annotated File View

ragge
1.19
1 /*      $Id: optim.c,v 1.19 2004/06/21 08:19:47 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.10
104                 ifLO(p) != NAME ) cerror"& error" );
105
106                 if( !andable(p->n_left) ) return(p);
ragge
1.1
107
108                 LO(p) = ICON;
109
110                 setuleft:
111                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
112                 p->n_left->n_type = p->n_type;
ragge
1.7
113                 p->n_left->n_df = p->n_df;
ragge
1.5
114                 p->n_left->n_sue = p->n_sue;
ragge
1.8
115                 q = p->n_left;
116                 nfree(p);
117                 return q;
ragge
1.1
118
ragge
1.16
119         case UMUL:
ragge
1.1
120                 ifLO(p) != ICON ) break;
121                 LO(p) = NAME;
122                 goto setuleft;
123
124         case MINUS:
ragge
1.19
125                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
126                         /* link-time constants, but both are the same */
127                         /* solve it now by forgetting the symbols */
128                         p->n_left->n_sp = p->n_right->n_sp = NULL;
129                 }
ragge
1.3
130                 if( !nncon(p->n_right) ) break;
ragge
1.1
131                 RV(p) = -RV(p);
ragge
1.3
132                 o = p->n_op = PLUS;
ragge
1.1
133
134         case MUL:
135         case PLUS:
136         case AND:
137         case OR:
138         case ER:
139                 /* commutative ops; for now, just collect constants */
140                 /* someday, do it right */
ragge
1.14
141                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
142                         SWAPp->n_leftp->n_right );
ragge
1.1
143                 /* make ops tower to the left, not the right */
144                 ifRO(p) == o ){
145                         NODE *t1, *t2, *t3;
ragge
1.3
146                         t1 = p->n_left;
147                         sp = p->n_right;
148                         t2 = sp->n_left;
149                         t3 = sp->n_right;
ragge
1.1
150                         /* now, put together again */
ragge
1.3
151                         p->n_left = sp;
152                         sp->n_left = t1;
153                         sp->n_right = t2;
154                         p->n_right = t3;
ragge
1.1
155                         }
ragge
1.3
156                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
157                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
158                         zapleft:
ragge
1.8
159
160                         q = p->n_left->n_left;
161                         nfree(p->n_left->n_right);
162                         nfree(p->n_left);
163                         p->n_left = q;
ragge
1.1
164                 }
ragge
1.14
165                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
166                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
167                         goto zapleft;
168                         }
ragge
1.10
169                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
170                         zapright:
ragge
1.8
171                         nfree(p->n_right);
ragge
1.14
172                         q = makety(p->n_leftp->n_typep->n_qual,
173                             p->n_dfp->n_sue);
ragge
1.8
174                         nfree(p);
175                         return clocal(q);
ragge
1.10
176                         }
ragge
1.1
177
ragge
1.10
178                 /* change muls to shifts */
ragge
1.1
179
ragge
1.10
180                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
181                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
182                                 goto zapright;
183                                 }
ragge
1.10
184                         o = p->n_op = LS;
185                         p->n_right->n_type = INT;
186                         p->n_right->n_df = NULL;
187                         RV(p) = i;
ragge
1.1
188                         }
189
190                 /* change +'s of negative consts back to - */
ragge
1.3
191                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
192                         RV(p) = -RV(p);
ragge
1.3
193                         o = p->n_op = MINUS;
ragge
1.1
194                         }
195                 break;
196
197         case DIV:
ragge
1.19
198                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
199                         goto zapright;
200                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
201                         goto zapright;
ragge
1.1
202                 break;
203
204         case EQ:
205         case NE:
206         case LT:
207         case LE:
208         case GT:
209         case GE:
210         case ULT:
211         case ULE:
212         case UGT:
213         case UGE:
214                 if( !LCON(p) ) break;
215
216                 /* exchange operands */
217
ragge
1.3
218                 sp = p->n_left;
219                 p->n_left = p->n_right;
220                 p->n_right = sp;
221                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
222                 break;
223
224                 }
225
226         return(p);
227         }
228
ragge
1.2
229 int
230 ispow2(CONSZ c)
231 {
232         int i;
ragge
1.1
233         ifc <= 0 || (c&(c-1)) ) return(-1);
234         fori=0c>1; ++ic >>= 1;
235         return(i);
ragge
1.2
236 }
ragge
1.10
237
238 int
239 nnconp ) NODE *p; {
240         /* is p a constant without a name */
241         returnp->n_op == ICON && p->n_sp == NULL );
242         }
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-09-01 15:40 +0200