dead_code_elimination.cpp 831 Bytes
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6
7
8
9
10
#include <migraph/dead_code_elimination.hpp>
#include <migraph/program.hpp>
#include <migraph/instruction.hpp>
#include <migraph/iterator_for.hpp>
#include <migraph/functional.hpp>

namespace migraph {

void dead_code_elimination::apply(program& p) const
{
11
12
    auto last = std::prev(p.end());
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
13
14
    {
        // Skip the last instruction
15
        if(ins == last)
Paul's avatar
Paul committed
16
            break;
17
18
19
        fix([&](auto self, auto leaf) {
            assert(p.has_instruction(leaf));
            if(leaf->output.empty())
Paul's avatar
Paul committed
20
            {
21
22
                auto args = leaf->arguments;
                p.move_instruction(leaf, p.end());
Paul's avatar
Paul committed
23
                for(auto arg : args)
Paul's avatar
Paul committed
24
25
                    self(arg);
            }
26
        })(ins);
Paul's avatar
Paul committed
27
    }
28
    p.remove_instructions(std::next(last), p.end());
Paul's avatar
Paul committed
29
30
31
}

} // namespace migraph