#pragma once

#include <vector>
#include <map>
#include <unordered_map>

#include "CFGraph.h"

// Lengauer, Thomas. A fast algorithm for finding dominators in a flowgraph / Thomas Lengauer, Robert Endre Tarjan 
// ACM Transactions on Programming Languages and Systems (TOPLAS). — 1979. — Vol. 1, no. 1. — Pp. 121–141.

namespace SAPFOR {

class BasicBlock;

class DominatorFinder {
private:
    BasicBlock* entry;
    std::vector<BasicBlock*> vertices;
    std::unordered_map<BasicBlock*, int> dfs_num;
    std::vector<int> parent, semi, vertex, ancestor, label;
    std::vector<std::vector<int>> bucket;
    int n;

    void DFS(BasicBlock* v, int parent_num);
    void Compress(int v);
    int  Eval(int v);
    void Link(int v, int w);

public:
    DominatorFinder(std::vector<BasicBlock*>& blocks);
};

void buildDominatorTree(std::vector<BasicBlock*>& blocks);
}
