182 if (str.size() <= allowedLength)
return str;
183 auto itr = newNameMap.find(str);
184 if (itr != newNameMap.end())
return itr->second;
186 cstring newStr = str.substr(0, allowedLength - std::to_string(count).size());
187 newStr += std::to_string(count);
189 newNameMap.insert(std::pair<cstring, cstring>(str, newStr));
190 origNameMap.insert(std::pair<cstring, cstring>(newStr, str));
195 if (name.originalName ==
"NoAction")
return name.originalName;
201 : newNameMap(newNameMap) {}
204 const IR::Node *preorder(IR::Member *m)
override {
205 if (m->toString().startsWith(
"m.") || m->toString().startsWith(
"t."))
206 m->member = shortenString(m->member);
208 m->member = shortenString(m->member, 30);
212 const IR::Node *preorder(IR::DpdkStructType *s)
override {
213 if (s->getAnnotations()->getSingle(
"__packet_data__"_cs)) {
214 s->name = shortenString(s->name);
216 for (
auto field : s->fields) {
217 IR::StructField *f =
new IR::StructField(field->name, field->type);
218 f->name = shortenString(f->name, 30);
219 changedFields.push_back(f);
221 return new IR::DpdkStructType(s->srcInfo, s->name, s->annotations, changedFields);
223 s->name = shortenString(s->name);
225 for (
auto field : s->fields) {
226 IR::StructField *f =
new IR::StructField(field->name, field->type);
227 f->name = shortenString(f->name);
228 changedFields.push_back(f);
230 return new IR::DpdkStructType(s->srcInfo, s->name, s->annotations, changedFields);
235 const IR::Node *preorder(IR::DpdkHeaderType *h)
override {
236 h->name = shortenString(h->name);
238 for (
auto field : h->fields) {
239 IR::StructField *f =
new IR::StructField(field->name, field->type);
240 f->name = shortenString(f->name, 30);
241 changedFields.push_back(f);
243 return new IR::DpdkHeaderType(h->srcInfo, h->name, h->annotations, changedFields);
246 const IR::Node *preorder(IR::DpdkExternDeclaration *e)
override {
247 e->name = shortenString(e->name);
251 const IR::Node *preorder(IR::Declaration *g)
override {
252 g->name = shortenString(g->name);
256 void shortenParamTypeName(IR::ParameterList &pl) {
258 for (
auto p : pl.parameters) {
259 auto newType0 = p->type->to<IR::Type_Name>();
260 auto path0 = newType0->path->clone();
261 path0->name = shortenString(path0->name);
262 new_pl.push_back(
new IR::Parameter(p->srcInfo, p->name, p->annotations, p->direction,
263 new IR::Type_Name(newType0->srcInfo, path0),
266 pl = IR::ParameterList{new_pl};
269 const IR::Node *preorder(IR::DpdkAction *a)
override {
270 a->name = shortenString(dropSuffixIfNoAction(a->name));
271 shortenParamTypeName(a->para);
275 const IR::Node *preorder(IR::ActionList *al)
override {
277 for (
auto ale : al->actionList) {
278 auto methodCallExpr = ale->expression->to<IR::MethodCallExpression>();
279 auto pathExpr = methodCallExpr->method->to<IR::PathExpression>();
280 auto path0 = pathExpr->path->clone();
281 path0->name = shortenString(dropSuffixIfNoAction(path0->name));
282 new_al.push_back(
new IR::ActionListElement(
283 ale->srcInfo, ale->annotations,
284 new IR::MethodCallExpression(
285 methodCallExpr->srcInfo, methodCallExpr->type,
286 new IR::PathExpression(pathExpr->srcInfo, pathExpr->type, path0),
287 methodCallExpr->typeArguments, methodCallExpr->arguments)));
289 return new IR::ActionList(al->srcInfo, new_al);
292 const IR::Node *preorder(IR::DpdkTable *t)
override {
293 t->name = shortenString(t->name);
294 auto methodCallExpr = t->default_action->to<IR::MethodCallExpression>();
295 auto pathExpr = methodCallExpr->method->to<IR::PathExpression>();
296 auto path0 = pathExpr->path->clone();
297 path0->name = shortenString(dropSuffixIfNoAction(path0->name));
298 t->default_action =
new IR::MethodCallExpression(
299 methodCallExpr->srcInfo, methodCallExpr->type,
300 new IR::PathExpression(pathExpr->srcInfo, pathExpr->type, path0),
301 methodCallExpr->typeArguments, methodCallExpr->arguments);
305 const IR::Node *preorder(IR::DpdkLearner *l)
override {
306 l->name = shortenString(l->name);
310 const IR::Node *preorder(IR::DpdkSelector *s)
override {
311 s->name = shortenString(s->name);
315 const IR::Node *preorder(IR::DpdkLearnStatement *ls)
override {
316 ls->action = shortenString(dropSuffixIfNoAction(ls->action));
320 const IR::Node *preorder(IR::DpdkApplyStatement *as)
override {
321 as->table = shortenString(as->table);
325 const IR::Node *preorder(IR::DpdkJmpStatement *j)
override {
326 j->label = shortenString(j->label);
330 const IR::Node *preorder(IR::DpdkLabelStatement *ls)
override {
331 ls->label = shortenString(ls->label);
335 const IR::Node *preorder(IR::DpdkJmpActionStatement *jas)
override {
336 jas->action = shortenString(dropSuffixIfNoAction(jas->action));
351 std::unordered_map<cstring, int> defInfo;
352 std::unordered_map<
cstring ,
const IR::Expression * > replacementMap;
353 std::unordered_map<cstring, bool> dontEliminate;
356 dontEliminate[
"m.pna_main_output_metadata_output_port"_cs] =
true;
357 dontEliminate[
"m.psa_ingress_output_metadata_drop"_cs] =
true;
358 dontEliminate[
"m.psa_ingress_output_metadata_egress_port"_cs] =
true;
361 bool preorder(
const IR::DpdkJmpCondStatement *b)
override {
367 bool preorder(
const IR::DpdkLearnStatement *b)
override {
369 dontEliminate[b->timeout->toString()] =
true;
371 usesInfo[b->argument->toString()]++;
374 dontEliminate[b->argument->toString()] =
true;
379 bool preorder(
const IR::DpdkUnaryStatement *u)
override {
381 defInfo[u->dst->toString()]++;
383 dontEliminate[u->dst->toString()] =
true;
387 bool preorder(
const IR::DpdkBinaryStatement *b)
override {
390 defInfo[b->dst->toString()]++;
393 dontEliminate[b->dst->toString()] =
true;
394 dontEliminate[b->src1->toString()] =
true;
398 bool preorder(
const IR::DpdkMovStatement *mv)
override {
399 defInfo[mv->dst->toString()]++;
401 replacementMap[mv->dst->toString()] = mv->src;
405 bool preorder(
const IR::DpdkCastStatement *c)
override {
407 defInfo[c->dst->toString()]++;
408 replacementMap[c->dst->toString()] = c->src;
412 bool preorder(
const IR::DpdkMirrorStatement *m)
override {
414 usesInfo[m->sessionId->toString()]++;
416 dontEliminate[m->slotId->toString()] =
true;
417 dontEliminate[m->sessionId->toString()] =
true;
421 bool preorder(
const IR::DpdkEmitStatement *e)
override {
422 auto type = typeMap->getType(e->header)->to<IR::Type_Header>();
424 for (
auto f : type->fields) {
425 cstring name = e->header->toString() +
"." + f->name.toString();
431 bool preorder(
const IR::DpdkExtractStatement *e)
override {
432 auto type = typeMap->getType(e->header)->to<IR::Type_Header>();
434 for (
auto f : type->fields) {
435 cstring name = e->header->toString() +
"." + f->name.toString();
441 dontEliminate[e->length->toString()] =
true;
446 bool preorder(
const IR::DpdkLookaheadStatement *l)
override {
447 auto type = typeMap->getType(l->header)->to<IR::Type_Header>();
449 for (
auto f : type->fields) {
450 cstring name = l->header->toString() +
"." + f->name.toString();
456 bool preorder(
const IR::DpdkRxStatement *r)
override {
459 dontEliminate[r->port->toString()] =
true;
463 bool preorder(
const IR::DpdkTxStatement *t)
override {
466 dontEliminate[t->port->toString()] =
true;
470 bool preorder(
const IR::DpdkRecircidStatement *t)
override {
473 dontEliminate[t->pass->toString()] =
true;
477 bool preorder(
const IR::DpdkRearmStatement *r)
override {
481 dontEliminate[r->timeout->toString()] =
true;
486 bool preorder(
const IR::DpdkChecksumAddStatement *c)
override {
489 if (
auto m = c->field->to<IR::Member>())
490 if (m->expr->is<IR::Type_Header>()) dontEliminate[c->field->toString()] =
true;
494 bool preorder(
const IR::DpdkChecksumSubStatement *c)
override {
497 if (
auto m = c->field->to<IR::Member>())
498 if (m->expr->is<IR::Type_Header>()) dontEliminate[c->field->toString()] =
true;
502 bool preorder(
const IR::DpdkGetHashStatement *c)
override {
505 dontEliminate[c->dst->toString()] =
true;
509 bool preorder(
const IR::DpdkVerifyStatement *v)
override {
510 usesInfo[v->condition->toString()]++;
513 dontEliminate[v->condition->toString()] =
true;
514 dontEliminate[v->error->toString()] =
true;
518 bool preorder(
const IR::DpdkMeterDeclStatement *c)
override {
523 bool preorder(
const IR::DpdkMeterExecuteStatement *e)
override {
525 if (e->length)
usesInfo[e->length->toString()]++;
526 usesInfo[e->color_in->toString()]++;
527 usesInfo[e->color_out->toString()]++;
531 bool preorder(
const IR::DpdkCounterCountStatement *c)
override {
533 if (c->incr)
usesInfo[c->incr->toString()]++;
537 bool preorder(
const IR::DpdkRegisterDeclStatement *r)
override {
542 bool preorder(
const IR::DpdkRegisterReadStatement *r)
override {
544 defInfo[r->dst->toString()]++;
548 bool preorder(
const IR::DpdkRegisterWriteStatement *r)
override {
553 bool preorder(
const IR::DpdkTable *t)
override {
554 auto keys = t->match_keys;
556 for (
auto ke : keys->keyElements) {
557 dontEliminate[ke->expression->toString()] =
true;
562 bool haveSingleUseDef(
cstring str) {
return defInfo[str] == 1 &&
usesInfo[str] == 1; }