From 97cf7348edbb5f102b1bb6dddd0084b155be85d0 Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Mon, 20 May 2024 17:05:32 -0400 Subject: [PATCH] Fixed bug in RCE --- include/builder/operator_overload.h | 8 ++++---- samples/outputs.var_names/sample42 | 1 + samples/outputs.var_names/sample57 | 6 ++++++ samples/outputs/sample42 | 1 + samples/outputs/sample57 | 6 ++++++ samples/sample42.cpp | 2 ++ samples/sample57.cpp | 31 +++++++++++++++++++++++++++++ src/blocks/rce.cpp | 10 ++++++++++ 8 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 samples/outputs.var_names/sample57 create mode 100644 samples/outputs/sample57 create mode 100644 samples/sample57.cpp diff --git a/include/builder/operator_overload.h b/include/builder/operator_overload.h index ede7a3b..dea9877 100644 --- a/include/builder/operator_overload.h +++ b/include/builder/operator_overload.h @@ -156,22 +156,22 @@ typename return_type_helper::type operator&(const T &a) { // Prefix increment template -typename return_type_helper::type operator++(T &a) { +typename return_type_helper::type>::type operator++(T &&a) { return (a = a + 1); } // Postfix increment template -typename return_type_helper::type operator++(T &a, int) { +typename return_type_helper::type>::type operator++(T &&a, int) { return ((a = a + 1) - 1); } // Prefix decrement template -typename return_type_helper::type operator--(T &a) { +typename return_type_helper::type>::type operator--(T &&a) { return (a = a - 1); } // Postfix increment template -typename return_type_helper::type operator--(T &a, int) { +typename return_type_helper::type>::type operator--(T &&a, int) { return ((a = a - 1) + 1); } diff --git a/samples/outputs.var_names/sample42 b/samples/outputs.var_names/sample42 index 5eee6ef..2b51296 100644 --- a/samples/outputs.var_names/sample42 +++ b/samples/outputs.var_names/sample42 @@ -8,5 +8,6 @@ void bar (void) { y_0[0] = y_0[0] - x_2; y_0[x_2] = y_0[x_2] / 2; } + (y_0[0] = y_0[0] + 1) - 1; } diff --git a/samples/outputs.var_names/sample57 b/samples/outputs.var_names/sample57 new file mode 100644 index 0000000..182253d --- /dev/null +++ b/samples/outputs.var_names/sample57 @@ -0,0 +1,6 @@ +void bar (void) { + int x_0 = 0; + int* y_1 = (&(x_0)); + (y_1[0] = y_1[0] + 1) - 1; +} + diff --git a/samples/outputs/sample42 b/samples/outputs/sample42 index f438a06..5f3dd65 100644 --- a/samples/outputs/sample42 +++ b/samples/outputs/sample42 @@ -8,5 +8,6 @@ void bar (void) { var0[0] = var0[0] - var2; var0[var2] = var0[var2] / 2; } + (var0[0] = var0[0] + 1) - 1; } diff --git a/samples/outputs/sample57 b/samples/outputs/sample57 new file mode 100644 index 0000000..62b48df --- /dev/null +++ b/samples/outputs/sample57 @@ -0,0 +1,6 @@ +void bar (void) { + int var0 = 0; + int* var1 = (&(var0)); + (var1[0] = var1[0] + 1) - 1; +} + diff --git a/samples/sample42.cpp b/samples/sample42.cpp index 97df989..6e85991 100644 --- a/samples/sample42.cpp +++ b/samples/sample42.cpp @@ -20,6 +20,8 @@ static void bar(void) { *y -= x; y[x] /= 2; } + + (*y)++; } int main(int argc, char *argv[]) { diff --git a/samples/sample57.cpp b/samples/sample57.cpp new file mode 100644 index 0000000..05f11eb --- /dev/null +++ b/samples/sample57.cpp @@ -0,0 +1,31 @@ +// Include the headers +#include "blocks/c_code_generator.h" +#include "builder/static_var.h" +#include "builder/dyn_var.h" +#include "blocks/rce.h" +#include + +// Include the BuildIt types +using builder::dyn_var; +using builder::static_var; + +static void bar(void) { + // Insert code to stage here + + dyn_var x = 0; + dyn_var y = &x; + + (*y)++; + + +} + +int main(int argc, char* argv[]) { + builder::builder_context context; + auto ast = context.extract_function_ast(bar, "bar"); + block::eliminate_redundant_vars(ast); + block::c_code_generator::generate_code(ast, std::cout, 0); + return 0; +} + + diff --git a/src/blocks/rce.cpp b/src/blocks/rce.cpp index 93f80c9..ab3a0e4 100644 --- a/src/blocks/rce.cpp +++ b/src/blocks/rce.cpp @@ -30,6 +30,16 @@ class var_use_counter : public block_visitor { if (std::find(assigned_vars.begin(), assigned_vars.end(), v) == assigned_vars.end()) assigned_vars.push_back(v); } + virtual void visit(addr_of_expr::Ptr e) override { + e->expr1->accept(this); + if (!isa(e->expr1)) + return; + var_expr::Ptr ve = to(e->expr1); + var::Ptr v = ve->var1; + // Variables that have their addresses taken can potentially be assigned and hence should not be copy eliminated + if (std::find(assigned_vars.begin(), assigned_vars.end(), v) == assigned_vars.end()) + assigned_vars.push_back(v); + } }; class check_side_effects : public block_visitor {