• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 17656823807

11 Sep 2025 08:42PM UTC coverage: 60.447% (+1.1%) from 59.335%
17656823807

Pull #219

github

web-flow
Merge d5416236f into 6c1992b40
Pull Request #219: stdlib Library Nodes and ConstantNodes

460 of 1635 new or added lines in 81 files covered. (28.13%)

93 existing lines in 35 files now uncovered.

9385 of 15526 relevant lines covered (60.45%)

107.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.54
/src/passes/structured_control_flow/dead_cfg_elimination.cpp
1
#include "sdfg/passes/structured_control_flow/dead_cfg_elimination.h"
2

3
#include "sdfg/structured_control_flow/structured_loop.h"
4

5
namespace sdfg {
6
namespace passes {
7

8
bool DeadCFGElimination::is_dead(const structured_control_flow::ControlFlowNode& node) {
15✔
9
    if (auto block_stmt = dynamic_cast<const structured_control_flow::Block*>(&node)) {
15✔
10
        return (block_stmt->dataflow().nodes().size() == 0);
8✔
11
    } else if (auto sequence_stmt = dynamic_cast<const structured_control_flow::Sequence*>(&node)) {
7✔
12
        return (sequence_stmt->size() == 0);
×
13
    } else if (auto if_else_stmt = dynamic_cast<const structured_control_flow::IfElse*>(&node)) {
7✔
14
        return (if_else_stmt->size() == 0);
×
15
    } else if (auto while_stmt = dynamic_cast<const structured_control_flow::While*>(&node)) {
7✔
16
        return is_dead(while_stmt->root());
×
17
    } else if (dynamic_cast<const structured_control_flow::For*>(&node)) {
7✔
18
        return false;
7✔
19
    }
20

21
    return false;
×
22
};
15✔
23

24
DeadCFGElimination::DeadCFGElimination()
5✔
25
    : Pass() {
5✔
26

27
      };
5✔
28

29
std::string DeadCFGElimination::name() { return "DeadCFGElimination"; };
×
30

31
bool DeadCFGElimination::run_pass(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
4✔
32
    bool applied = false;
4✔
33

34
    auto& sdfg = builder.subject();
4✔
35

36
    auto& root = sdfg.root();
4✔
37
    if (root.size() == 0) {
4✔
38
        return false;
×
39
    }
40
    auto last = root.at(root.size() - 1);
4✔
41
    if (last.second.empty()) {
4✔
42
        if (auto return_node = dynamic_cast<structured_control_flow::Return*>(&last.first)) {
3✔
NEW
43
            if (!return_node->has_data()) {
×
NEW
44
                builder.remove_child(root, root.size() - 1);
×
NEW
45
                return true;
×
46
            }
NEW
47
        }
×
48
    }
3✔
49

50
    std::list<structured_control_flow::ControlFlowNode*> queue = {&sdfg.root()};
4✔
51
    while (!queue.empty()) {
32✔
52
        auto curr = queue.front();
28✔
53
        queue.pop_front();
28✔
54

55
        if (auto sequence_stmt = dynamic_cast<structured_control_flow::Sequence*>(curr)) {
28✔
56
            // Simplify
57
            size_t i = 0;
12✔
58
            while (i < sequence_stmt->size()) {
28✔
59
                auto child = sequence_stmt->at(i);
16✔
60
                if (!child.second.empty()) {
16✔
61
                    i++;
1✔
62
                    continue;
1✔
63
                }
64

65
                // Dead
66
                if (is_dead(child.first)) {
15✔
67
                    builder.remove_child(*sequence_stmt, i);
×
68
                    applied = true;
×
69
                    continue;
×
70
                }
71

72
                // Trivial branch
73
                if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(&child.first)) {
15✔
74
                    auto branch = if_else_stmt->at(0);
×
75
                    if (symbolic::is_true(branch.second)) {
×
76
                        builder.move_children(branch.first, *sequence_stmt, i + 1);
×
77
                        builder.remove_child(*sequence_stmt, i);
×
78
                        applied = true;
×
79
                        continue;
×
80
                    }
81
                }
×
82

83
                i++;
15✔
84
            }
85

86
            // Add to queue
87
            for (size_t j = 0; j < sequence_stmt->size(); j++) {
28✔
88
                queue.push_back(&sequence_stmt->at(j).first);
16✔
89
            }
16✔
90
        } else if (auto if_else_stmt = dynamic_cast<structured_control_flow::IfElse*>(curr)) {
28✔
91
            // False branches are safe to remove
92
            size_t i = 0;
×
93
            while (i < if_else_stmt->size()) {
×
94
                auto child = if_else_stmt->at(i);
×
95
                if (symbolic::is_false(child.second)) {
×
96
                    builder.remove_case(*if_else_stmt, i);
×
97
                    applied = true;
×
98
                    continue;
×
99
                }
100

101
                i++;
×
102
            }
103

104
            // Trailing dead branches are safe to remove
105
            if (if_else_stmt->size() > 0) {
×
106
                if (is_dead(if_else_stmt->at(if_else_stmt->size() - 1).first)) {
×
107
                    builder.remove_case(*if_else_stmt, if_else_stmt->size() - 1);
×
108
                    applied = true;
×
109
                }
×
110
            }
×
111

112
            // If-else to simple if conversion
113
            if (if_else_stmt->size() == 2) {
×
114
                auto if_condition = if_else_stmt->at(0).second;
×
115
                auto else_condition = if_else_stmt->at(1).second;
×
116
                if (symbolic::eq(if_condition->logical_not(), else_condition)) {
×
117
                    if (is_dead(if_else_stmt->at(1).first)) {
×
118
                        builder.remove_case(*if_else_stmt, 1);
×
119
                        applied = true;
×
120
                    } else if (is_dead(if_else_stmt->at(0).first)) {
×
121
                        builder.remove_case(*if_else_stmt, 0);
×
122
                        applied = true;
×
123
                    }
×
124
                }
×
125
            }
×
126

127
            // Add to queue
128
            for (size_t j = 0; j < if_else_stmt->size(); j++) {
×
129
                queue.push_back(&if_else_stmt->at(j).first);
×
130
            }
×
131
        } else if (auto loop_stmt = dynamic_cast<structured_control_flow::While*>(curr)) {
16✔
132
            auto& root = loop_stmt->root();
×
133
            queue.push_back(&root);
×
134
        } else if (auto sloop_stmt = dynamic_cast<structured_control_flow::StructuredLoop*>(curr)) {
16✔
135
            auto& root = sloop_stmt->root();
8✔
136
            queue.push_back(&root);
8✔
137
        } else if (auto map_stmt = dynamic_cast<structured_control_flow::Map*>(curr)) {
16✔
138
            auto& root = map_stmt->root();
×
139
            queue.push_back(&root);
×
140
        }
×
141
    }
142

143
    return applied;
4✔
144
};
4✔
145

146
} // namespace passes
147
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc