// File: /workspace/github_cpp_makefile_star2_O3/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 0x5977a
movq 0x30(%rbx), %r13
movq %rbx, 0x10(%rsp)
movq 0x38(%rbx), %rbp
cmpq %rbp, %r13
je 0x3f6b6
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 0x3f6ad
leaq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x47afc
movq %rax, 0x38(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x47f24
movq (%rbx), %rbx
jmp 0x3f684
addq $0x8, %r13
cmpq %rbp, %r13
jne 0x3f677
movq 0x10(%rsp), %rdi
callq 0x598b4
movq 0x100(%rsp), %r14
testq %r14, %r14
je 0x3f92e
leaq 0x8(%rsp), %r15
leaq 0x38(%rsp), %rbp
leaq 0xb0(%rsp), %r13
cmpq $0x0, 0x40(%r14)
je 0x3f922
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 0x481b2
movq 0x280(%rbx), %r14
testq %r14, %r14
je 0x3f912
movq 0x8(%r14), %rdi
movq %rdi, 0x8(%rsp)
addq $0xc8, %rdi
movq %r12, %rsi
callq 0x4826a
testq %rax, %rax
je 0x3f90a
movq 0x8(%rsp), %rcx
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0x3f7c6
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0x3f795
movq (%r9), %r9
testq %r9, %r9
je 0x3f7c6
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0x3f7c6
cmpq %rdi, %rcx
jne 0x3f777
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0x482a4
movq %rax, %rdi
movq %r12, %rsi
callq 0x4826a
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0x482a4
testq %rbx, %rbx
jne 0x3f90a
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0x482a4
movq %rax, 0x38(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x4859a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x43a4f
movq 0x8(%rsp), %rbx
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %rax
cmpq %rax, %r12
je 0x3f812
movq (%r12), %rcx
cmpl $0x7, (%rcx)
jne 0x3f812
addq $0x8, %r12
jmp 0x3f7fe
movl $0xe0, %edi
callq 0x8290
movq %rax, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x28816
movq %r13, %rdx
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
callq 0x453f6
addq $0x8, %rbx
movq %rbp, 0x18(%rsp)
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x18(%rsp), %rdx
callq 0x488be
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x3f896
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x4826a
leaq 0x8(%rsp), %r15
leaq 0x18(%rsp), %rbx
testq %rax, %rax
leaq 0x38(%rsp), %rbp
jne 0x3f8ee
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x18(%rsp)
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4859a
movq 0x20(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movq %r15, %rsi
movq %rbx, %rdx
callq 0x47f24
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3f90a
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%r14), %r14
jmp 0x3f71e
movq 0x28(%rsp), %r14
cmpq $0x0, 0x40(%r14)
jne 0x3f705
movq (%r14), %r14
testq %r14, %r14
jne 0x3f6e3
leaq 0x78(%rsp), %rdi
callq 0x460be
leaq 0xf0(%rsp), %rdi
callq 0x46158
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3f9d2
jmp 0x3f9d2
jmp 0x3f9d2
jmp 0x3f9d2
jmp 0x3f971
jmp 0x3f9d2
jmp 0x3f9d2
jmp 0x3f971
movq %rax, %rbx
movq 0x10(%rsp), %rdi
jmp 0x3f9a8
movq %rax, %rbx
jmp 0x3f9b2
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0x3f99e
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %r15b, %r15b
movq 0x10(%rsp), %rdi
je 0x3f9b2
movl $0xe0, %esi
callq 0x82a0
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x3f9d5
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x3f9d5
jmp 0x3f9d2
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x460be
leaq 0xf0(%rsp), %rdi
callq 0x46158
movq %rbx, %rdi
callq 0x8540