// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movq %rsi, 0x90(%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
movq %rdi, 0x98(%rsp)
je 0x3fd12
leaq 0x70(%r12), %r15
leaq 0x38(%r12), %rbx
leaq 0xc0(%rsp), %rbp
leaq 0x38(%rsp), %r14
movq (%r13), %r12
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x57f98
movq %r13, (%rsp)
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x58895
cmpl $0x15, (%r12)
je 0x3fb71
movq 0x78(%rsp), %rax
cmpq 0x70(%rsp), %rax
je 0x3fb71
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x470c8
testq %rax, %rax
jne 0x3fb1c
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x471c4
testq %rax, %rax
jne 0x401c2
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x472c0
movl $0x0, 0x38(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x4766a
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x4777a
movl $0x0, (%rax)
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x472c0
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x3fb46
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), %rdx
movl %eax, 0x38(%rdx)
incq %r13
movq 0x78(%rsp), %rax
subq %rcx, %rax
sarq $0x3, %rax
addq $0x20, %r12
cmpq %r13, %rax
ja 0x3fab5
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x3fca7
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbp, %rdi
callq 0x471c4
movq 0x8(%rsp), %rsi
addq %r12, %rsi
testq %rax, %rax
je 0x3fbb0
movq %rbp, %rdi
callq 0x4777a
incl (%rax)
jmp 0x3fbbe
movq %rbp, %rdi
callq 0x4777a
movl $0x1, (%rax)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x471c4
testq %rax, %rax
jne 0x3fc25
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x470c8
testq %rax, %rax
jne 0x4015c
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x472c0
movl $0x0, 0x38(%rsp)
movq %rax, %rdi
movq %r14, %rsi
callq 0x4766a
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x4777a
movl $0x0, (%rax)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
callq 0x4777a
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movl %ecx, 0x38(%rsp)
movq 0x8(%rsp), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x472c0
movq 0x30(%rax), %rdx
movq 0x40(%rax), %rcx
addq $-0x4, %rcx
cmpq %rcx, %rdx
je 0x3fc70
movl 0x38(%rsp), %ecx
movl %ecx, (%rdx)
addq $0x4, %rdx
movq %rdx, 0x30(%rax)
jmp 0x3fc7f
movq %rax, %rdi
movq %r14, %rsi
callq 0x2776e
movl 0x38(%rsp), %ecx
movq 0x58(%rsp), %rax
movq (%rax,%r13,8), %rdx
movl %ecx, 0x38(%rdx)
incq %r13
movq 0x60(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
addq $0x20, %r12
cmpq %r13, %rcx
ja 0x3fb87
leaq 0x20(%rsp), %rdi
callq 0x45cd2
leaq 0x8(%rsp), %rdi
callq 0x45cd2
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x3fcd5
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
movq 0x98(%rsp), %r12
movq (%rsp), %r13
je 0x3fcf8
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
addq $0x8, %r13
cmpq 0x88(%rsp), %r13
jne 0x3fa6e
movq 0x90(%rsp), %rsi
movq 0x40(%rsi), %rbx
movq 0x48(%rsi), %rax
movq %rax, 0xa0(%rsp)
cmpq %rax, %rbx
je 0x40068
leaq 0x18(%rsp), %rbp
leaq 0x70(%r12), %r13
leaq 0x38(%r12), %rax
movq %rax, 0xb0(%rsp)
leaq 0x8(%rsp), %r12
movq %r13, 0xb8(%rsp)
movq (%rbx), %r14
movq 0x28(%r14), %r15
movq 0x30(%r14), %rsi
movq %r15, %rdi
leaq 0x90(%rsp), %rdx
callq 0x2c11b
movq %rbx, 0xa8(%rsp)
subq %r15, %rax
shrq $0x3, %rax
movslq %eax, %rdx
movq 0x30(%r14), %rax
subq 0x28(%r14), %rax
sarq $0x3, %rax
movq %rdx, (%rsp)
cmpq %rax, %rdx
jae 0x40225
movq 0x8(%r14), %rbx
movq 0x10(%r14), %r15
cmpq %r15, %rbx
je 0x4003e
shlq $0x6, (%rsp)
movq %r15, 0x88(%rsp)
movq (%rbx), %r14
movl (%r14), %eax
cmpl $0x7, %eax
je 0x40031
cmpl $0x15, %eax
jne 0x4003e
leaq 0x88(%r14), %rsi
movq 0xc8(%r14), %rdi
addq (%rsp), %rdi
callq 0x288be
movq %rbp, 0x8(%rsp)
movq 0x90(%r14), %rsi
movq 0x98(%r14), %rdx
addq %rsi, %rdx
movq %r12, %rdi
callq 0xc6dc
movq %r12, %rdi
leaq 0x319c2(%rip), %rsi     # 0x717cc
callq 0x8590
movq %rbp, %r13
movl 0xb0(%r14), %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x3fe73
movl $0x4, %r12d
movl %ebp, %eax
cmpl $0x63, %eax
jbe 0x3fe6a
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x3fe70
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x3fe73
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 0x3fe35
addl $-0x3, %r12d
jmp 0x3fe73
addl $-0x2, %r12d
jmp 0x3fe73
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 0xc35d
movq 0x8(%rsp), %rcx
movl $0xf, %edi
movq %r13, %rbp
cmpq %r13, %rcx
je 0x3febd
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %r8
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq (%rdx,%r8), %rax
cmpq %rdi, %rax
leaq 0x68(%rsp), %r15
movq 0xb8(%rsp), %r13
leaq 0x8(%rsp), %r12
jbe 0x3ff00
movl $0xf, %edi
leaq 0x48(%rsp), %r9
cmpq %r9, %rsi
je 0x3fefb
movq 0x48(%rsp), %rdi
cmpq %rdi, %rax
jbe 0x3ff0a
movq %r12, %rdi
callq 0x8120
jmp 0x3ff18
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 0x3ff38
movq %rdx, 0x58(%rsp)
movq (%rcx), %rdx
movq %rdx, 0x68(%rsp)
jmp 0x3ff3f
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 0x3ff78
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3ff8f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x470c8
testq %rax, %rax
jne 0x3ffd9
movq 0xb0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x4777a
movl $0x0, (%rax)
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x472c0
movl $0x0, 0x8(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x4766a
movq %r13, %rdi
leaq 0x58(%rsp), %rsi
callq 0x472c0
movq 0x30(%rax), %rcx
cmpq 0x38(%rax), %rcx
jne 0x40000
movq 0x48(%rax), %rax
movq -0x8(%rax), %rcx
movl $0x200, %eax            # imm = 0x200
addq %rax, %rcx
movl -0x4(%rcx), %eax
movq 0xc8(%r14), %rcx
movq (%rsp), %rdx
movl %eax, 0x38(%rcx,%rdx)
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x40029
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x88(%rsp), %r15
addq $0x8, %rbx
cmpq %r15, %rbx
jne 0x3fdb3
movq 0xa8(%rsp), %rbx
addq $0x8, %rbx
cmpq 0xa0(%rsp), %rbx
jne 0x3fd4f
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %r12
movq 0x258(%rsi), %rbx
movq 0x260(%rsi), %r14
cmpq %r14, %rbx
je 0x4008c
movq (%rbx), %rsi
movq %r12, %rdi
callq 0x3f9f4
addq $0x8, %rbx
jmp 0x40076
movq 0xd0(%rsp), %r14
testq %r14, %r14
je 0x400d7
addq $0x70, %r12
leaq 0x8(%r14), %rsi
movq %r12, %rdi
callq 0x472c0
cmpl $0x0, 0x28(%r14)
jle 0x400cf
movq %rax, %rbx
xorl %ebp, %ebp
movq 0x30(%rbx), %rax
cmpq 0x10(%rbx), %rax
je 0x400f6
movq %rbx, %rdi
callq 0x47ab0
incl %ebp
cmpl 0x28(%r14), %ebp
jl 0x400b5
movq (%r14), %r14
testq %r14, %r14
jne 0x4009d
leaq 0xc0(%rsp), %rdi
callq 0x45d30
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4eeeb(%rip), %rdi     # 0x8efe8
leaq 0x2e064(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4eed3(%rip), %rdi     # 0x8efe8
leaq 0x3168c(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4eebb(%rip), %rdi     # 0x8efe8
leaq 0x2f28c(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4eea3(%rip), %rdi     # 0x8efe8
movl $0x259, %esi            # imm = 0x259
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40286
movq 0x4ee85(%rip), %rdi     # 0x8efe8
leaq 0x2dffe(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4ee6d(%rip), %rdi     # 0x8efe8
leaq 0x31626(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4ee55(%rip), %rdi     # 0x8efe8
leaq 0x2f226(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4ee3d(%rip), %rdi     # 0x8efe8
movl $0x235, %esi            # imm = 0x235
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40286
movq 0x4ee1f(%rip), %rdi     # 0x8efe8
leaq 0x2df98(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4ee07(%rip), %rdi     # 0x8efe8
leaq 0x315c0(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4edef(%rip), %rdi     # 0x8efe8
leaq 0x2f1c0(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4edd7(%rip), %rdi     # 0x8efe8
movl $0x227, %esi            # imm = 0x227
callq 0x8520
movq %rax, %rdi
callq 0x8180
jmp 0x40286
movq 0x4edbc(%rip), %rdi     # 0x8efe8
leaq 0x2df35(%rip), %rsi     # 0x6e168
movl $0x8, %edx
callq 0x8330
movq 0x4eda4(%rip), %rdi     # 0x8efe8
leaq 0x3155d(%rip), %rsi     # 0x717a8
movl $0x18, %edx
callq 0x8330
movq 0x4ed8c(%rip), %rdi     # 0x8efe8
leaq 0x2f15d(%rip), %rsi     # 0x6f3c0
movl $0x1, %edx
callq 0x8330
movq 0x4ed74(%rip), %rdi     # 0x8efe8
movl $0x242, %esi            # imm = 0x242
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
jmp 0x40316
jmp 0x40316
movq %rax, %rbx
jmp 0x40338
jmp 0x40316
jmp 0x40316
jmp 0x40316
jmp 0x402f8
jmp 0x40321
jmp 0x40321
jmp 0x40321
jmp 0x402f8
jmp 0x40316
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x402dd
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x402dd
movq %rax, %rbx
movq %r13, %rbp
jmp 0x402dd
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x40342
movq 0x18(%rsp), %rsi
jmp 0x4030a
jmp 0x40316
jmp 0x40321
jmp 0x40321
jmp 0x40321
jmp 0x40321
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x40342
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40342
jmp 0x40321
movq %rax, %rbx
jmp 0x40342
jmp 0x40321
jmp 0x40321
jmp 0x40321
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x45cd2
leaq 0x8(%rsp), %rdi
callq 0x45cd2
leaq 0x58(%rsp), %rdi
callq 0x453c6
leaq 0xc0(%rsp), %rdi
callq 0x45d30
movq %rbx, %rdi
callq 0x8540
nop