// File: /workspace/github_cpp_makefile_star2_O2/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 0xa8(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
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 0xe0(%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 0x46b24
movq 0x30(%rbx), %r12
movq 0x38(%rbx), %r13
leaq 0x78(%rsp), %r14
leaq 0x38(%rsp), %r15
cmpq %r13, %r12
je 0x31a59
movq (%r12), %rbp
movq %rbp, 0x38(%rsp)
addq $0x68, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x31a53
leaq 0x8(%rbp), %rsi
movq %r14, %rdi
callq 0x38676
movq %rax, %rdi
movq %r15, %rsi
callq 0x38a9a
jmp 0x31a31
addq $0x8, %r12
jmp 0x31a1f
movq %rbx, %rdi
callq 0x46c18
movq 0x88(%rsp), %rax
leaq 0xb0(%rsp), %r12
leaq 0x8(%rsp), %rbp
leaq 0x38(%rsp), %r13
leaq 0xe8(%rsp), %rbx
testq %rax, %rax
je 0x31c3f
leaq 0x8(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq %rax, 0x20(%rsp)
addq $0x28, %rax
movq %rax, 0x18(%rsp)
movq 0x20(%rsp), %rax
cmpq $0x0, 0x40(%rax)
je 0x31c37
movq 0x38(%rax), %rsi
movq 0x8(%rsi), %r15
movq 0x18(%rsp), %rdi
callq 0x38de0
addq $0x280, %r15            # imm = 0x280
movq (%r15), %r15
testq %r15, %r15
je 0x31aa3
movq 0x8(%r15), %rdi
movq %rdi, 0x8(%rsp)
addq $0xc8, %rdi
movq 0x10(%rsp), %rsi
callq 0x38ee2
testq %rax, %rax
je 0x31acc
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38f10
testq %rax, %rax
je 0x31b2e
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38f8c
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x38ee2
movq %rax, %r14
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38f8c
testq %r14, %r14
jne 0x31acc
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38f8c
movq 0x10(%rsp), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x392a2
movq %r13, %rdi
movq %r14, %rsi
callq 0x34a10
movq 0x8(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x28(%rsp)
movq 0x8(%rax), %rbp
movq 0x10(%rax), %rax
cmpq %rax, %rbp
je 0x31b7e
movq (%rbp), %rcx
cmpl $0x7, (%rcx)
jne 0x31b7e
addq $0x8, %rbp
jmp 0x31b6a
movl $0xe0, %edi
callq 0x8250
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
callq 0x1da22
movq %rbx, %rdx
movq %r12, %r13
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rcx
subq 0x28(%rax), %rcx
shrq $0x3, %rcx
movb $0x1, %r12b
movq %r14, %rdi
pushq $0x15
popq %rsi
callq 0x35d8e
movq %r14, 0x30(%rsp)
xorl %r12d, %r12d
movq 0x28(%rsp), %rdi
movq %rbp, %rsi
leaq 0x30(%rsp), %rdx
callq 0x395ee
leaq 0xf0(%rsp), %rdi
callq 0x8578
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x38ee2
testq %rax, %rax
leaq 0x8(%rsp), %rbp
jne 0x31c20
movq 0x8(%rsp), %rdi
addq $0x58, %rdi
movq %r14, %rsi
callq 0x392a2
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x38a9a
leaq 0x40(%rsp), %rdi
callq 0x8578
movq %r13, %r12
leaq 0x38(%rsp), %r13
jmp 0x31acc
movq (%rax), %rax
jmp 0x31a83
leaq 0xb0(%rsp), %rdi
callq 0x36b3a
leaq 0x78(%rsp), %rdi
callq 0x36bf8
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31cb4
jmp 0x31cb4
jmp 0x31cb4
jmp 0x31c79
jmp 0x31c79
jmp 0x31cb4
movq %rax, %rbx
jmp 0x31c97
movq %rax, %rbx
jmp 0x31ca4
jmp 0x31cb4
jmp 0x31cb4
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x8578
testb %r12b, %r12b
je 0x31ca4
movl $0xe0, %esi
movq %r14, %rdi
callq 0x8260
leaq 0x40(%rsp), %rdi
callq 0x8578
jmp 0x31cb7
jmp 0x31cb4
jmp 0x31cb4
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x36b3a
leaq 0x78(%rsp), %rdi
callq 0x36bf8
movq %rbx, %rdi
callq 0x84d0