// File: 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:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x128(%rsp), %rdi
addq $0x30, %rdi
callq 0x8cc90
movq 0x30(%rsp), %rdi
movq (%rax), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0xa4f00
movq 0x30(%rsp), %rax
movl 0x34(%rax), %eax
subl $0x1, %eax
movl %eax, 0x124(%rsp)
cmpl $0x0, 0x124(%rsp)
jle 0xa5986
movq 0x30(%rsp), %rdi
addq $0x38, %rdi
movslq 0x124(%rsp), %rsi
callq 0x8ee50
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x118(%rsp)
addq $0x88, %rdi
leaq 0x118(%rsp), %rsi
callq 0xa6030
movq (%rax), %rax
movq %rax, 0x110(%rsp)
xorl %eax, %eax
cmpq 0x110(%rsp), %rax
jne 0xa55de
movq 0xa6a5a(%rip), %rdi     # 0x14bfe8
leaq 0x54b63(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x583a0(%rip), %rsi     # 0xfd944
callq 0x8330
movq %rax, %rdi
leaq 0x55e37(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0x42, %esi
callq 0x8650
movq %rax, %rdi
movq 0xa69b1(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
movq 0x110(%rsp), %rax
movq %rax, 0x108(%rsp)
movq 0x118(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rdi
callq 0x1cc40
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rdi
callq 0x1cac0
movq %rax, 0xf0(%rsp)
leaq 0xf8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xa564a
jmp 0xa5763
leaq 0xf8(%rsp), %rdi
callq 0x49320
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xe8(%rsp)
movq $0x0, 0xe0(%rsp)
addq $0x50, %rdi
leaq 0xe8(%rsp), %rsi
callq 0xa6060
movq 0x30(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x2c(%rsp)
addq $0x50, %rdi
leaq 0x118(%rsp), %rsi
callq 0xa6060
movq %rax, %rcx
movl 0x2c(%rsp), %eax
cmpl (%rcx), %eax
jg 0xa56bd
movq 0xe8(%rsp), %rax
movq %rax, 0xe0(%rsp)
jmp 0xa5702
movq 0x30(%rsp), %rdi
movq %rdi, %rax
addq $0x130, %rax            # imm = 0x130
movq %rax, 0x20(%rsp)
movq 0xe8(%rsp), %rsi
callq 0xa5160
movq 0x20(%rsp), %rdi
movq %rax, 0xd8(%rsp)
leaq 0xd8(%rsp), %rsi
callq 0xa6180
movq (%rax), %rax
movq %rax, 0xe0(%rsp)
movq 0x30(%rsp), %rdi
addq $0x50, %rdi
leaq 0xe0(%rsp), %rsi
callq 0xa6060
movq 0x30(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
addq $0x50, %rdi
leaq 0x108(%rsp), %rsi
callq 0xa6060
movq %rax, %rcx
movl 0x1c(%rsp), %eax
cmpl (%rcx), %eax
jge 0xa574f
movq 0xe0(%rsp), %rax
movq %rax, 0x108(%rsp)
jmp 0xa5751
leaq 0xf8(%rsp), %rdi
callq 0x49330
jmp 0xa562c
movq 0x30(%rsp), %rax
addq $0x130, %rax            # imm = 0x130
movq %rax, 0x10(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0xa5f80
movq 0x10(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xa5f30
movq 0x30(%rsp), %rdi
movq %rax, 0xb8(%rsp)
movb %dl, 0xc0(%rsp)
addq $0x1a0, %rdi            # imm = 0x1A0
leaq 0x108(%rsp), %rsi
callq 0xa61b0
movq %rax, %rdi
leaq 0x118(%rsp), %rsi
callq 0x8f780
movq 0x30(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movb %dl, 0xb0(%rsp)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
callq 0xa50c0
movq 0x30(%rsp), %rdi
addq $0x1a0, %rdi            # imm = 0x1A0
leaq 0x110(%rsp), %rsi
callq 0xa61b0
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x8d590
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x8d5c0
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x8d5f0
testb $0x1, %al
jne 0xa586c
jmp 0xa594f
leaq 0x98(%rsp), %rdi
callq 0x8d610
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0xa5160
movq 0x30(%rsp), %rdi
movq %rax, 0x80(%rsp)
addq $0x130, %rdi            # imm = 0x130
leaq 0x80(%rsp), %rsi
callq 0xa6030
movq (%rax), %rax
cmpq 0x110(%rsp), %rax
jne 0xa58f9
movq 0x110(%rsp), %rcx
movq 0x88(%rsp), %rax
movq %rcx, 0x250(%rax)
movq 0x110(%rsp), %rdi
addq $0x258, %rdi            # imm = 0x258
leaq 0x88(%rsp), %rsi
callq 0x186f0
jmp 0xa593b
movq 0x30(%rsp), %rax
addq $0x168, %rax            # imm = 0x168
movq %rax, 0x8(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0xa5f80
movq 0x8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xa5f30
movq %rax, 0x60(%rsp)
movb %dl, 0x68(%rsp)
jmp 0xa593d
leaq 0x98(%rsp), %rdi
callq 0x8d670
jmp 0xa584e
movq 0x30(%rsp), %rdi
addq $0x1a0, %rdi            # imm = 0x1A0
leaq 0x110(%rsp), %rsi
callq 0xa61b0
movq %rax, %rdi
callq 0xa61e0
movl 0x124(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0xa5528
movl $0x1, 0x5c(%rsp)
movq 0x30(%rsp), %rcx
movl 0x5c(%rsp), %eax
cmpl 0x34(%rcx), %eax
jge 0xa5ac2
movq 0x30(%rsp), %rdi
addq $0x38, %rdi
movslq 0x5c(%rsp), %rsi
callq 0x8ee50
movq 0x30(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x50(%rsp)
addq $0x168, %rdi            # imm = 0x168
leaq 0x50(%rsp), %rsi
callq 0xa5fd0
movq 0x30(%rsp), %rdi
movq %rax, 0x48(%rsp)
addq $0x168, %rdi            # imm = 0x168
callq 0xa6000
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xa5fb0
testb $0x1, %al
jne 0xa5a04
jmp 0xa5ab0
movq 0x30(%rsp), %rdi
addq $0x168, %rdi            # imm = 0x168
leaq 0x50(%rsp), %rsi
callq 0xa6030
movq (%rax), %rax
movq 0x250(%rax), %rax
movq %rax, 0x38(%rsp)
xorl %eax, %eax
cmpq 0x38(%rsp), %rax
jne 0xa5a89
movq 0xa65af(%rip), %rdi     # 0x14bfe8
leaq 0x546b8(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x57ef5(%rip), %rsi     # 0xfd944
callq 0x8330
movq %rax, %rdi
leaq 0x5598c(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0x69, %esi
callq 0x8650
movq %rax, %rdi
movq 0xa6506(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
movq 0x38(%rsp), %rcx
movq 0x50(%rsp), %rax
movq %rcx, 0x250(%rax)
movq 0x38(%rsp), %rdi
addq $0x258, %rdi            # imm = 0x258
leaq 0x50(%rsp), %rsi
callq 0x186f0
jmp 0xa5ab2
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0xa598e
addq $0x138, %rsp            # imm = 0x138
retq
nopw (%rax,%rax)