1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
/*
* iflib_off.c - Add and optimize explicit offset operators
*
* Written 2001,2002 by Werner Almesberger
* Copyright 2001 EPFL-ICA, Network Robots
* Copyright 2002 Werner Almesberger
*/
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "data.h"
#include "op.h"
#include "field.h"
#include "tc.h"
#include "iflib.h"
#include "tccmeta.h"
/* ----- Move offset from access() to offset() ----------------------------- */
static void use_offset(DATA *d)
{
if (d->op && d->op->dsc == &op_access) {
DATA new;
if (d->op->a.type != dt_field_root) abort();
if (d->op->b.type != dt_unum) abort();
if (d->op->b.op) { /* don't extract constant offset */
new = op_binary(&op_offset,*d,d->op->b);
d->op->b = data_unum(0);
*d = new;
}
use_offset(&d->op->b);
return;
}
if (!d->op) return;
use_offset(&d->op->a);
use_offset(&d->op->b);
use_offset(&d->op->c);
}
/* ----- Extract and combine offsets --------------------------------------- */
static int have_offset(DATA d)
{
if (!d.op) return 0;
if (d.op->dsc == &op_access) return 0;
if (d.op->dsc == &op_field_root) return 1;
if (d.op->dsc == &op_offset) return 1;
return have_offset(d.op->a) || have_offset(d.op->b) || have_offset(d.op->c);
}
static int offset_in_sum(DATA d,DATA offset)
{
if (expr_equal(d,offset)) return 1;
if (!d.op || d.op->dsc != &op_plus) return 0;
return offset_in_sum(d.op->a,offset)+offset_in_sum(d.op->b,offset);
}
/*
* Check if all branches have a common offset (or don't care).
* If we enter the region *except, we don't check the offset there, assuming
* that this region will be removed later (i.e. it is the offset candidate
* under consideration).
*/
static int have_common_offset(DATA *d,DATA offset);
static int do_have_common_offset(DATA *d,DATA offset)
{
if (!d->op) return 1;
if (expr_equal(*d,offset)) return 1;
if (d->op->dsc == &op_access) return 0;
if (d->op->dsc == &op_field_root) return 0;
if (d->op->dsc == &op_offset) {
int terms;
if (!have_common_offset(&d->op->b,offset)) return 0;
terms = offset_in_sum(d->op->b,offset);
return terms == 1 || (terms > 1 && !have_offset(offset));
/* @@@ I suppose, it *could* be recursive ... */
}
return have_common_offset(&d->op->a,offset) &&
have_common_offset(&d->op->b,offset) &&
have_common_offset(&d->op->c,offset);
}
static int have_common_offset(DATA *d,DATA offset)
{
int res;
if (debug > 1) {
debug_expr("have_common_offset on ...",*d);
debug_expr("... with offset",offset);
}
res = do_have_common_offset(d,offset);
if (debug > 1) printf("RESULT have_common_offset = %d\n",res);
return res;
}
static int remove_offset_from_sum(DATA *d,DATA offset)
{
DATA curr = *d;
if (!d->op || d->op->dsc != &op_plus) return 0;
if (expr_equal(d->op->a,offset)) {
if (debug > 1) printf("#### DROP ####\n");
*d = d->op->b;
data_destroy_1(curr.op->a);
return 1;
}
if (expr_equal(d->op->b,offset)) {
if (debug > 1) printf("#### DROP ####\n");
*d = d->op->a;
data_destroy_1(curr.op->b);
return 1;
}
if (remove_offset_from_sum(&d->op->a,offset)) return 1;
return remove_offset_from_sum(&d->op->b,offset);
}
static void remove_offset(DATA *d,DATA offset)
{
if (!d->op) return;
if (d->op->dsc == &op_access) return;
if (d->op->dsc == &op_offset) {
if (debug > 1) {
debug_expr("remove_offset offset ...",offset);
debug_expr("... from",d->op->b);
}
#if 0 /* @@@ looks useless and wrong - check */
if (have_offset(d->op->b)) {
remove_offset(&d->op->b,offset);
return;
}
#endif
if (expr_equal(d->op->b,offset)) {
DATA next;
if (debug > 1) printf("remove_offset: expr_equal\n");
data_destroy(d->op->b);
d->op->b = data_unum(0);
next = d->op->a;
data_destroy_1(*d);
*d = next;
return;
}
else {
if (debug > 1) printf("remove_offset: remove_offset_from_sum\n");
(void) remove_offset_from_sum(&d->op->b,offset);
}
}
remove_offset(&d->op->a,offset);
remove_offset(&d->op->b,offset);
remove_offset(&d->op->c,offset);
}
static DATA look_for_offset_in_sum(DATA d,DATA *root)
{
if (!d.op) return data_none();
if (d.op->dsc == &op_plus) {
DATA tmp;
tmp = look_for_offset_in_sum(d.op->a,root);
if (tmp.type != dt_none) return tmp;
tmp = look_for_offset_in_sum(d.op->b,root);
if (tmp.type != dt_none) return tmp;
}
if (debug > 1) {
printf("*** COMMON %d **************************************\n",
have_common_offset(root,d));
print_expr(stdout,*root);
print_expr(stdout,d);
printf("******************************************************\n");
}
if (have_common_offset(root,d)) return d;
return data_none();
}
static DATA look_for_offset_to_pull(DATA *d,DATA *root)
{
DATA tmp;
if (!d->op) return data_none();
if (d->op->dsc == &op_access) return data_none();
tmp = look_for_offset_to_pull(&d->op->a,root);
if (tmp.type != dt_none) return tmp;
tmp = look_for_offset_to_pull(&d->op->b,root);
if (tmp.type != dt_none) return tmp;
tmp = look_for_offset_to_pull(&d->op->c,root);
if (tmp.type != dt_none) return tmp;
if (d->op->dsc == &op_offset) {
tmp = look_for_offset_in_sum(d->op->b,root);
if (tmp.type != dt_none) {
tmp = data_clone(tmp);
remove_offset(root,tmp);
return tmp;
}
}
return data_none();
}
static void pull_offset(DATA *d)
{
if (!d->op || d->op->dsc != &op_offset) {
DATA found;
found = look_for_offset_to_pull(d,d);
if (debug > 1) {
debug_expr("look_for_offset_to_pull on ...",*d);
debug_expr("... yields",found);
}
if (found.type != dt_none) {
DATA new;
new = op_binary(&op_offset,*d,found);
*d = new;
}
}
if (!d->op) return;
pull_offset(&d->op->a);
pull_offset(&d->op->b);
pull_offset(&d->op->c);
}
/* ----- Extract and combine field roots ----------------------------------- */
static int have_common_root(DATA d,FIELD_ROOT **common_root)
{
FIELD_ROOT *root;
if (!d.op) return 1;
if (!have_offset(d)) return 1;
if (d.op->dsc != &op_field_root) return 0;
root = d.op->b.u.field_root;
if (*common_root) return root == *common_root;
*common_root = root;
return 1;
}
static void pull_root(DATA *d)
{
DATA old;
if (!d->op || d->op->dsc != &op_field_root) return;
old = *d;
*d = d->op->a;
data_destroy_1(old);
}
static void bubble_roots(DATA *d,int limit_bubbling)
{
FIELD_ROOT *common_root;
if (!d->op) {
if (d->type == dt_field_root)
*d = op_binary(&op_field_root,data_none(),*d);
return;
}
bubble_roots(&d->op->a,limit_bubbling);
bubble_roots(&d->op->b,limit_bubbling);
bubble_roots(&d->op->c,limit_bubbling);
common_root = NULL;
if (!have_common_root(d->op->a,&common_root)) return;
if (!have_common_root(d->op->b,&common_root)) {
if (!common_root) return;
}
else {
if (!have_common_root(d->op->c,&common_root)) return;
if (!common_root) return; /* nothing to do */
if (!limit_bubbling) {
pull_root(&d->op->b);
pull_root(&d->op->c);
}
}
/*
* Don't bubble field root beyond access if this is a meta field. This way,
* we avoid ||s under a common match, which would make it virtually
* impossible for if_u32.c to extract meta field matches.
*/
if (limit_bubbling &&
(d->op->dsc == &op_logical_and || d->op->dsc == &op_logical_or))
return;
pull_root(&d->op->a);
*d = op_binary(&op_field_root,*d,data_field_root(common_root));
}
/* ----- Remove redundant roots -------------------------------------------- */
static void trim_roots(DATA *d,const FIELD_ROOT *root)
{
while (d->op && d->op->dsc == &op_field_root) {
const FIELD_ROOT *this_root = d->op->b.u.field_root;
DATA next = d->op->a;
if (root != this_root) {
root = this_root;
break;
}
data_destroy_1(*d);
*d = next;
}
if (d->op) {
trim_roots(&d->op->a,root);
trim_roots(&d->op->b,root);
trim_roots(&d->op->c,root);
}
}
/* ----- Propagate constant offsets to access ------------------------------ */
static int extract_sum(DATA *d)
{
DATA old;
int a,b;
int sum;
if (d->op && d->op->dsc == &op_offset) return extract_sum(&d->op->a);
if (!d->op || d->op->dsc != &op_plus) return 0;
sum = extract_sum(&d->op->a)+extract_sum(&d->op->b);
a = !d->op->a.op && d->op->a.type == dt_unum;
b = !d->op->b.op && d->op->b.type == dt_unum;
if (!a && !b) return sum;
sum += (a ? d->op->a.u.unum : 0)+(b ? d->op->b.u.unum : 0);
old = *d;
if (a && b) {
*d = data_unum(0);
data_destroy(old);
return sum;
}
*d = a ? d->op->b : d->op->a;
data_destroy_1(old);
return sum;
}
static void do_push_offset(DATA *d,int base)
{
int new_base = base;
if (!d->op) return;
if (d->op->dsc == &op_offset) {
new_base += extract_sum(&d->op->b);
if (!d->op->b.op && d->op->b.type == dt_unum) {
DATA old;
new_base += d->op->b.u.unum;
old = *d;
*d = d->op->a;
data_destroy_1(old);
do_push_offset(d,new_base);
return;
}
}
if (d->op->dsc == &op_access) {
d->op->b.u.unum += base;
return;
}
do_push_offset(&d->op->a,new_base);
do_push_offset(&d->op->b,base);
do_push_offset(&d->op->c,base);
}
static void push_offset(DATA *d)
{
do_push_offset(d,0);
}
/* ------------------------------------------------------------------------- */
void iflib_offset(DATA *d,int for_u32)
{
use_offset(d);
debug_expr("AFTER use_offset",*d);
bubble_roots(d,for_u32);
debug_expr("AFTER bubble_roots",*d);
trim_roots(d,field_root(0));
debug_expr("AFTER trim_roots",*d);
if (for_u32) {
pull_offset(d);
debug_expr("AFTER pull_offset",*d);
}
push_offset(d);
debug_expr("AFTER push_offset",*d);
}
|