|
1 | 1 | #include "blocks/loops.h" |
| 2 | +#include <unordered_set> |
2 | 3 | #include <algorithm> |
3 | 4 | #include <tuple> |
4 | 5 | #include <set> |
@@ -700,23 +701,218 @@ stmt::Ptr loop::convert_to_ast_impl(dominator_analysis &dta_) { |
700 | 701 | return to<stmt>(while_block); |
701 | 702 | } |
702 | 703 |
|
| 704 | +std::map<stmt_block::Ptr, stmt_block::Ptr> ast_parent_map_global; |
703 | 705 | block::stmt_block::Ptr loop_info::convert_to_ast(block::stmt_block::Ptr ast) { |
704 | | - |
705 | 706 | block::stmt_block::Ptr return_ast = std::make_shared<stmt_block>(); |
706 | | - |
707 | | - for (auto bb: dta.cfg_) { |
| 707 | + |
| 708 | + std::deque<std::pair<std::shared_ptr<basic_block>, stmt_block::Ptr>> worklist; |
| 709 | + std::unordered_set<std::shared_ptr<basic_block>> visited; |
| 710 | + worklist.push_back({dta.cfg_[0], return_ast}); |
| 711 | + visited.insert(dta.cfg_[0]); |
| 712 | + |
| 713 | + while (worklist.size()) { |
| 714 | + auto bb_ast_pair = worklist.front(); |
| 715 | + auto bb = bb_ast_pair.first; |
| 716 | + auto ast = bb_ast_pair.second; |
| 717 | + worklist.pop_front(); |
| 718 | + |
708 | 719 | if (isa<label_stmt>(bb->parent)) { |
709 | | - for (auto loop: top_level_loops) { |
710 | | - if (loop->header_block->parent == bb->parent) |
711 | | - return_ast->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 720 | + for (auto loop : top_level_loops) { |
| 721 | + if (loop->header_block->parent == bb->parent) { |
| 722 | + ast->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 723 | + std::cerr << "found loop\n"; |
| 724 | + break; |
| 725 | + } |
712 | 726 | } |
713 | 727 | } |
714 | | - else if (!bb_loop_map.count(bb->id) && !visited_blocks.count(bb->parent) && !bb->is_exit_block) { |
715 | | - return_ast->stmts.push_back(bb->parent); |
| 728 | + else if (isa<if_stmt>(bb->parent)) { |
| 729 | + |
| 730 | + if (bb_loop_map.count(bb->id)) |
| 731 | + continue; |
| 732 | + |
| 733 | + if (visited_blocks.count(bb->parent)) |
| 734 | + continue; |
| 735 | + |
| 736 | + if_stmt::Ptr if_stmt_copy = std::make_shared<if_stmt>(); |
| 737 | + if_stmt_copy->then_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 738 | + if_stmt_copy->else_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 739 | + if_stmt_copy->cond = to<if_stmt>(bb->parent)->cond; |
| 740 | + |
| 741 | + // push the then branch onto worklist. (worklist should be a pair <processed, destination>) ? |
| 742 | + if (bb->then_branch) { |
| 743 | + ast_parent_map_global[to<stmt_block>(if_stmt_copy->then_stmt)] = ast; |
| 744 | + worklist.push_back({bb->then_branch, to<stmt_block>(if_stmt_copy->then_stmt)}); |
| 745 | + visited.insert(bb->then_branch); |
| 746 | + } |
| 747 | + |
| 748 | + if (bb->else_branch) { |
| 749 | + ast_parent_map_global[to<stmt_block>(if_stmt_copy->else_stmt)] = ast; |
| 750 | + worklist.push_back({bb->else_branch, to<stmt_block>(if_stmt_copy->else_stmt)}); |
| 751 | + visited.insert(bb->else_branch); |
| 752 | + } |
| 753 | + |
| 754 | + ast->stmts.push_back(to<stmt>(if_stmt_copy)); |
| 755 | + } |
| 756 | + else { |
| 757 | + assert(bb->successor.size() <= 1); |
| 758 | + |
| 759 | + if (bb_loop_map.count(bb->id)) |
| 760 | + continue; |
| 761 | + |
| 762 | + if (visited_blocks.count(bb->parent)) |
| 763 | + continue; |
| 764 | + |
| 765 | + // what is happening is that when we see a if stmt exit block we should |
| 766 | + // reduce the level of the tree, it is not being done now. |
| 767 | + if (!bb->is_exit_block && !isa<stmt_block>(bb->parent)) |
| 768 | + ast->stmts.push_back(to<stmt>(bb->parent)); |
| 769 | + |
| 770 | + if (bb->successor.size()) { |
| 771 | + if (visited.count(bb->successor[0])) |
| 772 | + continue; |
| 773 | + else |
| 774 | + visited.insert(bb->successor[0]); |
| 775 | + |
| 776 | + if (bb->is_exit_block) |
| 777 | + worklist.push_back({bb->successor[0], ast_parent_map_global[ast]}); |
| 778 | + else |
| 779 | + worklist.push_back({bb->successor[0], ast}); |
| 780 | + } |
716 | 781 | } |
717 | 782 | } |
718 | | - // for (auto loop: top_level_loops) { |
719 | | - // return_ast->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 783 | + // // iterate using preorder bb |
| 784 | + // // use stack for current parent block |
| 785 | + // std::stack<stmt::Ptr> parent_stack; |
| 786 | + // parent_stack.push(to<stmt>(return_ast)); |
| 787 | + |
| 788 | + // for (unsigned int i = 0; i < dta.get_preorder().size(); i++) { |
| 789 | + // auto bb = dta.cfg_[dta.get_preorder()[i]]; |
| 790 | + |
| 791 | + // std::cerr << "bb: " << bb->id << "\n"; |
| 792 | + // if (isa<label_stmt>(bb->parent)) { |
| 793 | + // std::cerr << "inside label block\n"; |
| 794 | + |
| 795 | + // for (auto loop: top_level_loops) { |
| 796 | + // if (loop->header_block->parent == bb->parent) { |
| 797 | + // to<stmt_block>(parent_stack.top())->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 798 | + // break; |
| 799 | + // } |
| 800 | + // } |
| 801 | + // } |
| 802 | + // else if (isa<if_stmt>(bb->parent)) { |
| 803 | + // std::cerr << "inside if block\n"; |
| 804 | + |
| 805 | + // if (bb_loop_map.count(bb->id)) |
| 806 | + // continue; |
| 807 | + // std::cerr << "inside if block (exit 1)\n"; |
| 808 | + |
| 809 | + // if (visited_blocks.count(bb->parent)) |
| 810 | + // continue; |
| 811 | + // std::cerr << "inside if block (exit 2)\n"; |
| 812 | + |
| 813 | + // if (dta.get_preorder().size() <= i + 1) |
| 814 | + // continue; |
| 815 | + // std::cerr << "inside if block (exit 3)\n"; |
| 816 | + |
| 817 | + // if_stmt::Ptr if_stmt_copy = std::make_shared<if_stmt>(); |
| 818 | + // if_stmt_copy->then_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 819 | + // if_stmt_copy->else_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 820 | + // if_stmt_copy->cond = to<if_stmt>(bb->parent)->cond; |
| 821 | + |
| 822 | + // int next_block = -1; |
| 823 | + // if (dta.get_preorder()[i + 1] == (int)bb->then_branch->id) |
| 824 | + // next_block = 0; |
| 825 | + // else if (dta.get_preorder()[i + 1] == (int)bb->else_branch->id) |
| 826 | + // next_block = 1; |
| 827 | + |
| 828 | + // assert(next_block != -1); |
| 829 | + |
| 830 | + // to<stmt_block>(parent_stack.top())->stmts.push_back(if_stmt_copy); |
| 831 | + // if (next_block == 0) { |
| 832 | + // parent_stack.push(to<stmt>(if_stmt_copy->then_stmt)); |
| 833 | + // } |
| 834 | + // else if (next_block == 1) { |
| 835 | + // parent_stack.push(to<stmt>(if_stmt_copy->else_stmt)); |
| 836 | + // } |
| 837 | + |
| 838 | + // } |
| 839 | + // else if (bb->is_exit_block) { |
| 840 | + // std::cerr << "inside exit block\n"; |
| 841 | + |
| 842 | + // if (bb_loop_map.count(bb->id)) |
| 843 | + // continue; |
| 844 | + // std::cerr << "inside exit block (exit 1)\n"; |
| 845 | + |
| 846 | + // if (visited_blocks.count(bb->parent)) |
| 847 | + // continue; |
| 848 | + |
| 849 | + // parent_stack.pop(); |
| 850 | + // } |
| 851 | + // else { |
| 852 | + // std::cerr << "inside default block\n"; |
| 853 | + |
| 854 | + // if (bb_loop_map.count(bb->id)) |
| 855 | + // continue; |
| 856 | + // std::cerr << "inside default block (exit 1)\n"; |
| 857 | + |
| 858 | + // if (visited_blocks.count(bb->parent)) |
| 859 | + // continue; |
| 860 | + // std::cerr << "inside default block (exit 2)\n"; |
| 861 | + |
| 862 | + // to<stmt_block>(parent_stack.top())->stmts.push_back(bb->parent); |
| 863 | + // } |
| 864 | + // } |
| 865 | + |
| 866 | + // for (auto bb: dta.cfg_) { |
| 867 | + // std::cerr << bb->id << " " << bb_loop_map.count(bb->id) << " " << visited_blocks.count(bb->parent) << " " << bb->is_exit_block << "\n"; |
| 868 | + // if (isa<label_stmt>(bb->parent)) { |
| 869 | + // std::cerr << "inside label block\n"; |
| 870 | + // for (auto loop: top_level_loops) { |
| 871 | + // if (loop->header_block->parent == bb->parent) { |
| 872 | + // if (ast_parent_map_global.count(bb->parent)) { |
| 873 | + // ast_parent_map_global[bb->parent]->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 874 | + // } |
| 875 | + // else { |
| 876 | + // return_ast->stmts.push_back(loop->convert_to_ast_impl(dta)); |
| 877 | + // } |
| 878 | + // } |
| 879 | + // } |
| 880 | + // } |
| 881 | + // else if (!bb_loop_map.count(bb->id) && !visited_blocks.count(bb->parent) && !bb->is_exit_block) { |
| 882 | + // std::cerr << "inside if block\n"; |
| 883 | + // stmt::Ptr push_block = bb->parent; |
| 884 | + |
| 885 | + // if (isa<if_stmt>(bb->parent)) { |
| 886 | + // if_stmt::Ptr if_stmt_copy = std::make_shared<if_stmt>(); |
| 887 | + // if_stmt_copy->then_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 888 | + // if_stmt_copy->else_stmt = to<stmt>(std::make_shared<stmt_block>()); |
| 889 | + // if_stmt_copy->cond = to<if_stmt>(bb->parent)->cond; |
| 890 | + // push_block = to<stmt>(if_stmt_copy); |
| 891 | + |
| 892 | + // for (auto stmt: to<stmt_block>(to<if_stmt>(bb->parent)->then_stmt)->stmts) { |
| 893 | + // if (!isa<label_stmt>(stmt)) { |
| 894 | + // std::cerr << "ifstmt\n"; |
| 895 | + // visited_blocks.insert(stmt); |
| 896 | + // to<stmt_block>(if_stmt_copy->then_stmt)->stmts.push_back(stmt); |
| 897 | + // } |
| 898 | + // ast_parent_map_global.insert({stmt, to<stmt_block>(if_stmt_copy->then_stmt)}); |
| 899 | + // } |
| 900 | + // for (auto stmt: to<stmt_block>(to<if_stmt>(bb->parent)->else_stmt)->stmts) { |
| 901 | + // if (!isa<label_stmt>(stmt)) { |
| 902 | + // std::cerr << "elsestmt\n"; |
| 903 | + // visited_blocks.insert(stmt); |
| 904 | + // to<stmt_block>(if_stmt_copy->else_stmt)->stmts.push_back(stmt); |
| 905 | + // } |
| 906 | + // ast_parent_map_global.insert({stmt, to<stmt_block>(if_stmt_copy->else_stmt)}); |
| 907 | + // } |
| 908 | + // } |
| 909 | + // else if (isa<stmt_block>(bb->parent)) { |
| 910 | + // continue; |
| 911 | + // } |
| 912 | + |
| 913 | + // visited_blocks.insert(bb->parent); |
| 914 | + // return_ast->stmts.push_back(push_block); |
| 915 | + // } |
720 | 916 | // } |
721 | 917 |
|
722 | 918 | return return_ast; |
|
0 commit comments