Skip to content

Commit 6683a17

Browse files
Merge pull request #48 from AjayBrahmakshatriya/master
Fixed a bug in sub expression cleanup
2 parents eb9039e + 766b483 commit 6683a17

File tree

7 files changed

+117
-7
lines changed

7 files changed

+117
-7
lines changed

include/blocks/stmt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class expr_stmt : public stmt {
2929

3030
expr::Ptr expr1;
3131

32+
// member to keep track if this expr stmt
33+
// has been spuriously created and needs to be deleted
34+
bool mark_for_deletion = false;
35+
3236
virtual bool is_same(block::Ptr other) override {
3337
if (!isa<expr_stmt>(other))
3438
return false;

include/blocks/sub_expr_cleanup.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef SUB_EXPR_CLEANUP_H
2+
#define SUB_EXPR_CLEANUP_H
3+
#include "blocks/block_visitor.h"
4+
#include "blocks/stmt.h"
5+
namespace block {
6+
class sub_expr_cleanup : public block_visitor {
7+
public:
8+
using block_visitor::visit;
9+
virtual void visit(stmt_block::Ptr);
10+
};
11+
} // namespace block
12+
#endif

samples/outputs.var_names/sample48

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FUNC_DECL
2+
SCALAR_TYPE (VOID)
3+
STMT_BLOCK
4+
DECL_STMT
5+
SCALAR_TYPE (INT)
6+
VAR (x_0)
7+
NO_INITIALIZATION
8+
WHILE_STMT
9+
INT_CONST (1)
10+
STMT_BLOCK
11+
DECL_STMT
12+
SCALAR_TYPE (INT)
13+
VAR (var1)
14+
INT_CONST (1)
15+
DECL_STMT
16+
SCALAR_TYPE (INT)
17+
VAR (var2)
18+
INT_CONST (2)
19+
void my_bar (void) {
20+
int x_0;
21+
while (1) {
22+
int var1 = 1;
23+
int var2 = 2;
24+
}
25+
}
26+

samples/outputs/sample48

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FUNC_DECL
2+
SCALAR_TYPE (VOID)
3+
STMT_BLOCK
4+
DECL_STMT
5+
SCALAR_TYPE (INT)
6+
VAR (var0)
7+
NO_INITIALIZATION
8+
WHILE_STMT
9+
INT_CONST (1)
10+
STMT_BLOCK
11+
DECL_STMT
12+
SCALAR_TYPE (INT)
13+
VAR (var1)
14+
INT_CONST (1)
15+
DECL_STMT
16+
SCALAR_TYPE (INT)
17+
VAR (var2)
18+
INT_CONST (2)
19+
void my_bar (void) {
20+
int var0;
21+
while (1) {
22+
int var1 = 1;
23+
int var2 = 2;
24+
}
25+
}
26+

samples/sample48.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "blocks/c_code_generator.h"
2+
#include "builder/array.h"
3+
#include "builder/dyn_var.h"
4+
#include "builder/static_var.h"
5+
6+
using builder::dyn_arr;
7+
using builder::dyn_var;
8+
using builder::static_var;
9+
10+
static void foo(void) {
11+
dyn_var<int> x;
12+
while (1)
13+
dyn_arr<int, 2> z = {1, 2};
14+
}
15+
int main(int argc, char *argv[]) {
16+
builder::builder_context context;
17+
auto ast = context.extract_function_ast(foo, "my_bar");
18+
ast->dump(std::cout, 0);
19+
20+
block::c_code_generator::generate_code(ast, std::cout, 0);
21+
return 0;
22+
}

src/blocks/sub_expr_cleanup.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "blocks/sub_expr_cleanup.h"
2+
3+
namespace block {
4+
5+
void sub_expr_cleanup::visit(stmt_block::Ptr sb) {
6+
std::vector<stmt::Ptr> new_stmts;
7+
for (auto stmt : sb->stmts) {
8+
if (!isa<expr_stmt>(stmt) || !to<expr_stmt>(stmt)->mark_for_deletion) {
9+
new_stmts.push_back(stmt);
10+
}
11+
}
12+
sb->stmts = new_stmts;
13+
block_visitor::visit(sb);
14+
}
15+
16+
} // namespace block

src/builder/builder_context.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "blocks/loop_finder.h"
66
#include "blocks/loop_roll.h"
77
#include "blocks/rce.h"
8+
#include "blocks/sub_expr_cleanup.h"
89
#include "blocks/var_namer.h"
910
#include "builder/builder.h"
1011
#include "builder/dyn_var.h"
@@ -97,18 +98,16 @@ void builder_context::remove_node_from_sequence(block::expr::Ptr e) {
9798
} else {
9899
// Could be committed already
99100
// It is safe to update the parent block here, because the memoization doesn't care about indices
100-
std::vector<block::stmt::Ptr> new_stmts;
101+
// But don't actually delete the statement, because there could be gotos that are jumping here
102+
// instead just mark it for deletion later
101103
for (auto stmt : current_block_stmt->stmts) {
102-
bool found = false;
103104
if (block::isa<block::expr_stmt>(stmt)) {
104105
auto expr_s = block::to<block::expr_stmt>(stmt);
105-
if (expr_s->expr1 == e)
106-
found = true;
106+
if (expr_s->expr1 == e) {
107+
expr_s->mark_for_deletion = true;
108+
}
107109
}
108-
if (!found)
109-
new_stmts.push_back(stmt);
110110
}
111-
current_block_stmt->stmts = new_stmts;
112111
}
113112
}
114113
void builder_context::add_node_to_sequence(block::expr::Ptr e) {
@@ -284,6 +283,11 @@ block::stmt::Ptr builder_context::extract_ast_from_function_impl(void) {
284283
inserter.offset_to_label = creator.offset_to_label;
285284
ast->accept(&inserter);
286285

286+
// At this point it is safe to remove statements that are
287+
// marked for deletion
288+
block::sub_expr_cleanup cleaner;
289+
ast->accept(&cleaner);
290+
287291
if (run_rce) {
288292
block::eliminate_redundant_vars(ast);
289293
}

0 commit comments

Comments
 (0)