1994-06-24 11:31:16 +00:00
|
|
|
/* $Id$ */
|
1987-03-09 19:15:41 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1984-11-26 13:58:05 +00:00
|
|
|
#include <stdio.h>
|
2017-11-14 22:04:01 +00:00
|
|
|
#include <string.h>
|
1991-03-05 12:16:17 +00:00
|
|
|
#include <em_mnem.h>
|
|
|
|
#include <em_spec.h>
|
1984-11-26 13:58:05 +00:00
|
|
|
#include "../share/types.h"
|
|
|
|
#include "../share/debug.h"
|
|
|
|
#include "../share/global.h"
|
2019-02-08 23:02:41 +00:00
|
|
|
#include "../share/utils.h"
|
1984-11-26 13:58:05 +00:00
|
|
|
#include "../share/cset.h"
|
|
|
|
#include "../share/lset.h"
|
|
|
|
#include "cs.h"
|
2018-03-08 23:51:07 +00:00
|
|
|
#include "cs_alloc.h"
|
1984-11-26 13:58:05 +00:00
|
|
|
#include "cs_aux.h"
|
|
|
|
#include "cs_debug.h"
|
|
|
|
#include "cs_avail.h"
|
|
|
|
#include "cs_partit.h"
|
|
|
|
|
|
|
|
STATIC cset addr_modes;
|
|
|
|
STATIC cset cheaps;
|
|
|
|
STATIC cset forbidden;
|
1988-06-21 16:10:19 +00:00
|
|
|
STATIC cset sli_counts;
|
1984-11-26 13:58:05 +00:00
|
|
|
STATIC short LX_threshold;
|
|
|
|
STATIC short AR_limit;
|
2018-03-05 18:32:06 +00:00
|
|
|
STATIC bool RM_to_DV;
|
1984-11-26 13:58:05 +00:00
|
|
|
|
2018-02-05 21:09:30 +00:00
|
|
|
STATIC void get_instrs(FILE *f, cset *s_p)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
1988-06-21 15:31:51 +00:00
|
|
|
/* Read a set of integers from inputfile f into *s_p.
|
|
|
|
* Such a set must be delimited by a negative number.
|
1984-11-26 13:58:05 +00:00
|
|
|
*/
|
1985-01-23 16:16:49 +00:00
|
|
|
int instr;
|
1984-11-26 13:58:05 +00:00
|
|
|
|
|
|
|
fscanf(f, "%d", &instr);
|
1988-06-21 15:31:51 +00:00
|
|
|
while (instr >= 0) {
|
1985-01-23 16:16:49 +00:00
|
|
|
Cadd((Celem_t) instr, s_p);
|
1984-11-26 13:58:05 +00:00
|
|
|
fscanf(f, "%d", &instr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:09:30 +00:00
|
|
|
STATIC void choose_cset(FILE *f, cset *s_p, int max)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
1988-06-21 15:31:51 +00:00
|
|
|
/* Read two compact sets of integers from inputfile f.
|
1984-11-26 13:58:05 +00:00
|
|
|
* Choose the first if we optimize with respect to time,
|
|
|
|
* the second if we optimize with respect to space, as
|
|
|
|
* indicated by time_space_ratio.
|
|
|
|
*/
|
|
|
|
cset cs1, cs2; /* Two dummy sets. */
|
|
|
|
|
1988-06-21 15:31:51 +00:00
|
|
|
*s_p = Cempty_set((short) max);
|
1984-11-26 13:58:05 +00:00
|
|
|
|
1988-06-21 15:31:51 +00:00
|
|
|
cs1 = Cempty_set((short) max);
|
1984-11-26 13:58:05 +00:00
|
|
|
get_instrs(f, &cs1);
|
1988-06-21 15:31:51 +00:00
|
|
|
cs2 = Cempty_set((short) max);
|
1984-11-26 13:58:05 +00:00
|
|
|
get_instrs(f, &cs2);
|
|
|
|
|
|
|
|
Ccopy_set(time_space_ratio >= 50 ? cs1 : cs2, s_p);
|
|
|
|
|
|
|
|
Cdeleteset(cs1); Cdeleteset(cs2);
|
1988-06-21 15:20:01 +00:00
|
|
|
}
|
1984-11-26 13:58:05 +00:00
|
|
|
|
2017-11-15 21:29:27 +00:00
|
|
|
void cs_machinit(void *vp)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
2017-11-15 21:29:27 +00:00
|
|
|
FILE *f = vp;
|
1984-11-26 13:58:05 +00:00
|
|
|
char s[100];
|
|
|
|
int time, space;
|
|
|
|
|
|
|
|
/* Find piece that is relevant for this phase. */
|
|
|
|
do {
|
|
|
|
while (getc(f) != '\n');
|
2018-06-02 18:51:41 +00:00
|
|
|
fscanf(f, "%99s", s);
|
1984-11-26 13:58:05 +00:00
|
|
|
} while (strcmp(s, "%%CS"));
|
|
|
|
|
|
|
|
/* Choose a set of instructions which must only be eliminated
|
|
|
|
* if they are at the root of another expression.
|
|
|
|
*/
|
1988-06-21 15:31:51 +00:00
|
|
|
choose_cset(f, &addr_modes, sp_lmnem);
|
1984-11-26 13:58:05 +00:00
|
|
|
|
|
|
|
/* Choose a set of cheap instructions; i.e. instructions that
|
|
|
|
* are cheaper than a move to save the result of such an
|
|
|
|
* instruction.
|
|
|
|
*/
|
1988-06-21 15:31:51 +00:00
|
|
|
choose_cset(f, &cheaps, sp_lmnem);
|
1984-11-26 13:58:05 +00:00
|
|
|
|
|
|
|
/* Read how many lexical levels back an LXL/LXA instruction
|
|
|
|
* must at least look before it will be eliminated.
|
|
|
|
*/
|
|
|
|
fscanf(f, "%d %d", &time, &space);
|
|
|
|
LX_threshold = time_space_ratio >= 50 ? time : space;
|
|
|
|
|
|
|
|
/* Read what the size of an array-element may be,
|
|
|
|
* before we think that it is to big to replace
|
|
|
|
* a LAR/SAR of it by AAR LOI/STI <size>.
|
|
|
|
*/
|
|
|
|
fscanf(f, "%d", &space);
|
|
|
|
AR_limit = space;
|
|
|
|
|
2018-03-05 18:32:06 +00:00
|
|
|
/* Read whether to convert a remainder RMI/RMU to a division
|
|
|
|
* DVI/DVU using the formula a % b = a - b * (a / b).
|
|
|
|
*/
|
|
|
|
fscanf(f, "%d %d", &time, &space);
|
|
|
|
RM_to_DV = time_space_ratio >= 50 ? time : space;
|
|
|
|
|
1988-06-21 16:10:19 +00:00
|
|
|
/* Read for what counts we must not eliminate an SLI instruction
|
1984-11-26 13:58:05 +00:00
|
|
|
* when it is part of an array-index computation.
|
|
|
|
*/
|
1988-06-21 16:10:19 +00:00
|
|
|
choose_cset(f, &sli_counts, 8 * ws);
|
1984-11-26 13:58:05 +00:00
|
|
|
|
|
|
|
/* Read a set of instructions which we do not want to eliminate.
|
|
|
|
* Note: only instructions need be given that may in principle
|
|
|
|
* be eliminated, but for which better code can be generated
|
|
|
|
* when they stay, and with which is not dealt in the common
|
|
|
|
* decision routines.
|
|
|
|
*/
|
1988-06-21 15:31:51 +00:00
|
|
|
choose_cset(f, &forbidden, sp_lmnem);
|
1984-11-26 13:58:05 +00:00
|
|
|
}
|
|
|
|
|
Check AAR earlier to prevent LOI/STI unknown size.
In ego, the CS phase may convert a LAR/SAR to AAR LOI/STI so it can
optimize multiple occurrences of AAR of the same array element. This
conversion should not happen if it would LOI/STI a large or unknown
size.
cs_profit.c okay_lines() checked the size of each occurrence of AAR
except the first. If the first AAR was the implicit AAR in a LAR/SAR,
then the conversion happened without checking the size. For unknown
size, this made a bad LOI -1 or STI -1. Fix by checking the size
earlier: if a LAR/SAR has a bad size, then don't enter it as an AAR.
This Modula-2 code showed the bug. Given M.def:
DEFINITION MODULE M;
TYPE S = SET OF [0..95];
PROCEDURE F(a: ARRAY OF S; i, j: INTEGER);
END M.
and M.mod:
(*$R-*) IMPLEMENTATION MODULE M;
FROM SYSTEM IMPORT ADDRESS, ADR;
PROCEDURE G(s: S; p, q: ADDRESS; t: S); BEGIN
s := s; p := p; q := q; t := t;
END G;
PROCEDURE F(a: ARRAY OF S; i, j: INTEGER); BEGIN
G(a[i + j], ADR(a[i + j]), ADR(a[i + j]), a[i + j])
END F;
END M.
then the bug caused an error:
$ ack -mlinuxppc -O3 -c.e M.mod
/tmp/Ack_b357d.g, line 57: Argument range error
The bug had put LOI -1 in the code, then em_decode got an error
because -1 is out of range for LOI.
Procedure F has 4 occurrences of `a[i + j]`. The size of `a[i + j]`
is 96 bits, or 12 bytes, but the EM code hides the size in an array
descriptor, so the size is unknown to CS. The pragma `(*$R-*)`
disables a range check on `i + j` so CS can work. EM uses AAR for the
2 `ADR(a[i + j])` and LAR for the other 2 `a[i + j]`. EM pushes the
arguments to G in reverse order, so the last `a[i + j]` in Modula-2 is
the first LAR in EM.
CS found 4 occurrences of AAR. The first AAR was an implicit AAR in
LAR. Because of the bug, CS converted this LAR 4 to AAR 4 LOI -1.
2018-03-02 21:06:21 +00:00
|
|
|
bool may_become_aar(avail_p avp)
|
|
|
|
{
|
|
|
|
/* Check whether it is desirable to treat a LAR or SAR as an
|
|
|
|
* AAR LOI/STI. This depends on the size of the array-elements.
|
|
|
|
*/
|
|
|
|
offset sz;
|
|
|
|
|
|
|
|
sz = array_elemsize(avp->av_othird);
|
|
|
|
if (sz == UNKNOWN_SIZE)
|
|
|
|
return FALSE;
|
|
|
|
if (time_space_ratio < 50)
|
|
|
|
return sz <= AR_limit;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-03-05 18:32:06 +00:00
|
|
|
bool may_become_dv(void)
|
|
|
|
{
|
|
|
|
return RM_to_DV;
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:09:30 +00:00
|
|
|
STATIC bool sli_no_eliminate(line_p lnp)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
|
|
|
/* Return whether the SLI-instruction in lnp is part of
|
1988-06-21 16:10:19 +00:00
|
|
|
* an array-index computation, and should not be eliminated.
|
1984-11-26 13:58:05 +00:00
|
|
|
*/
|
1988-06-21 16:10:19 +00:00
|
|
|
offset cst;
|
|
|
|
|
1984-11-26 13:58:05 +00:00
|
|
|
return lnp->l_prev != (line_p) 0 && INSTR(lnp->l_prev) == op_loc &&
|
1988-06-21 16:10:19 +00:00
|
|
|
lnp->l_next != (line_p) 0 && INSTR(lnp->l_next) == op_ads &&
|
1992-07-21 11:23:24 +00:00
|
|
|
((cst = off_set(lnp->l_prev)), cst == (Celem_t) cst) &&
|
1988-06-21 16:10:19 +00:00
|
|
|
Cis_elem((Celem_t) cst, sli_counts)
|
|
|
|
;
|
1984-11-26 13:58:05 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 21:09:30 +00:00
|
|
|
STATIC bool gains(avail_p avp)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
|
|
|
/* Return whether we can gain something, when we eliminate
|
|
|
|
* an expression such as in avp. We just glue together some
|
|
|
|
* heuristics with some user-supplied stuff.
|
|
|
|
*/
|
|
|
|
if (Cis_elem(avp->av_instr & BMASK, forbidden))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (avp->av_instr == (byte) op_lxa || avp->av_instr == (byte) op_lxl)
|
|
|
|
return off_set(avp->av_found) >= LX_threshold;
|
|
|
|
|
1988-06-22 11:31:45 +00:00
|
|
|
if (avp->av_instr == (byte) op_sli || avp->av_instr == (byte) op_slu)
|
1988-06-21 16:10:19 +00:00
|
|
|
return ! sli_no_eliminate(avp->av_found);
|
1984-11-26 13:58:05 +00:00
|
|
|
|
1988-06-21 16:49:52 +00:00
|
|
|
if (avp->av_instr == (byte) op_ads &&
|
1988-06-22 11:31:45 +00:00
|
|
|
avp->av_found->l_prev &&
|
|
|
|
( INSTR(avp->av_found->l_prev) == op_sli ||
|
|
|
|
INSTR(avp->av_found->l_prev) == op_slu))
|
1988-06-21 16:49:52 +00:00
|
|
|
return ! sli_no_eliminate(avp->av_found->l_prev);
|
|
|
|
|
1984-11-26 13:58:05 +00:00
|
|
|
if (Cis_elem(avp->av_instr & BMASK, addr_modes))
|
|
|
|
return instrgroup(avp->av_found->l_prev) != SIMPLE_LOAD;
|
|
|
|
|
|
|
|
if (Cis_elem(avp->av_instr & BMASK, cheaps))
|
|
|
|
return avp->av_saveloc != (entity_p) 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:09:30 +00:00
|
|
|
STATIC bool okay_lines(avail_p avp, occur_p ocp)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
Check AAR earlier to prevent LOI/STI unknown size.
In ego, the CS phase may convert a LAR/SAR to AAR LOI/STI so it can
optimize multiple occurrences of AAR of the same array element. This
conversion should not happen if it would LOI/STI a large or unknown
size.
cs_profit.c okay_lines() checked the size of each occurrence of AAR
except the first. If the first AAR was the implicit AAR in a LAR/SAR,
then the conversion happened without checking the size. For unknown
size, this made a bad LOI -1 or STI -1. Fix by checking the size
earlier: if a LAR/SAR has a bad size, then don't enter it as an AAR.
This Modula-2 code showed the bug. Given M.def:
DEFINITION MODULE M;
TYPE S = SET OF [0..95];
PROCEDURE F(a: ARRAY OF S; i, j: INTEGER);
END M.
and M.mod:
(*$R-*) IMPLEMENTATION MODULE M;
FROM SYSTEM IMPORT ADDRESS, ADR;
PROCEDURE G(s: S; p, q: ADDRESS; t: S); BEGIN
s := s; p := p; q := q; t := t;
END G;
PROCEDURE F(a: ARRAY OF S; i, j: INTEGER); BEGIN
G(a[i + j], ADR(a[i + j]), ADR(a[i + j]), a[i + j])
END F;
END M.
then the bug caused an error:
$ ack -mlinuxppc -O3 -c.e M.mod
/tmp/Ack_b357d.g, line 57: Argument range error
The bug had put LOI -1 in the code, then em_decode got an error
because -1 is out of range for LOI.
Procedure F has 4 occurrences of `a[i + j]`. The size of `a[i + j]`
is 96 bits, or 12 bytes, but the EM code hides the size in an array
descriptor, so the size is unknown to CS. The pragma `(*$R-*)`
disables a range check on `i + j` so CS can work. EM uses AAR for the
2 `ADR(a[i + j])` and LAR for the other 2 `a[i + j]`. EM pushes the
arguments to G in reverse order, so the last `a[i + j]` in Modula-2 is
the first LAR in EM.
CS found 4 occurrences of AAR. The first AAR was an implicit AAR in
LAR. Because of the bug, CS converted this LAR 4 to AAR 4 LOI -1.
2018-03-02 21:06:21 +00:00
|
|
|
/* Check whether all lines in this occurrence can in
|
|
|
|
* principle be eliminated; no stores, messages, calls etc.
|
|
|
|
*/
|
1984-11-26 13:58:05 +00:00
|
|
|
register line_p lnp, next;
|
|
|
|
|
|
|
|
for (lnp = ocp->oc_lfirst; lnp != (line_p) 0; lnp = next) {
|
|
|
|
next = lnp != ocp->oc_llast ? lnp->l_next : (line_p) 0;
|
|
|
|
|
|
|
|
if (INSTR(lnp) < sp_fmnem || INSTR(lnp) > sp_lmnem)
|
|
|
|
return FALSE;
|
|
|
|
if (!stack_group(INSTR(lnp))) {
|
|
|
|
/* Check for SAR-instruction. */
|
|
|
|
if (INSTR(lnp) != op_sar || next != (line_p) 0)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2017-11-15 21:29:27 +00:00
|
|
|
bool desirable(avail_p avp)
|
1984-11-26 13:58:05 +00:00
|
|
|
{
|
|
|
|
register Lindex i, next;
|
|
|
|
|
|
|
|
if (!gains(avp)) {
|
|
|
|
OUTTRACE("no gain", 0);
|
|
|
|
SHOWAVAIL(avp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Walk through the occurrences to see whether it is okay to
|
|
|
|
* eliminate them. If not, remove them from the set.
|
|
|
|
*/
|
|
|
|
for (i = Lfirst(avp->av_occurs); i != (Lindex) 0; i = next) {
|
|
|
|
next = Lnext(i, avp->av_occurs);
|
|
|
|
|
|
|
|
if (!okay_lines(avp, occ_elem(i))) {
|
|
|
|
OUTTRACE("may not eliminate", 0);
|
|
|
|
# ifdef TRACE
|
|
|
|
SHOWOCCUR(occ_elem(i));
|
|
|
|
# endif
|
|
|
|
oldoccur(occ_elem(i));
|
|
|
|
Lremove(Lelem(i), &avp->av_occurs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Lnrelems(avp->av_occurs) > 0;
|
|
|
|
}
|