optimization of scripting idioms - min/max, has-elements

This commit is contained in:
Vern Paxson 2024-04-08 18:16:15 -04:00 committed by Tim Wojtulewicz
parent e38053ee3c
commit 5445e8c7ff
5 changed files with 194 additions and 8 deletions

View file

@ -4,6 +4,7 @@
#include "zeek/Desc.h"
#include "zeek/Reporter.h"
#include "zeek/script_opt/ZAM/BuiltIn.h"
#include "zeek/script_opt/ZAM/Compile.h"
namespace zeek::detail {
@ -134,8 +135,12 @@ const ZAMStmt ZAMCompiler::CompileAssignExpr(const AssignExpr* e) {
auto op2 = e->GetOp2();
auto lhs = op1->AsRefExpr()->GetOp1()->AsNameExpr();
auto lt = lhs->GetType().get();
auto rhs = op2.get();
if ( rhs->Tag() == EXPR_SCRIPT_OPT_BUILTIN )
return CompileZAMBuiltin(lhs, static_cast<const ScriptOptBuiltinExpr*>(rhs));
auto lt = lhs->GetType().get();
auto r1 = rhs->GetOp1();
if ( rhs->Tag() == EXPR_INDEX && (r1->Tag() == EXPR_NAME || r1->Tag() == EXPR_CONST) )
@ -178,8 +183,10 @@ const ZAMStmt ZAMCompiler::CompileAssignExpr(const AssignExpr* e) {
return L_In_VecVLC(lhs, r1->AsListExpr(), r2c);
}
if ( rhs->Tag() == EXPR_ANY_INDEX )
return AnyIndexVVi(lhs, r1->AsNameExpr(), rhs->AsAnyIndexExpr()->Index());
if ( rhs->Tag() == EXPR_ANY_INDEX ) {
auto rhs_as_any = static_cast<const AnyIndexExpr*>(rhs);
return AnyIndexVVi(lhs, r1->AsNameExpr(), rhs_as_any->Index());
}
if ( rhs->Tag() == EXPR_LAMBDA )
return BuildLambda(lhs, rhs->AsLambdaExpr());
@ -220,6 +227,72 @@ const ZAMStmt ZAMCompiler::CompileAssignExpr(const AssignExpr* e) {
#include "ZAM-GenExprsDefsV.h"
}
const ZAMStmt ZAMCompiler::CompileZAMBuiltin(const NameExpr* lhs, const ScriptOptBuiltinExpr* zbi) {
auto op1 = zbi->GetOp1();
auto op2 = zbi->GetOp2();
switch ( zbi->Tag() ) {
case ScriptOptBuiltinExpr::MINIMUM:
case ScriptOptBuiltinExpr::MAXIMUM: {
auto t1 = op1->GetType()->InternalType();
ASSERT(t1 == op2->GetType()->InternalType());
bool is_min = zbi->Tag() == ScriptOptBuiltinExpr::MINIMUM;
// Canonicalize to have constant as second op.
if ( op1->Tag() == EXPR_CONST ) {
ASSERT(op2->Tag() != EXPR_CONST);
std::swap(op1, op2);
is_min = ! is_min;
}
ZOp op;
auto n1 = op1->AsNameExpr();
auto n2 = op2->Tag() == EXPR_NAME ? op2->AsNameExpr() : nullptr;
auto c = op2->Tag() == EXPR_CONST ? op2->AsConstExpr() : nullptr;
if ( c ) {
if ( t1 == TYPE_INTERNAL_UNSIGNED )
op = is_min ? OP_MINU_VVC : OP_MAXU_VVC;
else if ( t1 == TYPE_INTERNAL_INT )
op = is_min ? OP_MINI_VVC : OP_MAXI_VVC;
else {
ASSERT(t1 == TYPE_INTERNAL_DOUBLE);
op = is_min ? OP_MIND_VVC : OP_MAXD_VVC;
}
}
else {
if ( t1 == TYPE_INTERNAL_UNSIGNED )
op = is_min ? OP_MINU_VVV : OP_MAXU_VVV;
else if ( t1 == TYPE_INTERNAL_INT )
op = is_min ? OP_MINI_VVV : OP_MAXI_VVV;
else {
ASSERT(t1 == TYPE_INTERNAL_DOUBLE);
op = is_min ? OP_MIND_VVV : OP_MAXD_VVV;
}
}
if ( c )
return AddInst(GenInst(op, lhs, n1, c));
else
return AddInst(GenInst(op, lhs, n1, n2));
}
case ScriptOptBuiltinExpr::HAS_ELEMENTS: {
auto n = op1->AsNameExpr();
auto op = op1->GetType()->Tag() == TYPE_TABLE ? OP_TABLE_HAS_ELEMENTS_VV : OP_VECTOR_HAS_ELEMENTS_VV;
return AddInst(GenInst(op, lhs, n));
}
case ScriptOptBuiltinExpr::FUNC_ID_STRING: {
auto n = op1->AsNameExpr();
return AddInst(GenInst(OP_FUNC_ID_STRING_VV, lhs, n));
}
default: reporter->InternalError("bad built-in tag in ZAMCompiler::CompileZAMBuiltin");
}
}
const ZAMStmt ZAMCompiler::CompileAssignToIndex(const NameExpr* lhs, const IndexExpr* rhs) {
auto aggr = rhs->GetOp1();
auto const_aggr = aggr->Tag() == EXPR_CONST;