summaryrefslogtreecommitdiff
path: root/test/example/relooper-merge7.c
diff options
context:
space:
mode:
authorhobby8 <hobby8@users.noreply.github.com>2019-06-07 17:31:07 +0000
committerAlon Zakai <azakai@google.com>2019-06-07 10:31:07 -0700
commit5f252c3a6c2129a3f86e00401806b74bc2f266df (patch)
tree3a8bf48591fca3a875859cf89e439eda214835fc /test/example/relooper-merge7.c
parent1578cec27d45cf5104480f533fb092e81f0dbeb5 (diff)
downloadbinaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.tar.gz
binaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.tar.bz2
binaryen-5f252c3a6c2129a3f86e00401806b74bc2f266df.zip
Fix bug and leak in relooper merge consecutive blocks (#2159)
Fixes in Relooper merge consecutive blocks: Entry block getting removed when it is part of a loop: bb1->AddBranchTo(bb2, nullptr); bb1->AddBranchTo(bb3, ...); bb2->AddBranchTo(bb1, nullptr); bb3->AddBranchTo(bb4, nullptr); relooper.AddBlock(bb1); relooper.AddBlock(bb2); relooper.AddBlock(bb3); relooper.AddBlock(bb4); relooper.Calculate(bb1); Branches memory leak
Diffstat (limited to 'test/example/relooper-merge7.c')
-rw-r--r--test/example/relooper-merge7.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/example/relooper-merge7.c b/test/example/relooper-merge7.c
new file mode 100644
index 000000000..c942e3b25
--- /dev/null
+++ b/test/example/relooper-merge7.c
@@ -0,0 +1,49 @@
+
+#include <assert.h>
+#include <stdio.h>
+
+#include "binaryen-c.h"
+
+int main() {
+ BinaryenModuleRef module = BinaryenModuleCreate();
+
+ RelooperRef relooper = RelooperCreate(module);
+
+ // Create the basic blocks
+ RelooperBlockRef b[4];
+ int hasTerminator[4] = {0, 0, 0, 1};
+
+ int numBlocks = sizeof(b) / sizeof(RelooperBlockRef);
+ assert(sizeof(hasTerminator) / sizeof(int) == numBlocks);
+ int i;
+ for (i = 0; i < numBlocks; i++) {
+ BinaryenExpressionRef args[] = {
+ BinaryenConst(module, BinaryenLiteralInt32(i))};
+ BinaryenExpressionRef list[] = {
+ BinaryenCall(module, "print", args, 1, BinaryenTypeNone()),
+ BinaryenReturn(module, NULL) // relevant only if hasTerminator[i]
+ };
+ b[i] = RelooperAddBlock(
+ relooper,
+ BinaryenBlock(
+ module, NULL, list, 1 + hasTerminator[i], BinaryenTypeNone()));
+ }
+
+ // Create the branches.
+ // In this testcase, only b[2] and b[3] can be merged.
+ RelooperAddBranch(b[0], b[1], NULL, NULL);
+ RelooperAddBranch(
+ b[0], b[2], BinaryenConst(module, BinaryenLiteralInt32(-10)), NULL);
+ RelooperAddBranch(b[1], b[0], NULL, NULL);
+ RelooperAddBranch(b[2], b[3], NULL, NULL);
+
+ BinaryenExpressionRef all = RelooperRenderAndDispose(relooper, b[0], 1);
+
+ // Print it out
+ BinaryenExpressionPrint(all);
+
+ // Clean up the module, which owns all the objects we created above
+ BinaryenModuleDispose(module);
+
+ return 0;
+}