// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::GenerateSSABasicBlocks(IRFunction*, SSAFunction*, std::unordered_map<IRBasicBlock*, SSABasicBlock*, std::hash<IRBasicBlock*>, std::equal_to<IRBasicBlock*>, std::allocator<std::pair<IRBasicBlock* const, SSABasicBlock*>>>&)
void ConvertSSA::GenerateSSABasicBlocks(IRFunction* func, SSAFunction* ssafunc,
                                        std::unordered_map<IRBasicBlock*, SSABasicBlock*>& bb_map) {
  // create armbb according to irbb
  for (auto bb : func->bb_list_) {
    SSABasicBlock* ssabb = new SSABasicBlock(ssafunc);
    bb_map.insert({bb, ssabb});
  }

  // maintain pred and succ
  for (auto bb : func->bb_list_) {
    for (auto pred : bb->pred_) {  // NOTE: it's OK?
      bb_map[bb]->AddPredBB(bb_map[pred]);
      // bb_map[pred]->AddSuccBB(bb_map[bb]);
    }
    for (auto succ : bb->succ_) {
      bb_map[bb]->AddSuccBB(bb_map[succ]);
    }
  }

  // maintain dom info
  for (auto bb : func->bb_list_) {
    bb_map[bb]->SetIDom(bb_map[bb->idom_]);
    for (auto dom : bb->doms_) {
      bb_map[bb]->AddDomBB(bb_map[dom]);
    }
    for (auto df : bb->df_) {
      bb_map[bb]->AddDF(bb_map[df]);
    }
  }

  // 处理所有label语句 给ssabasicblock绑定bbvalue 把bbvalue添加到workmap中
  for (auto bb : func->bb_list_) {
    auto ssabb = bb_map[bb];
    auto first_ir = bb->ir_list_.front();
    if (first_ir->op_ == ir::IR::OpKind::LABEL) {
      if (first_ir->opn1_.type_ == ir::Opn::Type::Label) {  // 如果是label就把label赋给这个bb的label
        auto& label = first_ir->opn1_.name_;
        ssabb->SetLabel(label);
        MyAssert(work_map.find(label) == work_map.end());
        work_map[label] = new BasicBlockValue(label, ssabb);
      }  // 如果是func label不用管 直接删了就行
      else {
        auto label = "." + func->name_ + ".bb." + std::to_string(bb->IndexInFunc());
        ssabb->SetLabel(label);
        new BasicBlockValue(label, ssabb);
      }
      bb->ir_list_.erase(bb->ir_list_.begin());
    } else {
      auto label = "." + func->name_ + ".bb." + std::to_string(bb->IndexInFunc());
      ssabb->SetLabel(label);
      new BasicBlockValue(label, ssabb);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rdi, 0x20(%rsp)
movq 0x30(%rsi), %r13
movq %rsi, (%rsp)
movq 0x38(%rsi), %rbp
cmpq %rbp, %r13
je 0x33372
movq (%r13), %r15
movl $0xd0, %edi
callq 0x8250
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x34e14
movq %r15, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x3730a
addq $0x8, %r13
jmp 0x33334
movq (%rsp), %rax
movq 0x30(%rax), %rcx
movq 0x38(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x28(%rsp), %r14
leaq 0x48(%rsp), %r15
cmpq 0x8(%rsp), %rcx
je 0x33438
movq %rcx, 0x10(%rsp)
movq (%rcx), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rax), %r13
movq 0x30(%rax), %rbp
cmpq %rbp, %r13
je 0x333e5
movq (%r13), %rax
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3763c
movq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3763c
movq (%rax), %rsi
movq %r12, %rdi
callq 0x34f16
addq $0x8, %r13
jmp 0x333ad
movq 0x28(%rsp), %rax
movq 0x40(%rax), %rbp
movq 0x48(%rax), %r13
cmpq %r13, %rbp
je 0x3342a
movq (%rbp), %rax
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3763c
movq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3763c
movq (%rax), %rsi
movq %r12, %rdi
callq 0x34f2c
addq $0x8, %rbp
jmp 0x333f2
movq 0x10(%rsp), %rcx
addq $0x8, %rcx
jmp 0x3338d
movq (%rsp), %rax
movq 0x30(%rax), %rcx
movq 0x38(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x28(%rsp), %r14
leaq 0x48(%rsp), %r15
cmpq 0x8(%rsp), %rcx
je 0x33534
movq %rcx, 0x10(%rsp)
movq (%rcx), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3763c
movq (%rax), %r12
movq 0x28(%rsp), %rsi
movl $0x250, %eax            # imm = 0x250
addq %rax, %rsi
movq %rbx, %rdi
callq 0x3763c
movq (%rax), %rax
movq %rax, 0x70(%r12)
movq 0x28(%rsp), %rax
movq 0x258(%rax), %rbp
movq 0x260(%rax), %r13
cmpq %r13, %rbp
je 0x334e1
movq (%rbp), %rax
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3763c
movq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3763c
movq (%rax), %rsi
movq %r12, %rdi
callq 0x34f42
addq $0x8, %rbp
jmp 0x334a9
movq 0x28(%rsp), %rbp
movl $0x280, %eax            # imm = 0x280
addq %rax, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x33526
movq 0x8(%rbp), %rax
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3763c
movq (%rax), %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3763c
movq (%rax), %rsi
movq %r12, %rdi
callq 0x34f58
jmp 0x334ee
movq 0x10(%rsp), %rcx
addq $0x8, %rcx
jmp 0x33453
movq (%rsp), %rax
movq 0x30(%rax), %r12
movq 0x38(%rax), %rcx
movq %rcx, 0x10(%rsp)
addq $0x8, %rax
movq %rax, (%rsp)
addq $0xa8, 0x20(%rsp)
cmpq 0x10(%rsp), %r12
je 0x3376a
movq (%r12), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x3763c
movq (%rax), %rbp
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %r15
cmpl $0x7, (%r15)
jne 0x335fc
cmpl $0x2, 0x8(%r15)
jne 0x336a8
movq %rbp, %r13
pushq $0x50
popq %r14
movq %r12, 0x8(%rsp)
addq $0x10, %r15
leaq 0x8(%rbp), %rdi
movq %r15, %rsi
callq 0x81a0
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x36d60
testq %rax, %rax
jne 0x3377c
movq %r14, %rdi
callq 0x8250
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x26940
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x36e30
movq %rbp, (%rax)
movq 0x8(%rsp), %r12
jmp 0x3374f
leaq 0x68(%rsp), %r15
movq %r15, %rdi
leaq 0x26121(%rip), %rsi     # 0x5972c
movq (%rsp), %rdx
callq 0x1ae0c
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
leaq 0x26106(%rip), %rdx     # 0x59729
callq 0xa440
movq 0x18(%rsp), %rdi
callq 0x1fc18
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movl %eax, %esi
callq 0xa36c
leaq 0x28(%rsp), %r13
movq %r13, %rdi
leaq 0x48(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0xa2cd
movq %r14, %rdi
callq 0x8578
movq %r15, %rdi
callq 0x8578
leaq 0x68(%rsp), %rdi
callq 0x8578
leaq 0x8(%rbp), %rdi
movq %r13, %rsi
callq 0x81a0
pushq $0x50
popq %rdi
callq 0x8250
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x26940
movq %r13, %rdi
callq 0x8578
jmp 0x33761
leaq 0x68(%rsp), %r15
movq %r15, %rdi
leaq 0x26075(%rip), %rsi     # 0x5972c
movq (%rsp), %rdx
callq 0x1ae0c
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
leaq 0x2605a(%rip), %rdx     # 0x59729
callq 0xa440
movq 0x18(%rsp), %rdi
callq 0x1fc18
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movl %eax, %esi
callq 0xa36c
leaq 0x28(%rsp), %r13
movq %r13, %rdi
leaq 0x48(%rsp), %r15
movq %r15, %rsi
movq %r14, %rdx
callq 0xa2cd
movq %r14, %rdi
callq 0x8578
movq %r15, %rdi
callq 0x8578
leaq 0x68(%rsp), %rdi
callq 0x8578
leaq 0x8(%rbp), %rdi
movq %r13, %rsi
callq 0x81a0
pushq $0x50
popq %rdi
callq 0x8250
movq %rax, %r15
movq %rax, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x26940
movq %r13, %rdi
callq 0x8578
movq 0x18(%rsp), %rdi
movq 0x8(%rdi), %rsi
addq $0x8, %rdi
callq 0x34f70
addq $0x8, %r12
jmp 0x33556
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4d865(%rip), %rdi     # 0x80fe8
leaq 0x2295e(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x25f72(%rip), %rsi     # 0x59708
movq %rax, %rdi
callq 0x8240
leaq 0x23b7b(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0xfa, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
jmp 0x337ef
jmp 0x33803
movq %rax, %rbx
pushq $0x50
popq %rsi
movq %rbp, %rdi
jmp 0x3383c
jmp 0x337dd
movq %rax, %rbx
pushq $0x50
popq %rsi
movq %r15, %rdi
callq 0x8260
jmp 0x3381f
jmp 0x33808
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x8578
jmp 0x3380b
jmp 0x3381c
movq %rax, %rbx
jmp 0x33815
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x8578
leaq 0x68(%rsp), %rdi
jmp 0x33824
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
callq 0x8260
jmp 0x33829
nop