// File: /workspace/github_cpp_makefile_star2_O1/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 %rdi, (%rsp)
movq %rsi, 0x98(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq 0x8(%rsi), %r13
movq 0x10(%rsi), %rax
movq %rax, 0x88(%rsp)
cmpq %rax, %r13
je 0x4045f
movq (%rsp), %rax
leaq 0x70(%rax), %rbx
leaq 0x38(%rax), %r15
leaq 0xc0(%rsp), %rbp
leaq 0x38(%rsp), %r14
movq (%r13), %r12
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x58c7c
movq %r13, 0x90(%rsp)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x59681
cmpl $0x15, (%r12)
je 0x402c8
movq 0x78(%rsp), %rax
cmpq 0x70(%rsp), %rax
je 0x402c8
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x478d8
testq %rax, %rax
jne 0x40271
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x479d4
testq %rax, %rax
jne 0x4093f
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x47ad0
movl $0x0, 0x38(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x47e84
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x47f94
movl $0x0, (%rax)
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x47ad0
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x4029b
movq 0x48(%rax), %rax
movq -0x8(%rax), %rcx
movl $0x200, %eax            # imm = 0x200
addq %rax, %rcx
movl -0x4(%rcx), %eax
movq 0x70(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
movl %eax, 0x38(%rcx)
movq 0x78(%rsp), %rax
subq 0x70(%rsp), %rax
incq %r13
sarq $0x3, %rax
addq $0x20, %r12
cmpq %r13, %rax
ja 0x4020a
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x40400
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbp, %rdi
callq 0x479d4
movq 0x8(%rsp), %rsi
addq %r12, %rsi
testq %rax, %rax
je 0x40307
movq %rbp, %rdi
callq 0x47f94
incl (%rax)
jmp 0x40315
movq %rbp, %rdi
callq 0x47f94
movl $0x1, (%rax)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x479d4
testq %rax, %rax
jne 0x4037c
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x478d8
testq %rax, %rax
jne 0x408d9
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x47ad0
movl $0x0, 0x38(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x47e84
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x47f94
movl $0x0, (%rax)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x47f94
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movl %ecx, 0x38(%rsp)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x47ad0
movq 0x30(%rax), %rcx
movq 0x40(%rax), %rdx
addq $-0x4, %rdx
cmpq %rdx, %rcx
je 0x403c7
movl 0x38(%rsp), %edx
movl %edx, (%rcx)
addq $0x4, %rcx
movq %rcx, 0x30(%rax)
jmp 0x403d2
movq %rax, %rdi
movq %r14, %rsi
callq 0x270cc
movl 0x38(%rsp), %eax
movq 0x58(%rsp), %rcx
movq (%rcx,%r13,8), %rcx
movl %eax, 0x38(%rcx)
movq 0x60(%rsp), %rax
subq 0x58(%rsp), %rax
incq %r13
sarq $0x3, %rax
addq $0x20, %r12
cmpq %r13, %rax
ja 0x402de
leaq 0x20(%rsp), %rdi
callq 0x4643e
leaq 0x8(%rsp), %rdi
callq 0x4643e
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x4042e
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
movq 0x90(%rsp), %r13
je 0x4044d
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
addq $0x8, %r13
cmpq 0x88(%rsp), %r13
jne 0x401bf
movq 0x98(%rsp), %rax
movq 0x40(%rax), %r14
movq 0x48(%rax), %rax
movq %rax, 0xa0(%rsp)
cmpq %rax, %r14
je 0x407da
leaq 0x18(%rsp), %r12
movq (%rsp), %rax
leaq 0x70(%rax), %rbp
addq $0x38, %rax
movq %rax, 0xa8(%rsp)
movq %rbp, 0xb8(%rsp)
movq (%r14), %rbx
movq 0x28(%rbx), %r15
movq 0x30(%rbx), %rsi
movq %r15, %rdi
leaq 0x98(%rsp), %rdx
callq 0x2bbe7
movq %r14, 0xb0(%rsp)
subq %r15, %rax
shrq $0x3, %rax
movslq %eax, %rcx
movq 0x30(%rbx), %rax
subq 0x28(%rbx), %rax
sarq $0x3, %rax
movq %rcx, 0x90(%rsp)
cmpq %rax, %rcx
jae 0x409a2
movq 0x8(%rbx), %r13
movq 0x10(%rbx), %rcx
cmpq %rcx, %r13
leaq 0x8(%rsp), %r15
je 0x407c0
movq %rcx, 0x88(%rsp)
movq (%r13), %r14
movl (%r14), %eax
cmpl $0x7, %eax
je 0x405c9
cmpl $0x15, %eax
jne 0x405d3
leaq 0x88(%r14), %rsi
movq 0x90(%rsp), %rbx
shlq $0x6, %rbx
movq 0xc8(%r14), %rdi
addq %rbx, %rdi
callq 0x2822e
movq %r12, 0x8(%rsp)
movq 0x90(%r14), %rsi
movq 0x98(%r14), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0xca10
movq %r15, %rdi
leaq 0x3225c(%rip), %rsi     # 0x727c8
callq 0x8590
movl 0xb0(%r14), %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x405e6
movl $0x4, %r12d
movl %ebp, %eax
cmpl $0x63, %eax
jbe 0x405dd
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x405e3
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x405e6
movl %eax, %ecx
movl $0xd1b71759, %edx       # imm = 0xD1B71759
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r12d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x40594
addl $-0x3, %r12d
jmp 0x405e6
movl $0xd, %eax
jmp 0x407aa
movl $0xc, %eax
jmp 0x407aa
addl $-0x2, %r12d
jmp 0x405e6
decl %r12d
shrl $0x1f, %r15d
leal (%r12,%r15), %esi
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x38(%rsp), %rdi
movl $0x2d, %edx
callq 0x8350
addq 0x38(%rsp), %r15
movq %r15, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0xc65b
movq 0x8(%rsp), %rcx
movl $0xf, %esi
leaq 0x18(%rsp), %r12
cmpq %r12, %rcx
je 0x40632
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rsi, %rax
leaq 0x68(%rsp), %r15
movq 0xb8(%rsp), %rbp
jbe 0x4066d
movl $0xf, %esi
leaq 0x48(%rsp), %rdi
cmpq %rdi, 0x38(%rsp)
je 0x40668
movq 0x48(%rsp), %rsi
cmpq %rsi, %rax
jbe 0x4067e
movq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdi
callq 0x8120
jmp 0x4068c
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8310
movq %r15, 0x58(%rsp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x406ac
movq %rdx, 0x58(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x68(%rsp)
jmp 0x406b3
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x60(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x406ec
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x40703
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
callq 0x478d8
testq %rax, %rax
jne 0x4074f
movq 0xa8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x47f94
movl $0x0, (%rax)
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
callq 0x47ad0
movl $0x0, 0x8(%rsp)
movq %rax, %rdi
leaq 0x8(%rsp), %rsi
callq 0x47e84
movq %rbp, %rdi
leaq 0x58(%rsp), %rsi
callq 0x47ad0
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x40776
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,%rbx)
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x4079b
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x82a0
xorl %eax, %eax
leaq 0x8(%rsp), %r15
movq 0x88(%rsp), %rcx
cmpl $0xd, %eax
je 0x407b3
testl %eax, %eax
jne 0x407c0
addq $0x8, %r13
cmpq %rcx, %r13
jne 0x40509
movq 0xb0(%rsp), %r14
addq $0x8, %r14
cmpq 0xa0(%rsp), %r14
jne 0x404a1
movq 0x98(%rsp), %rax
movq 0x258(%rax), %rbx
movq 0x260(%rax), %r14
cmpq %r14, %rbx
je 0x40807
movq (%rbx), %rsi
movq (%rsp), %rdi
callq 0x4014a
addq $0x8, %rbx
jmp 0x407f0
movq 0xd0(%rsp), %r14
testq %r14, %r14
je 0x40854
addq $0x70, (%rsp)
leaq 0x8(%r14), %rsi
movq (%rsp), %rdi
callq 0x47ad0
cmpl $0x0, 0x28(%r14)
jle 0x4084c
movq %rax, %rbx
xorl %ebp, %ebp
movq 0x30(%rbx), %rax
cmpq 0x10(%rbx), %rax
je 0x40873
movq %rbx, %rdi
callq 0x482d4
incl %ebp
cmpl 0x28(%r14), %ebp
jl 0x40832
movq (%r14), %r14
testq %r14, %r14
jne 0x40819
leaq 0xc0(%rsp), %rdi
callq 0x4649a
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4f776(%rip), %rdi     # 0x8fff0
leaq 0x2e8e7(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4f75e(%rip), %rdi     # 0x8fff0
leaq 0x31f0b(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4f746(%rip), %rdi     # 0x8fff0
leaq 0x2fb0f(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4f72e(%rip), %rdi     # 0x8fff0
movl $0x259, %esi            # imm = 0x259
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40a03
movq 0x4f710(%rip), %rdi     # 0x8fff0
leaq 0x2e881(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4f6f8(%rip), %rdi     # 0x8fff0
leaq 0x31ea5(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4f6e0(%rip), %rdi     # 0x8fff0
leaq 0x2faa9(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4f6c8(%rip), %rdi     # 0x8fff0
movl $0x235, %esi            # imm = 0x235
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40a03
movq 0x4f6aa(%rip), %rdi     # 0x8fff0
leaq 0x2e81b(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4f692(%rip), %rdi     # 0x8fff0
leaq 0x31e3f(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4f67a(%rip), %rdi     # 0x8fff0
leaq 0x2fa43(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4f662(%rip), %rdi     # 0x8fff0
movl $0x227, %esi            # imm = 0x227
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40a03
movq 0x4f647(%rip), %rdi     # 0x8fff0
leaq 0x2e7b8(%rip), %rsi     # 0x6f168
movl $0x8, %edx
callq 0x8330
movq 0x4f62f(%rip), %rdi     # 0x8fff0
leaq 0x31ddc(%rip), %rsi     # 0x727a4
movl $0x18, %edx
callq 0x8330
movq 0x4f617(%rip), %rdi     # 0x8fff0
leaq 0x2f9e0(%rip), %rsi     # 0x703c0
movl $0x1, %edx
callq 0x8330
movq 0x4f5ff(%rip), %rdi     # 0x8fff0
movl $0x242, %esi            # imm = 0x242
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
jmp 0x40ab4
jmp 0x40ab4
movq %rax, %rbx
jmp 0x40ad6
jmp 0x40ab4
jmp 0x40ab4
jmp 0x40ab4
jmp 0x40a94
jmp 0x40abf
jmp 0x40abf
jmp 0x40abf
jmp 0x40a94
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x40a65
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40a65
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
jne 0x40a83
jmp 0x40ae0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x40ae0
movq 0x18(%rsp), %rsi
jmp 0x40aa6
jmp 0x40ab4
jmp 0x40ab4
jmp 0x40abf
jmp 0x40abf
jmp 0x40abf
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x40ae0
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40ae0
jmp 0x40abf
jmp 0x40abf
movq %rax, %rbx
jmp 0x40ae0
jmp 0x40abf
jmp 0x40abf
jmp 0x40abf
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4643e
leaq 0x8(%rsp), %rdi
callq 0x4643e
leaq 0x58(%rsp), %rdi
callq 0x45b5a
leaq 0xc0(%rsp), %rdi
callq 0x4649a
movq %rbx, %rdi
callq 0x8540
nop