1 /* $Id: cs_profit.c,v 1.14 1995/03/17 12:32:47 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
9 #include "../share/types.h"
10 #include "../share/debug.h"
11 #include "../share/global.h"
12 #include "../share/aux.h"
13 #include "../share/cset.h"
14 #include "../share/lset.h"
19 #include "cs_partit.h"
21 STATIC cset addr_modes;
23 STATIC cset forbidden;
24 STATIC cset sli_counts;
25 STATIC short LX_threshold;
26 STATIC short AR_limit;
28 STATIC get_instrs(f, s_p)
32 /* Read a set of integers from inputfile f into *s_p.
33 * Such a set must be delimited by a negative number.
37 fscanf(f, "%d", &instr);
39 Cadd((Celem_t) instr, s_p);
40 fscanf(f, "%d", &instr);
44 STATIC choose_cset(f, s_p, max)
48 /* Read two compact sets of integers from inputfile f.
49 * Choose the first if we optimize with respect to time,
50 * the second if we optimize with respect to space, as
51 * indicated by time_space_ratio.
53 cset cs1, cs2; /* Two dummy sets. */
55 *s_p = Cempty_set((short) max);
57 cs1 = Cempty_set((short) max);
59 cs2 = Cempty_set((short) max);
62 Ccopy_set(time_space_ratio >= 50 ? cs1 : cs2, s_p);
64 Cdeleteset(cs1); Cdeleteset(cs2);
73 /* Find piece that is relevant for this phase. */
75 while (getc(f) != '\n');
77 } while (strcmp(s, "%%CS"));
79 /* Choose a set of instructions which must only be eliminated
80 * if they are at the root of another expression.
82 choose_cset(f, &addr_modes, sp_lmnem);
84 /* Choose a set of cheap instructions; i.e. instructions that
85 * are cheaper than a move to save the result of such an
88 choose_cset(f, &cheaps, sp_lmnem);
90 /* Read how many lexical levels back an LXL/LXA instruction
91 * must at least look before it will be eliminated.
93 fscanf(f, "%d %d", &time, &space);
94 LX_threshold = time_space_ratio >= 50 ? time : space;
96 /* Read what the size of an array-element may be,
97 * before we think that it is to big to replace
98 * a LAR/SAR of it by AAR LOI/STI <size>.
100 fscanf(f, "%d", &space);
103 /* Read for what counts we must not eliminate an SLI instruction
104 * when it is part of an array-index computation.
106 choose_cset(f, &sli_counts, 8 * ws);
108 /* Read a set of instructions which we do not want to eliminate.
109 * Note: only instructions need be given that may in principle
110 * be eliminated, but for which better code can be generated
111 * when they stay, and with which is not dealt in the common
114 choose_cset(f, &forbidden, sp_lmnem);
117 STATIC bool sli_no_eliminate(lnp)
120 /* Return whether the SLI-instruction in lnp is part of
121 * an array-index computation, and should not be eliminated.
125 return lnp->l_prev != (line_p) 0 && INSTR(lnp->l_prev) == op_loc &&
126 lnp->l_next != (line_p) 0 && INSTR(lnp->l_next) == op_ads &&
127 ((cst = off_set(lnp->l_prev)), cst == (Celem_t) cst) &&
128 Cis_elem((Celem_t) cst, sli_counts)
132 STATIC bool gains(avp)
135 /* Return whether we can gain something, when we eliminate
136 * an expression such as in avp. We just glue together some
137 * heuristics with some user-supplied stuff.
139 if (Cis_elem(avp->av_instr & BMASK, forbidden))
142 if (avp->av_instr == (byte) op_lxa || avp->av_instr == (byte) op_lxl)
143 return off_set(avp->av_found) >= LX_threshold;
145 if (avp->av_instr == (byte) op_sli || avp->av_instr == (byte) op_slu)
146 return ! sli_no_eliminate(avp->av_found);
148 if (avp->av_instr == (byte) op_ads &&
149 avp->av_found->l_prev &&
150 ( INSTR(avp->av_found->l_prev) == op_sli ||
151 INSTR(avp->av_found->l_prev) == op_slu))
152 return ! sli_no_eliminate(avp->av_found->l_prev);
154 if (Cis_elem(avp->av_instr & BMASK, addr_modes))
155 return instrgroup(avp->av_found->l_prev) != SIMPLE_LOAD;
157 if (Cis_elem(avp->av_instr & BMASK, cheaps))
158 return avp->av_saveloc != (entity_p) 0;
163 STATIC bool okay_lines(avp, ocp)
167 register line_p lnp, next;
170 for (lnp = ocp->oc_lfirst; lnp != (line_p) 0; lnp = next) {
171 next = lnp != ocp->oc_llast ? lnp->l_next : (line_p) 0;
173 if (INSTR(lnp) < sp_fmnem || INSTR(lnp) > sp_lmnem)
175 if (!stack_group(INSTR(lnp))) {
176 /* Check for SAR-instruction. */
177 if (INSTR(lnp) != op_sar || next != (line_p) 0)
181 /* All lines in this occurrence can in principle be eliminated;
182 * no stores, messages, calls etc.
183 * We now check whether it is desirable to treat a LAR or a SAR
184 * as an AAR LOI/STI. This depends on the size of the array-elements.
186 if (INSTR(ocp->oc_llast) == op_lar || INSTR(ocp->oc_llast) == op_sar) {
187 sz = array_elemsize(avp->av_othird);
188 if (sz == UNKNOWN_SIZE) return FALSE;
189 if (avp->av_instr == (byte) op_aar && time_space_ratio < 50) {
190 return sz <= AR_limit;
199 register Lindex i, next;
202 OUTTRACE("no gain", 0);
207 /* Walk through the occurrences to see whether it is okay to
208 * eliminate them. If not, remove them from the set.
210 for (i = Lfirst(avp->av_occurs); i != (Lindex) 0; i = next) {
211 next = Lnext(i, avp->av_occurs);
213 if (!okay_lines(avp, occ_elem(i))) {
214 OUTTRACE("may not eliminate", 0);
216 SHOWOCCUR(occ_elem(i));
218 oldoccur(occ_elem(i));
219 Lremove(Lelem(i), &avp->av_occurs);
223 return Lnrelems(avp->av_occurs) > 0;