// File: /workspace/github_cpp_makefile_star2_O1/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::InsertPhiIR(IRFunction*)
void ConvertSSA::InsertPhiIR(IRFunction* f) {
#ifdef DEBUG_CONVERT_SSA_PROCESS
  std::cout << "insert phi" << std::endl;
#endif
  std::unordered_map<std::string, std::unordered_set<IRBasicBlock*>> defsites;  // 变量被定值的基本块集合
  std::unordered_map<IRBasicBlock*, std::unordered_set<std::string>> phi_vars;  // 一个基本块内拥有phi函数的变量
  // compute def_use without array
  IRLivenessAnalysis::GetDefUse4Func(f, false);
  // fill defsites
  for (auto bb : f->bb_list_) {
    for (auto& def : bb->def_) {
      defsites[def].insert(bb);
    }
  }
  IRLivenessAnalysis::Run4Func(f);  // 用于插入phi node的时候判断是否活跃决定是否插入
  for (auto& [var, defbbs] : defsites) {
    while (!defbbs.empty()) {
      auto bb = *defbbs.begin();
#ifdef DEBUG_CONVERT_SSA_PROCESS
      std::cout << "var:" << var << "在这些bb中def:" << std::endl;
      std::cout << bb->IndexInFunc() << std::endl;
#endif
      defbbs.erase(defbbs.begin());
      for (auto df : bb->df_) {  // 对于变量定值所在bb的每一个df都要加一个phi函数
        // NOTE: 如果var并不入口活跃就不用加入phi函数
        if (df->livein_.find(var) == df->livein_.end()) continue;
        if (phi_vars.find(df) == phi_vars.end() || phi_vars[df].find(var) == phi_vars[df].end()) {
          phi_vars[df].insert(var);
          auto&& res = CompName2Opn(var);
          // NOTE: phi结点的scope id可能并不正确
          auto ir_list_it = df->ir_list_.begin();
          while (ir_list_it != df->ir_list_.end() && (*ir_list_it)->op_ == IR::OpKind::LABEL) ++ir_list_it;
          df->ir_list_.insert(ir_list_it, new IR{IR::OpKind::PHI, res, static_cast<int>(df->pred_.size())});
          if (df->def_.find(var) == df->def_.end()) {
            df->def_.insert(var);
            defbbs.insert(df);
          }
        }
      }  // end of df for
    }    // end of defbbs while
#ifdef DEBUG_CONVERT_SSA_PROCESS
    std::cout << "end." << std::endl;
#endif
  }  // end of defsites for
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
leaq 0x120(%rsp), %rax
movq %rax, -0x30(%rax)
movl $0x1, %ecx
movq %rcx, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
leaq 0xa8(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movups %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movups %xmm0, -0x8(%rax)
movq %rsi, %rdi
xorl %esi, %esi
callq 0x5a578
movq 0x30(%rbx), %r13
movq %rbx, 0x10(%rsp)
movq 0x38(%rbx), %rbp
cmpq %rbp, %r13
je 0x3fde6
leaq 0xf0(%rsp), %r14
leaq 0x18(%rsp), %r15
leaq 0x38(%rsp), %r12
movq (%r13), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rax), %rbx
testq %rbx, %rbx
je 0x3fddd
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x48320
movq %rax, 0x38(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x4875a
movq (%rbx), %rbx
jmp 0x3fdb4
addq $0x8, %r13
cmpq %rbp, %r13
jne 0x3fda7
movq 0x10(%rsp), %rdi
callq 0x5a6b2
movq 0x100(%rsp), %r14
testq %r14, %r14
je 0x40085
leaq 0x78(%rsp), %r15
leaq 0x8(%rsp), %r13
leaq 0x38(%rsp), %rbp
cmpq $0x0, 0x40(%r14)
je 0x40079
leaq 0x8(%r14), %r12
leaq 0x28(%r14), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq 0x38(%r14), %rsi
movq 0x8(%rsi), %rbx
movq 0x20(%rsp), %rdi
callq 0x48a20
movq 0x280(%rbx), %r14
testq %r14, %r14
je 0x40069
movq 0x8(%r14), %rdi
movq %rdi, 0x8(%rsp)
addq $0xc8, %rdi
movq %r12, %rsi
callq 0x48ae0
testq %rax, %rax
je 0x40061
movq 0x8(%rsp), %rcx
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x3fed9
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0x3fed9
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0x3fed7
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0x3fed9
movq %r10, %rax
cmpq %rdi, %rcx
jne 0x3feac
jmp 0x3fed9
xorl %eax, %eax
testq %rax, %rax
je 0x3ff11
cmpq $0x0, (%rax)
je 0x3ff11
movq %r15, %rdi
movq %r13, %rsi
callq 0x48b62
movq %rax, %rdi
movq %r12, %rsi
callq 0x48ae0
movq %rax, %rbx
movq %r15, %rdi
movq %r13, %rsi
callq 0x48b62
testq %rbx, %rbx
jne 0x40061
movq %r15, %rdi
movq %r13, %rsi
callq 0x48b62
movq %rax, 0x38(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x48e46
movq %rbp, %rdi
movq %r12, %rsi
callq 0x441e2
movq 0x8(%rsp), %r12
movq 0x8(%r12), %rbx
movq 0x10(%r12), %rax
cmpq %rax, %rbx
je 0x3ff5c
movq (%rbx), %rcx
cmpl $0x7, (%rcx)
jne 0x3ff5c
addq $0x8, %rbx
jmp 0x3ff49
movl $0xe0, %edi
callq 0x8290
movq %rax, 0x10(%rsp)
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x28186
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rcx
subq 0x28(%rax), %rcx
shrq $0x3, %rcx
movb $0x1, %r15b
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl $0x15, %esi
movq %r13, %rdx
callq 0x45b8a
addq $0x8, %r12
movq %rbp, 0x18(%rsp)
xorl %r15d, %r15d
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x18(%rsp), %rdx
callq 0x4916c
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x3ffe8
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x48ae0
leaq 0x78(%rsp), %r15
leaq 0x18(%rsp), %rbx
testq %rax, %rax
leaq 0x8(%rsp), %r13
jne 0x40040
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x18(%rsp)
movq %r12, %rsi
movq %rbx, %rdx
callq 0x48e46
movq 0x20(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4875a
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4005c
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x38(%rsp), %rbp
movq (%r14), %r14
jmp 0x3fe4b
movq 0x28(%rsp), %r14
cmpq $0x0, 0x40(%r14)
jne 0x3fe32
movq (%r14), %r14
testq %r14, %r14
jne 0x3fe10
leaq 0x78(%rsp), %rdi
callq 0x46824
leaq 0xf0(%rsp), %rdi
callq 0x468be
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40127
jmp 0x40127
jmp 0x40127
jmp 0x40127
jmp 0x400c4
movq %rax, %rbx
movb $0x1, %r15b
jmp 0x400f3
jmp 0x400c4
jmp 0x40127
movq %rax, %rbx
jmp 0x40107
jmp 0x40127
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x400f3
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x10(%rsp), %rdi
testb %r15b, %r15b
je 0x40107
movl $0xe0, %esi
callq 0x82a0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4012a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x4012a
jmp 0x40127
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x46824
leaq 0xf0(%rsp), %rdi
callq 0x468be
movq %rbx, %rdi
callq 0x8540
nop