// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/dominant.cpp
// Function: ComputeDominance::ComputeIDomInfo(IRFunction*)
void ComputeDominance::ComputeIDomInfo(IRFunction* f) {
  // 构造深度优先生成树 构造完后 保证vertex dfnum有效完整 parent[r]为空
  DFS(nullptr, f->bb_list_.front());
  // std::cout << N << " " << f->bb_list_.size() << std::endl;
  // MyAssert(N == f->bb_list_.size());

  // 基于半必经结点定理计算所有 非根节点 的半必经结点
  for (int i = N - 1; i > 0; --i) {
    BB* n = vertex[i];
    auto p = parent[n];
    MyAssert(nullptr != p);
    auto s = p;  // s即n的半必经结点
    for (auto pred : n->pred_) {
      BB* s_temp = nullptr;
      if (dfnum[pred] <= dfnum[n]) {  // 该前驱在生成树上是n的祖先
        s_temp = pred;                // 则该结点是semi[n]的候选
      } else {                        // 该前驱在生成树上不是n的祖先
        s_temp = semi[AncestorWithLowestSemi(pred)];
        // 则该前驱到其公共祖先的路径上的y:min(dfnum[semi[y]]) semi[y]成为semi[n]候选
      }
      if (dfnum[s_temp] < dfnum[s]) {  // 半必经结点是dfnum最小的
        s = s_temp;
      }
    }
    semi.insert({n, s});  // semi和bucket和ancestor中不会有key为root的pair
    bucket[s].insert(n);
    Link(p, n);  // 把p->n链入到生成树森林

    // 计算 以p为半必经结点的 结点v 的 必经结点 这里可以保证会把semi为root结点的结点的idom都计算到
    for (auto v : bucket[p]) {
      BB* y = AncestorWithLowestSemi(v);  // min(dfnum[semi[y]]) y: from p to v(included). dfnum[v]>dfnum[y].
      if (semi[y] == p /*semi[v]*/) {     // 没有旁路p的分支
        // idom.insert({v, p});
        v->idom_ = p;
        p->doms_.push_back(v);
      } else {
        samedom.insert({v, y});  // fill idom[samedom.key]=idom[samedom.value] later.
      }
    }
    bucket[p].clear();
  }

  // fill idom[samedom.key]=idom[samedom.value]
  for (int i = 1; i < N; ++i) {
    auto n = vertex[i];
    if (samedom.find(n) != samedom.end()) {
      auto idom = samedom[n]->idom_;
      // MyAssert(idom.find(samedom[n]) != idom.end());
      // idom.insert({n, idom[samedom[n]]});
      MyAssert(nullptr != idom);
      n->idom_ = idom;
      idom->doms_.push_back(n);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x30(%rsi), %rax
movq (%rax), %rdx
xorl %esi, %esi
callq 0x3b29c
movl 0x34(%rbx), %ecx
leaq 0x88(%rbx), %rax
movq %rax, 0x48(%rsp)
leaq 0x50(%rbx), %r12
leaq 0x130(%rbx), %r13
leaq 0x1a0(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x168(%rbx), %rax
movq %rax, 0x38(%rsp)
cmpl $0x1, %ecx
jle 0x3b7f6
decq %rcx
movq 0x38(%rbx), %rax
movq %rcx, 0x50(%rsp)
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3c1a4
movq (%rax), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x3b872
movq %rax, 0x28(%rsp)
movq 0x8(%rsp), %rax
movq 0x28(%rax), %r14
movq 0x30(%rax), %r15
cmpq %r15, %r14
je 0x3b6f8
movq (%r14), %rax
movq %rax, 0x10(%rsp)
andq $0x0, (%rsp)
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3c276
movl (%rax), %ebp
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3c276
cmpl (%rax), %ebp
jle 0x3b6bf
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x3b390
movq %rax, 0x20(%rsp)
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3c5c8
movq (%rax), %rax
jmp 0x3b6c4
movq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %rsp, %rsi
callq 0x3c276
movl (%rax), %ebp
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3c276
cmpl (%rax), %ebp
jge 0x3b6ef
movq (%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x8, %r14
jmp 0x3b665
movq 0x8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3be2e
movq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3c69a
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x38a9a
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %rbx, %rdi
callq 0x3b344
movq %r14, %rdi
leaq 0x30(%rsp), %rsi
callq 0x3c69a
movq %rax, %r14
addq $0x10, %r14
movq 0x38(%rsp), %r15
movq (%r14), %r14
testq %r14, %r14
je 0x3b7d5
movq 0x8(%r14), %rsi
movq %rsi, (%rsp)
movq %rbx, %rdi
callq 0x3b390
movq %rax, 0x20(%rsp)
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3c1a4
movq 0x30(%rsp), %rdi
cmpq %rdi, (%rax)
je 0x3b7b9
movq (%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x3be2e
jmp 0x3b763
movq (%rsp), %rax
movq %rdi, 0x250(%rax)
addq $0x258, %rdi            # imm = 0x258
movq %rsp, %rsi
callq 0xdd4a
jmp 0x3b763
movq 0x40(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x3c69a
movq %rax, %rdi
callq 0x1fe48
movq 0x50(%rsp), %rcx
jmp 0x3b615
pushq $0x1
popq %r15
leaq 0x10(%rsp), %r14
movslq 0x34(%rbx), %rax
cmpq %rax, %r15
jge 0x3b863
movq 0x38(%rbx), %rax
movq (%rax,%r15,8), %rax
movq %rax, 0x10(%rsp)
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x3c186
testq %rax, %rax
je 0x3b85e
movq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x3c1a4
movq (%rax), %rax
movq 0x250(%rax), %rdi
testq %rdi, %rdi
je 0x3b8a7
movq 0x10(%rsp), %rax
movq %rdi, 0x250(%rax)
addq $0x258, %rdi            # imm = 0x258
movq %r14, %rsi
callq 0xdd4a
incq %r15
jmp 0x3b7ff
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4576f(%rip), %rdi     # 0x80fe8
leaq 0x1a868(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x1defa(%rip), %rsi     # 0x59786
movq %rax, %rdi
callq 0x8240
leaq 0x1ba85(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x42
jmp 0x3b8da
movq 0x4573a(%rip), %rdi     # 0x80fe8
leaq 0x1a833(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x1dec5(%rip), %rsi     # 0x59786
movq %rax, %rdi
callq 0x8240
leaq 0x1ba50(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x69
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
nop