iverilog/tgt-vvp/vector.c

291 lines
7.2 KiB
C
Raw Normal View History

2002-09-27 18:33:34 +02:00
/*
* Copyright (c) 2002 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vector.c,v 1.6 2005/09/15 02:50:13 steve Exp $"
2002-09-27 18:33:34 +02:00
#endif
# include "vvp_priv.h"
# include <assert.h>
/* Maximum vector bits in a thread. If a thread co-processor is
* implemented, this value may need to be reduced. At that time
* wider operatons will need to be partitioned. For example
* shift operations on WIDE (say > 64k bit) registers.
*/
2003-07-03 19:44:10 +02:00
#define MAX_VEC (256*1024)
2002-09-27 18:33:34 +02:00
static struct allocation_score_s {
ivl_expr_t exp;
unsigned bit;
2002-09-27 18:33:34 +02:00
unsigned alloc : 1;
} allocation_map[MAX_VEC] = { {0} };
2002-09-27 18:33:34 +02:00
/* This is the largest bit to have lookaside values. */
static unsigned lookaside_top = 0;
static inline int peek_bit(unsigned addr)
{
return allocation_map[addr].alloc;
}
static inline void set_bit(unsigned addr)
{
allocation_map[addr].alloc = 1;
}
static inline void clr_bit(unsigned addr)
{
allocation_map[addr].alloc = 0;
}
static inline ivl_expr_t peek_exp(unsigned addr)
{
return allocation_map[addr].exp;
}
static inline unsigned peek_exp_bit(unsigned addr)
{
return allocation_map[addr].bit;
}
static inline void set_exp(unsigned addr, ivl_expr_t exp, unsigned ebit)
{
allocation_map[addr].exp = exp;
allocation_map[addr].bit = ebit;
}
2002-09-27 18:33:34 +02:00
/*
* This clears a vector that was previously allocated by
* allocate_vector. That is, it unmarks all the bits of the map that
* represent this vector.
*
* If the vector is based in one of 4 constant bit values, then there
* are no bits to clear. If the vector is based in the 4-8 result
* area, then someone is broken.
*/
void clr_vector(struct vector_info vec)
{
unsigned idx;
if (vec.base < 4)
return;
assert(vec.base >= 8);
for (idx = 0 ; idx < vec.wid ; idx += 1)
clr_bit(vec.base + idx);
}
static unsigned allocate_vector_no_lookaside(unsigned wid, int skip_lookaside)
2002-09-27 18:33:34 +02:00
{
unsigned base = 8;
unsigned idx = 0;
2002-09-27 18:33:34 +02:00
while (idx < wid) {
assert((base + idx) < MAX_VEC);
if (peek_bit(base+idx) || (skip_lookaside && peek_exp(base+idx))) {
2002-09-27 18:33:34 +02:00
base = base + idx + 1;
idx = 0;
} else {
idx += 1;
}
}
for (idx = 0 ; idx < wid ; idx += 1) {
set_bit(base+idx);
set_exp(base+idx, 0, 0);
2002-09-27 18:33:34 +02:00
}
return base;
}
/*
* This unconditionally allocates a stretch of bits from the register
* set. It never returns a bit addressed <8 (0-3 are constant, 4-7 are
* condition codes).
*
* First try to allocate a vector without interfering with any bits
* cached by the lookaside buffer. If that doesn't work, then try
* again without worrying about trashing lookaside results. This
* should lead to preferentially allocating new bits instead of
* constantly overwriting intermediate results.
*/
unsigned allocate_vector(unsigned wid)
{
unsigned base = allocate_vector_no_lookaside(wid, 1);
if (base == 0)
base = allocate_vector_no_lookaside(wid, 0);
return base;
}
2002-09-27 18:33:34 +02:00
/*
* This clears the expression cache of the allocation map. It is
* called to prevent reuse of existing expressions, normally at the
* start of a basic block, but also at the end of thread processing.
2002-09-27 18:33:34 +02:00
*/
void clear_expression_lookaside(void)
{
unsigned idx;
for (idx = 0 ; idx < lookaside_top ; idx += 1)
set_exp(idx, 0, 0);
2002-09-27 18:33:34 +02:00
lookaside_top = 0;
}
void save_expression_lookaside(unsigned addr, ivl_expr_t exp,
unsigned wid)
2002-09-27 18:33:34 +02:00
{
unsigned idx;
assert(addr >= 8);
assert((addr+wid) <= MAX_VEC);
2002-09-27 18:33:34 +02:00
for (idx = 0 ; idx < wid ; idx += 1)
set_exp(addr+idx, exp, idx);
2002-09-27 18:33:34 +02:00
if ((addr+wid) > lookaside_top)
lookaside_top = addr+wid;
}
static int compare_exp(ivl_expr_t l, ivl_expr_t r)
{
if (! (l && r))
return 0;
if (l == r)
return 1;
if (ivl_expr_type(l) != ivl_expr_type(r))
return 0;
switch (ivl_expr_type(l)) {
case IVL_EX_NUMBER:
if (ivl_expr_width(l) != ivl_expr_width(r))
return 0;
{ const char*bitl = ivl_expr_bits(l);
const char*bitr = ivl_expr_bits(r);
unsigned idx;
for (idx = 0 ; idx < ivl_expr_width(l) ; idx += 1) {
if (bitl[idx] != bitr[idx])
return 0;
}
}
return 1;
case IVL_EX_SELECT:
if (! compare_exp(ivl_expr_oper1(l), ivl_expr_oper1(r)))
return 0;
if (ivl_expr_oper2(l) == 0 && ivl_expr_oper1(r) == 0)
return 1;
if (! compare_exp(ivl_expr_oper2(l), ivl_expr_oper2(r)))
return 0;
return 1;
2002-09-27 18:33:34 +02:00
case IVL_EX_SIGNAL:
if (ivl_expr_signal(l) != ivl_expr_signal(r))
return 0;
if (ivl_expr_width(l) != ivl_expr_width(r))
return 0;
return 1;
default:
break;
}
return 0;
}
static unsigned find_expression_lookaside(ivl_expr_t exp,
unsigned wid)
2002-09-27 18:33:34 +02:00
{
unsigned top;
unsigned idx, match;
if (lookaside_top <= wid)
return 0;
top = lookaside_top - wid + 1;
assert(exp);
match = 0;
for (idx = 8 ; idx < top ; idx += 1) {
if (! compare_exp(allocation_map[idx].exp, exp)) {
match = 0;
continue;
}
if (allocation_map[idx].bit != match) {
match = 0;
continue;
}
match += 1;
if (match == wid)
return idx-match+1;
}
return 0;
}
/*
* Look for the expression in the expression lookaside table. If it is
* there, then allocate it and return the base. In this case the
* caller will not need to evaluate the expression. If this function
* returns 0, then the expression is not found and nothing is allocated.
*/
unsigned allocate_vector_exp(ivl_expr_t exp, unsigned wid)
2002-09-27 18:33:34 +02:00
{
unsigned idx;
unsigned la = find_expression_lookaside(exp, wid);
2002-09-27 18:33:34 +02:00
for (idx = 0 ; idx < wid ; idx += 1)
if (allocation_map[la+idx].alloc)
return 0;
for (idx = 0 ; idx < wid ; idx += 1)
allocation_map[la+idx].alloc = 1;
return la;
}
/*
* $Log: vector.c,v $
* Revision 1.6 2005/09/15 02:50:13 steve
* Preserve precalculated expressions when possible.
*
* Revision 1.5 2005/01/24 05:08:02 steve
* Part selects are done in the compiler, not here.
*
2003-07-03 19:44:10 +02:00
* Revision 1.4 2003/07/03 17:44:10 steve
* Wider thread vector limit.
*
* Revision 1.3 2003/06/17 19:17:42 steve
* Remove short int restrictions from vvp opcodes.
*
* Revision 1.2 2003/06/05 04:18:50 steve
* Better width testing for thread vector allocation.
*
2002-09-27 18:33:34 +02:00
* Revision 1.1 2002/09/27 16:33:34 steve
* Add thread expression lookaside map.
*
*/