|
2 | 2 |
|
3 | 3 | namespace block { |
4 | 4 |
|
| 5 | + |
| 6 | +class continue_counter: public block_visitor { |
| 7 | +public: |
| 8 | + int count = 0; |
| 9 | + using block_visitor::visit; |
| 10 | + virtual void visit(continue_stmt::Ptr) { |
| 11 | + count++; |
| 12 | + } |
| 13 | + |
| 14 | + // Do not recurse for loops |
| 15 | + virtual void visit(while_stmt::Ptr) { |
| 16 | + return; |
| 17 | + } |
| 18 | + virtual void visit(for_stmt::Ptr) { |
| 19 | + return; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | +static stmt::Ptr fix_loop_inversion(if_stmt::Ptr ifs) { |
| 25 | + |
| 26 | + if (!isa<stmt_block>(ifs->then_stmt)) |
| 27 | + return ifs; |
| 28 | + |
| 29 | + auto then_block = to<stmt_block>(ifs->then_stmt); |
| 30 | + auto else_block = to<stmt_block>(ifs->else_stmt); |
| 31 | + |
| 32 | + if (then_block->stmts.size() != 1 || else_block->stmts.size() != 0) |
| 33 | + return ifs; |
| 34 | + |
| 35 | + if (!isa<while_stmt>(then_block->stmts[0])) |
| 36 | + return ifs; |
| 37 | + |
| 38 | + auto ws = to<while_stmt>(then_block->stmts[0]); |
| 39 | + |
| 40 | + if (!isa<int_const>(ws->cond)) |
| 41 | + return ifs; |
| 42 | + |
| 43 | + if (!(to<int_const>(ws->cond)->value == 1)) |
| 44 | + return ifs; |
| 45 | + |
| 46 | + if (!isa<stmt_block>(ws->body)) |
| 47 | + return ifs; |
| 48 | + |
| 49 | + if (to<stmt_block>(ws->body)->stmts.size() == 0) |
| 50 | + return ifs; |
| 51 | + |
| 52 | + auto last_stmt = to<stmt_block>(ws->body)->stmts.back(); |
| 53 | + if (!isa<if_stmt>(last_stmt)) |
| 54 | + return ifs; |
| 55 | + |
| 56 | + if (!isa<not_expr>(to<if_stmt>(last_stmt)->cond)) |
| 57 | + return ifs; |
| 58 | + if (!to<not_expr>(to<if_stmt>(last_stmt)->cond)->expr1->is_same(ifs->cond)) |
| 59 | + return ifs; |
| 60 | + |
| 61 | + auto nfs = to<if_stmt>(last_stmt); |
| 62 | + |
| 63 | + if (!isa<stmt_block>(nfs->then_stmt) || to<stmt_block>(nfs->then_stmt)->stmts.size() != 1) |
| 64 | + return ifs; |
| 65 | + if (!isa<stmt_block>(nfs->else_stmt) || to<stmt_block>(nfs->else_stmt)->stmts.size() != 0) |
| 66 | + return ifs; |
| 67 | + |
| 68 | + if (!isa<break_stmt>(to<stmt_block>(nfs->then_stmt)->stmts[0])) |
| 69 | + return ifs; |
| 70 | + |
| 71 | + // make sure ws has no continues |
| 72 | + continue_counter counter; |
| 73 | + ws->accept(&counter); |
| 74 | + if (counter.count != 0) |
| 75 | + return ifs; |
| 76 | + |
| 77 | + |
| 78 | + // everything looks good, time to patch |
| 79 | + ws->cond = ifs->cond; |
| 80 | + to<stmt_block>(ws->body)->stmts.pop_back(); |
| 81 | + |
| 82 | + return ws; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | + |
5 | 87 | void if_switcher::visit(if_stmt::Ptr ifs) { |
6 | 88 | if (isa<stmt_block>(ifs->then_stmt)) { |
7 | 89 | auto then_block = to<stmt_block>(ifs->then_stmt); |
8 | | - if (then_block->stmts.size() == 0) { |
| 90 | + auto else_block = to<stmt_block>(ifs->else_stmt); |
| 91 | + if (then_block->stmts.size() == 0 && else_block->stmts.size() != 0) { |
9 | 92 | ifs->then_stmt = ifs->else_stmt; |
10 | 93 | ifs->else_stmt = then_block; |
11 | 94 | auto ne = std::make_shared<not_expr>(); |
12 | 95 | ne->expr1 = ifs->cond; |
13 | 96 | ifs->cond = ne; |
14 | 97 | } |
| 98 | + |
15 | 99 | } |
16 | | - block_visitor::visit(ifs); |
| 100 | + block_replacer::visit(ifs); |
| 101 | + |
| 102 | + node = fix_loop_inversion(ifs); |
17 | 103 | } |
18 | 104 |
|
| 105 | + |
| 106 | + |
| 107 | + |
19 | 108 | } // namespace block |
0 commit comments