AST code working

This commit is contained in:
Vern Paxson 2024-05-09 15:11:32 -07:00
parent 1961e1243f
commit 694095c56f
3 changed files with 48 additions and 13 deletions

View file

@ -2860,18 +2860,19 @@ RecordFieldUpdates::RecordFieldUpdates(ExprTag t, const std::vector<const Stmt*>
: BinaryExpr(t, get_RFU_LHS_var(stmts[0]), get_RFU_RHS_var(stmts[0])) { : BinaryExpr(t, get_RFU_LHS_var(stmts[0]), get_RFU_RHS_var(stmts[0])) {
for ( auto s : stmts ) { for ( auto s : stmts ) {
auto s_e = s->AsExprStmt()->StmtExpr(); auto s_e = s->AsExprStmt()->StmtExpr();
auto lhs = s_e->GetOp1(); auto lhs = s_e->GetOp1()->GetOp1();
auto lhs_field = lhs->AsFieldExpr()->Field(); auto lhs_field = lhs->AsFieldExpr()->Field();
auto rhs = s_e->GetOp2(); auto rhs = s_e->GetOp2();
if ( rhs->Tag() != EXPR_FIELD ) if ( rhs->Tag() != EXPR_FIELD )
rhs = rhs->GetOp2(); rhs = rhs->GetOp2();
auto rhs_field = rhs->GetOp2()->AsFieldExpr()->Field(); auto rhs_field = rhs->AsFieldExpr()->Field();
lhs_map.push_back(lhs_field); lhs_map.push_back(lhs_field);
rhs_map.push_back(rhs_field); rhs_map.push_back(rhs_field);
ASSERT(stmt_pool.count(s) > 0);
stmt_pool.erase(s); stmt_pool.erase(s);
} }
} }
@ -2901,6 +2902,26 @@ void RecordFieldUpdates::ExprDescribe(ODesc* d) const {
op2->Describe(d); op2->Describe(d);
} }
ExprPtr RecordFieldUpdates::Reduce(Reducer* c, StmtPtr& red_stmt) {
if ( c->Optimizing() ) {
op1 = c->UpdateExpr(op1);
op2 = c->UpdateExpr(op2);
}
red_stmt = nullptr;
if ( ! op1->IsSingleton(c) )
op1 = op1->ReduceToSingleton(c, red_stmt);
StmtPtr red2_stmt;
if ( ! op2->IsSingleton(c) )
op2 = op2->ReduceToSingleton(c, red2_stmt);
red_stmt = MergeStmts(red_stmt, std::move(red2_stmt));
return ThisPtr();
}
ExprPtr AssignRecordFields::Duplicate() { ExprPtr AssignRecordFields::Duplicate() {
auto e1 = op1->Duplicate(); auto e1 = op1->Duplicate();
auto e2 = op2->Duplicate(); auto e2 = op2->Duplicate();

View file

@ -115,6 +115,7 @@ public:
bool IsPure() const override { return false; } bool IsPure() const override { return false; }
bool IsReduced(Reducer* c) const override; bool IsReduced(Reducer* c) const override;
ExprPtr Reduce(Reducer* c, StmtPtr& red_stmt) override;
protected: protected:
RecordFieldUpdates(ExprTag t, const std::vector<const Stmt*>& stmts, std::set<const Stmt*>& stmt_pool); RecordFieldUpdates(ExprTag t, const std::vector<const Stmt*>& stmts, std::set<const Stmt*>& stmt_pool);

View file

@ -856,12 +856,24 @@ static void UpdateAssignmentChains(const StmtPtr& s, OpChain& assign_chains, OpC
cf->second.push_back(s.get()); cf->second.push_back(s.get());
} }
static void TransformChain(const OpChain& c, ExprTag t, std::set<const Stmt*>& chain_stmts) { static StmtPtr TransformChain(const OpChain& c, ExprTag t, std::set<const Stmt*>& chain_stmts, StmtPtr s0) {
for ( auto& id_stmts : c ) auto sl = with_location_of(make_intrusive<StmtList>(), s0);
for ( auto i_s : id_stmts.second ) { auto& stmts = sl->Stmts();
ASSERT(chain_stmts.count(i_s) > 0);
chain_stmts.erase(i_s); for ( auto& id_stmts : c ) {
auto orig_s = id_stmts.second;
ExprPtr e;
if ( t == EXPR_ASSIGN )
e = make_intrusive<AssignRecordFields>(orig_s, chain_stmts);
else
e = make_intrusive<AddRecordFields>(orig_s, chain_stmts);
e->SetLocationInfo(sl->GetLocationInfo());
auto es = with_location_of(make_intrusive<ExprStmt>(std::move(e)), sl);
stmts.emplace_back(std::move(es));
} }
return sl;
} }
static bool SimplifyChain(const std::vector<StmtPtr>& stmts, unsigned int start, unsigned int end, static bool SimplifyChain(const std::vector<StmtPtr>& stmts, unsigned int start, unsigned int end,
@ -890,13 +902,14 @@ static bool SimplifyChain(const std::vector<StmtPtr>& stmts, unsigned int start,
return false; return false;
} }
TransformChain(assign_chains, EXPR_ASSIGN, chain_stmts); f_stmts.push_back(TransformChain(assign_chains, EXPR_ASSIGN, chain_stmts, stmts[start]));
TransformChain(add_chains, EXPR_ADD, chain_stmts); f_stmts.push_back(TransformChain(add_chains, EXPR_ADD, chain_stmts, stmts[start]));
printf("chain reduction %d -> %lu starting at %s\n", end - start + 1, chain_stmts.size(), for ( auto s : stmts )
obj_desc(stmts[start].get()).c_str()); if ( chain_stmts.count(s.get()) > 0 )
f_stmts.push_back(s);
return false; return true;
} }
bool StmtList::ReduceStmt(unsigned int& s_i, std::vector<StmtPtr>& f_stmts, Reducer* c) { bool StmtList::ReduceStmt(unsigned int& s_i, std::vector<StmtPtr>& f_stmts, Reducer* c) {