DeadCode.hpp 758 Bytes
Newer Older
刘睿博's avatar
刘睿博 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#pragma once

#include "FuncInfo.hpp"
#include "PassManager.hpp"

#include <unordered_set>

/**
 * 死代码消除:参见
 *https://www.clear.rice.edu/comp512/Lectures/10Dead-Clean-SCCP.pdf
 **/
class DeadCode : public Pass {
  public:
    DeadCode(Module *m) : Pass(m), func_info(std::make_shared<FuncInfo>(m)) {}

    void run();

  private:
    std::shared_ptr<FuncInfo> func_info;
    int ins_count{0}; // 用以衡量死代码消除的性能
    std::deque<Instruction *> work_list{};
    std::unordered_map<Instruction *, bool> marked{};

    void mark(Function *func);
    void mark(Instruction *ins);
    bool sweep(Function *func);
    bool clear_basic_blocks(Function *func);
    bool is_critical(Instruction *ins);
    void sweep_globally();
};