// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::Rename(IRBasicBlock*)
void ConvertSSA::Rename(IRBasicBlock* bb) {
#ifdef DEBUG_CONVERT_SSA_PROCESS
  std::cout << "rename for irbb: " << std::endl;
  // bb->EmitCode(std::cout);
#endif
  std::unordered_map<std::string, int> deftimes;
  // 对bb中每条ir的使用和定值var rename
  for (auto ir : bb->ir_list_) {
    // 不考虑对数组的定值和使用
    auto [def, use] = GetDefUsePtr(ir, false);
    auto [defvar, usevar] = GetDefUse(ir, false);
    if (ir->op_ != IR::OpKind::PHI) {
      for (int i = 0; i < use.size(); ++i) {
        if (this->stack_.find(usevar[i]) == this->stack_.end()) {
          MyAssert(this->count_.find(usevar[i]) == this->count_.end());
          this->stack_[usevar[i]].push(0);
          this->count_[usevar[i]] = 0;
        }
        use[i]->ssa_id_ = this->stack_[usevar[i]].top();
      }
    }
    for (int i = 0; i < def.size(); ++i) {
      if (deftimes.find(defvar[i]) == deftimes.end()) {
        deftimes[defvar[i]] = 1;
      } else {
        ++deftimes[defvar[i]];
      }
      if (this->count_.find(defvar[i]) == this->count_.end()) {
        MyAssert(this->stack_.find(defvar[i]) == this->stack_.end());
        this->stack_[defvar[i]].push(0);
        this->count_[defvar[i]] = 0;
      }
      int cnt = ++this->count_[defvar[i]];
      this->stack_[defvar[i]].push(cnt);
      def[i]->ssa_id_ = cnt;
    }
  }  // end of ir for-loop
  // 填写bb的succ中的phi ir
  // std::cout << "fill phi" << std::endl;
  for (auto succ : bb->succ_) {
    int order = std::distance(succ->pred_.begin(), std::find(succ->pred_.begin(), succ->pred_.end(), bb));
    MyAssert(order < succ->pred_.size());
    for (auto phi_ir : succ->ir_list_) {
      if (phi_ir->op_ == IR::OpKind::LABEL) continue;
      if (phi_ir->op_ != IR::OpKind::PHI) break;
      phi_ir->phi_args_[order] = phi_ir->res_;
      // NOTE here!!!
      const auto&& varname = phi_ir->res_.name_ + "." + std::to_string(phi_ir->res_.scope_id_);
      // std::cout << varname << std::endl;
      if (this->stack_.find(varname) == this->stack_.end()) {
        this->count_[varname] = 0;
        this->stack_[varname].push(0);
      }
      phi_ir->phi_args_[order].ssa_id_ = this->stack_[varname].top();
    }
  }
  // 遍历支配树子结点
  for (auto dom : bb->doms_) {
    Rename(dom);
  }
  // 维护stack
  for (auto it = deftimes.begin(); it != deftimes.end(); ++it) {
    auto& stack = this->stack_[(*it).first];
    for (int i = 0; i < (*it).second; ++i) {
      MyAssert(!stack.empty());
      stack.pop();
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, 0x20(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
leaq 0xd0(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x3f800000, 0x10(%rax) # imm = 0x3F800000
movups %xmm0, 0x18(%rax)
movq 0x8(%rsi), %r13
movq 0x10(%rsi), %rax
movq %rax, 0x10(%rsp)
leaq 0x70(%rdi), %rbx
movq %rdi, 0x58(%rsp)
leaq 0x38(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0xc0(%rsp), %rbp
leaq 0xa0(%rsp), %r14
cmpq 0x10(%rsp), %r13
je 0x31f95
movq (%r13), %r12
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x4632c
movq %r13, 0x18(%rsp)
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x465cf
cmpl $0x15, (%r12)
jne 0x31ebf
movq 0x70(%rsp), %rax
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x78(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x31e9d
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq %rbp, %rdi
callq 0x37dea
movq 0x28(%rsp), %rsi
addq %r12, %rsi
testq %rax, %rax
je 0x31dce
movq %rbp, %rdi
callq 0x38318
incl (%rax)
jmp 0x31ddc
movq %rbp, %rdi
callq 0x38318
movl $0x1, (%rax)
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rdi
callq 0x37dea
testq %rax, %rax
jne 0x31e44
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37d1a
testq %rax, %rax
jne 0x32201
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37eba
andl $0x0, 0xa0(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x38256
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rdi
callq 0x38318
andl $0x0, (%rax)
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rdi
callq 0x38318
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movl %ecx, 0xa0(%rsp)
movq 0x28(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37eba
movq %rax, %rdi
movq %r14, %rsi
callq 0x1cd82
movl 0xa0(%rsp), %ecx
movq 0x70(%rsp), %rax
movq (%rax,%r13,8), %rdx
movl %ecx, 0x38(%rdx)
incq %r13
addq $0x20, %r12
jmp 0x31d90
leaq 0x28(%rsp), %rdi
callq 0x35d46
leaq 0x70(%rsp), %rdi
callq 0x35d5c
movq 0x18(%rsp), %r13
addq $0x8, %r13
jmp 0x31d48
movq 0x88(%rsp), %rax
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x90(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x31d85
movq 0x40(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37d1a
testq %rax, %rax
jne 0x31f4d
movq 0x40(%rsp), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rdi
callq 0x37dea
testq %rax, %rax
jne 0x32249
movq 0x40(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37eba
andl $0x0, 0xa0(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x38256
movq 0x40(%rsp), %rsi
addq %r12, %rsi
movq 0x8(%rsp), %rdi
callq 0x38318
andl $0x0, (%rax)
movq 0x40(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x37eba
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x31f77
movq 0x48(%rax), %rax
movq -0x8(%rax), %rcx
movl $0x200, %eax            # imm = 0x200
addq %rax, %rcx
movl -0x4(%rcx), %ecx
movq 0x88(%rsp), %rax
movq (%rax,%r13,8), %rdx
movl %ecx, 0x38(%rdx)
incq %r13
addq $0x20, %r12
jmp 0x31ecd
leaq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x40(%rax), %rbp
movq 0x48(%rax), %rax
movq %rax, 0x60(%rsp)
leaq 0x70(%rsp), %r15
cmpq 0x60(%rsp), %rbp
je 0x3212f
movq (%rbp), %r14
movq 0x28(%r14), %r13
movq 0x30(%r14), %rsi
movq %r13, %rdi
leaq 0x20(%rsp), %rdx
callq 0x200f5
movq %rbp, 0x68(%rsp)
subq %r13, %rax
shrq $0x3, %rax
movslq %eax, %rbp
movq 0x30(%r14), %rax
subq 0x28(%r14), %rax
sarq $0x3, %rax
cmpq %rax, %rbp
jae 0x32299
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rdx
shlq $0x6, %rbp
movq %rdx, 0x10(%rsp)
cmpq %rdx, %rcx
je 0x32121
movq (%rcx), %r14
movl (%r14), %eax
cmpl $0x7, %eax
je 0x32118
cmpl $0x15, %eax
jne 0x32121
movq %rcx, 0x18(%rsp)
leaq 0x88(%r14), %rsi
movq 0xc8(%r14), %rdi
addq %rbp, %rdi
callq 0x1daba
leaq 0x90(%r14), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x276d5(%rip), %rdx     # 0x5972c
callq 0xa334
movl 0xb0(%r14), %esi
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0xa36c
movq %r15, %rdi
leaq 0x28(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0xa2cd
movq %r12, %rdi
callq 0x8578
movq %r13, %rdi
callq 0x8578
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37d1a
testq %rax, %rax
jne 0x320d3
movq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x38318
andl $0x0, (%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37eba
andl $0x0, 0x28(%rsp)
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x38256
movq %rbx, %rdi
movq %r15, %rsi
callq 0x37eba
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x320f8
movq 0x48(%rax), %rax
movq -0x8(%rax), %rcx
movl $0x200, %eax            # imm = 0x200
addq %rax, %rcx
movl -0x4(%rcx), %eax
movq 0xc8(%r14), %rcx
movl %eax, 0x38(%rcx,%rbp)
movq %r15, %rdi
callq 0x8578
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rdx
addq $0x8, %rcx
jmp 0x32008
movq 0x68(%rsp), %rbp
addq $0x8, %rbp
jmp 0x31faf
movq 0x20(%rsp), %rax
movq 0x258(%rax), %r14
movq 0x260(%rax), %r15
cmpq %r15, %r14
je 0x3215a
movq (%r14), %rsi
movq 0x58(%rsp), %rdi
callq 0x31cd6
addq $0x8, %r14
jmp 0x32142
leaq 0xd0(%rsp), %r15
movq (%r15), %r15
testq %r15, %r15
je 0x32197
leaq 0x8(%r15), %rsi
movq %rbx, %rdi
callq 0x37eba
movq %rax, %r14
xorl %ebp, %ebp
cmpl 0x28(%r15), %ebp
jge 0x32162
movq 0x30(%r14), %rax
cmpq 0x10(%r14), %rax
je 0x321b6
movq %r14, %rdi
callq 0x1cfb6
incl %ebp
jmp 0x3217b
leaq 0xc0(%rsp), %rdi
callq 0x3672c
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4ee2b(%rip), %rdi     # 0x80fe8
leaq 0x23f24(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x27538(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x25141(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x259, %esi            # imm = 0x259
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x3228f
movq 0x4ede0(%rip), %rdi     # 0x80fe8
leaq 0x23ed9(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x274ed(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x250f6(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x235, %esi            # imm = 0x235
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x3228f
movq 0x4ed98(%rip), %rdi     # 0x80fe8
leaq 0x23e91(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x274a5(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x250ae(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x227, %esi            # imm = 0x227
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
movq 0x4ed48(%rip), %rdi     # 0x80fe8
leaq 0x23e41(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x27455(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x2505e(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x242, %esi            # imm = 0x242
callq 0x84b0
movq %rax, %rdi
callq 0x8170
jmp 0x3228f
jmp 0x32333
jmp 0x32333
jmp 0x32333
jmp 0x32333
movq %rax, %rbx
jmp 0x3234b
jmp 0x32333
jmp 0x32322
jmp 0x3233e
jmp 0x3233e
jmp 0x3233e
movq %rax, %rbx
jmp 0x32313
jmp 0x32333
jmp 0x32333
jmp 0x32322
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x8578
leaq 0x28(%rsp), %rdi
jmp 0x3232a
jmp 0x3233e
jmp 0x3233e
jmp 0x3233e
jmp 0x3233e
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x8578
jmp 0x32355
jmp 0x3233e
movq %rax, %rbx
jmp 0x32355
jmp 0x3233e
jmp 0x3233e
jmp 0x3233e
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x35d46
leaq 0x70(%rsp), %rdi
callq 0x35d5c
leaq 0xc0(%rsp), %rdi
callq 0x3672c
movq %rbx, %rdi
callq 0x84d0