<commit_before>// Copyright (c) 2014 <NAME>
// See LICENSE for details

#include <algorithm>
#include "ssa.h"

namespace hcc { namespace ssa {

bool prelive(const instruction& instr)
{
    switch (instr.type) {
    case instruction_type::JUMP:
    case instruction_type::BRANCH:
    case instruction_type::CALL:
    case instruction_type::RETURN:
    case instruction_type::LOAD:
    case instruction_type::STORE:
        return true;
    default:
        return false;
    }
}

void subroutine::dead_code_elimination()
{
    recompute_control_flow_graph();

    std::stack<std::pair<instruction_list::iterator, int>> worklist; // instruction and it's block
    for (auto node = 0; node < static_cast<int>(nodes.size()); ++node) {
        for (auto instruction = nodes[node].begin(), e = nodes[node].end(); instruction != e; ++instruction) {
            instruction->mark = false;
            if (prelive(*instruction)) {
                instruction->mark = true;
                worklist.push(std::make_pair(instruction, node));
            }
        }
    }

    while (!worklist.empty()) {
        auto p = worklist.top();
        auto o = p.first;
        worklist.pop();

        // for each definer...
        std::set<std::string> used_variables;
        o->use_apply([&](std::string& s) { used_variables.insert(s); });
        for (const auto& v : used_variables) {
            for (auto node = 0; node < static_cast<int>(nodes.size()); ++node) {
                for (auto instruction = nodes[node].begin(), e = nodes[node].end(); instruction != e; ++instruction) {
                    bool assigned = false;
                    instruction->def_apply([&] (std::string& s) { assigned = assigned || (s == v); });
                    if (assigned) {
                        //... maybe set live and append to worklist
                        if (!instruction->mark) {
                            instruction->mark = true;;
                            worklist.push(std::make_pair(instruction, node));
                        }
                    }
                }
            }
        }


        // for each y in r_dfs(x)
        for (int b : reverse_dominance->dfs[p.second]) {
            auto last = --nodes[b].end();
            if (!last->mark) {
                last->mark = true;
                worklist.push(std::make_pair(last, b));
            }
        }
    }

    // sweep
    for (auto i = instructions.begin(), e = instructions.end(); i != e;) {
        if (i->type != instruction_type::LABEL && !i->mark) {
            i = instructions.erase(i);
        } else {
            ++i;
        }
    }
}

}} // namespace hcc::ssa
<commit_msg>Simplified dead code elimination<commit_after>// Copyright (c) 2014 <NAME>
// See LICENSE for details

#include <algorithm>
#include "ssa.h"

namespace hcc { namespace ssa {

bool prelive(const instruction& instr)
{
    switch (instr.type) {
    case instruction_type::JUMP:
    case instruction_type::BRANCH:
    case instruction_type::CALL:
    case instruction_type::RETURN:
    case instruction_type::LOAD:
    case instruction_type::STORE:
        return true;
    default:
        return false;
    }
}

void subroutine::dead_code_elimination()
{
    recompute_control_flow_graph();

    std::stack<std::pair<instruction_list::iterator, int>> worklist; // instruction and its block
    for (auto node = 0; node < static_cast<int>(nodes.size()); ++node) {
        for (auto instruction = nodes[node].begin(), e = nodes[node].end(); instruction != e; ++instruction) {
            instruction->mark = false;
            if (prelive(*instruction)) {
                instruction->mark = true;
                worklist.push(std::make_pair(instruction, node));
            }
        }
    }

    while (!worklist.empty()) {
        auto w = worklist.top();
        worklist.pop();

        // for each use...
        w.first->use_apply([&] (std::string& use) {
            // ...find the definer...
            for (auto node = 0; node < static_cast<int>(nodes.size()); ++node) {
                for (auto instruction = nodes[node].begin(), e = nodes[node].end(); instruction != e; ++instruction) {
                    instruction->def_apply([&] (std::string& def) {
                        if (def == use && !instruction->mark) {
                            // ... mark and append to worklist
                            instruction->mark = true;;
                            worklist.push(std::make_pair(instruction, node));
                        }
                    });
                }
            }
        });

        // for each y in r_dfs(x)
        for (int b : reverse_dominance->dfs[w.second]) {
            auto last = --nodes[b].end();
            if (!last->mark) {
                last->mark = true;
                worklist.push(std::make_pair(last, b));
            }
        }
    }

    // sweep
    for (auto i = instructions.begin(), e = instructions.end(); i != e;) {
        if (i->type != instruction_type::LABEL && !i->mark) {
            i = instructions.erase(i);
        } else {
            ++i;
        }
    }
}

}} // namespace hcc::ssa

{'fraction_non_alphanumeric': 0.14519268197742313, 'fraction_numerical': 0.0023355391202802647, 'mean_word_length': 1.7688577586206897, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6179889', 'n_tokens_mistral': 1497, 'n_tokens_neox': 1438, 'n_words': 335}