// File: 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:
subq $0x318, %rsp            # imm = 0x318
movq %rdi, 0x310(%rsp)
movq %rsi, 0x308(%rsp)
movq %rdx, 0x300(%rsp)
movq %rcx, 0x2f8(%rsp)
movq 0x310(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x308(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x2f0(%rsp)
movq 0x2f0(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x2e8(%rsp)
movq 0x2f0(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x2e0(%rsp)
leaq 0x2e8(%rsp), %rdi
leaq 0x2e0(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89a80
jmp 0x89b4e
leaq 0x2e8(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x2d8(%rsp)
movl $0xd0, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x70(%rsp)
movq 0x300(%rsp), %rsi
callq 0x8d320
jmp 0x89abc
movq 0x70(%rsp), %rax
movq %rax, 0x2d0(%rsp)
movq 0x2f8(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0x2b0(%rsp), %rdi
leaq 0x2d8(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
callq 0x8d480
movq 0x68(%rsp), %rdi
leaq 0x2b0(%rsp), %rsi
callq 0x8d430
movq %rax, 0x2a0(%rsp)
movb %dl, 0x2a8(%rsp)
leaq 0x2e8(%rsp), %rdi
callq 0x49330
jmp 0x89a62
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
movl $0xd0, %esi
callq 0x8350
jmp 0x8a5bb
movq 0x308(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x298(%rsp)
movq 0x298(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x290(%rsp)
movq 0x298(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x288(%rsp)
leaq 0x290(%rsp), %rdi
leaq 0x288(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89baa
jmp 0x89d5a
leaq 0x290(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x280(%rsp)
movq 0x280(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x278(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x270(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x268(%rsp)
leaq 0x270(%rsp), %rdi
leaq 0x268(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89c1b
jmp 0x89c84
leaq 0x270(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x260(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x8d4b0
movq 0x60(%rsp), %rdi
movq (%rax), %rsi
callq 0x8d4e0
leaq 0x270(%rsp), %rdi
callq 0x49330
jmp 0x89c00
movq 0x280(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x258(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x250(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x248(%rsp)
leaq 0x250(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89cdd
jmp 0x89d46
leaq 0x250(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x240(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x280(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x240(%rsp), %rsi
callq 0x8d4b0
movq 0x58(%rsp), %rdi
movq (%rax), %rsi
callq 0x8d510
leaq 0x250(%rsp), %rdi
callq 0x49330
jmp 0x89cc2
jmp 0x89d48
leaq 0x290(%rsp), %rdi
callq 0x49330
jmp 0x89b8c
movq 0x308(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x238(%rsp)
movq 0x238(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x230(%rsp)
movq 0x238(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x228(%rsp)
leaq 0x230(%rsp), %rdi
leaq 0x228(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89db6
jmp 0x89fb0
leaq 0x230(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x220(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x2f8(%rsp), %rdi
movq 0x220(%rsp), %rsi
addq $0x250, %rsi            # imm = 0x250
callq 0x8d4b0
movq 0x50(%rsp), %rdi
movq (%rax), %rsi
callq 0x8d540
movq 0x220(%rsp), %rax
addq $0x258, %rax            # imm = 0x258
movq %rax, 0x218(%rsp)
movq 0x218(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x210(%rsp)
movq 0x218(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x208(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x89e6f
jmp 0x89ed8
leaq 0x210(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x200(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x48(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x8d4b0
movq 0x48(%rsp), %rdi
movq (%rax), %rsi
callq 0x8d560
leaq 0x210(%rsp), %rdi
callq 0x49330
jmp 0x89e54
movq 0x220(%rsp), %rax
addq $0x270, %rax            # imm = 0x270
movq %rax, 0x1f8(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0x8d590
movq %rax, 0x1f0(%rsp)
movq 0x1f8(%rsp), %rdi
callq 0x8d5c0
movq %rax, 0x1e8(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x8d5f0
testb $0x1, %al
jne 0x89f33
jmp 0x89f9c
leaq 0x1f0(%rsp), %rdi
callq 0x8d610
movq (%rax), %rax
movq %rax, 0x1e0(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x8d4b0
movq 0x40(%rsp), %rdi
movq (%rax), %rsi
callq 0x8d630
leaq 0x1f0(%rsp), %rdi
callq 0x8d670
jmp 0x89f18
jmp 0x89f9e
leaq 0x230(%rsp), %rdi
callq 0x49330
jmp 0x89d98
movq 0x308(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x1d0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0x8a00c
jmp 0x8a5b3
leaq 0x1d0(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x1c0(%rsp)
movq 0x2f8(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x8d4b0
movq (%rax), %rax
movq %rax, 0x1b8(%rsp)
movq 0x1c0(%rsp), %rdi
addq $0x8, %rdi
callq 0x8d6a0
movq (%rax), %rax
movq %rax, 0x1b0(%rsp)
movq 0x1b0(%rsp), %rax
cmpl $0x7, (%rax)
jne 0x8a3ef
movq 0x1b0(%rsp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x8a1d8
movq 0x1b0(%rsp), %rax
addq $0x8, %rax
addq $0x8, %rax
movq %rax, 0x1a8(%rsp)
movq 0x1b8(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
callq 0x8d6d0
movq 0x78(%rsp), %rdi
addq $0xa8, %rdi
movq 0x1a8(%rsp), %rsi
callq 0x8ccc0
movq 0x78(%rsp), %rdi
movq %rax, 0x1a0(%rsp)
addq $0xa8, %rdi
callq 0x8cd10
movq %rax, 0x198(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x8d700
xorb $-0x1, %al
testb $0x1, %al
jne 0x8a107
jmp 0x8a15e
movq 0xc1eda(%rip), %rdi     # 0x14bfe8
leaq 0x6ffe3(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x73781(%rip), %rsi     # 0xfd8a5
callq 0x8330
movq %rax, %rdi
leaq 0x712b7(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0xfa, %esi
callq 0x8650
movq %rax, %rdi
movq 0xc1e31(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
movl $0x50, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq 0x1a8(%rsp), %rsi
movq 0x1b8(%rsp), %rdx
callq 0x5c720
jmp 0x8a18a
movq 0x78(%rsp), %rdi
addq $0xa8, %rdi
movq 0x1a8(%rsp), %rsi
callq 0x8cd60
movq 0x38(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x8a391
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
movl $0x50, %esi
callq 0x8350
jmp 0x8a5bb
movq 0x308(%rsp), %rdx
addq $0x8, %rdx
leaq 0x736de(%rip), %rsi     # 0xfd8c9
leaq 0x138(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x37520
movq 0x30(%rsp), %rsi
leaq 0x736bd(%rip), %rdx     # 0xfd8c6
leaq 0x158(%rsp), %rdi
callq 0xcbd0
jmp 0x8a218
movq 0x1c0(%rsp), %rdi
callq 0x49000
movl %eax, 0x2c(%rsp)
jmp 0x8a22b
movl 0x2c(%rsp), %esi
leaq 0x118(%rsp), %rdi
callq 0xc7b0
jmp 0x8a23e
leaq 0x178(%rsp), %rdi
leaq 0x158(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0xc630
jmp 0x8a25d
leaq 0x118(%rsp), %rdi
callq 0x8728
leaq 0x158(%rsp), %rdi
callq 0x8728
leaq 0x138(%rsp), %rdi
callq 0x8728
movq 0x1b8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x8d6d0
jmp 0x8a29b
movl $0x50, %edi
callq 0x8340
movq %rax, 0x20(%rsp)
jmp 0x8a2ac
movq 0x20(%rsp), %rdi
movq 0x1b8(%rsp), %rdx
leaq 0x178(%rsp), %rsi
callq 0x5c720
jmp 0x8a2c8
leaq 0x178(%rsp), %rdi
callq 0x8728
jmp 0x8a391
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a334
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a327
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x8728
leaq 0x158(%rsp), %rdi
callq 0x8728
leaq 0x138(%rsp), %rdi
callq 0x8728
jmp 0x8a5bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a37f
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
movl $0x50, %esi
callq 0x8350
leaq 0x178(%rsp), %rdi
callq 0x8728
jmp 0x8a5bb
movq 0x1c0(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movq 0x1c0(%rsp), %rdi
addq $0x8, %rdi
callq 0x1b070
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x8d790
movq 0x18(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0x8d720
movq %rax, 0x100(%rsp)
jmp 0x8a59f
movq 0x308(%rsp), %rdx
addq $0x8, %rdx
leaq 0x734c7(%rip), %rsi     # 0xfd8c9
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x37520
movq 0x10(%rsp), %rsi
leaq 0x734a6(%rip), %rdx     # 0xfd8c6
leaq 0xc0(%rsp), %rdi
callq 0xcbd0
jmp 0x8a42f
movq 0x1c0(%rsp), %rdi
callq 0x49000
movl %eax, 0xc(%rsp)
jmp 0x8a442
movl 0xc(%rsp), %esi
leaq 0x80(%rsp), %rdi
callq 0xc7b0
jmp 0x8a455
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0xc630
jmp 0x8a474
leaq 0x80(%rsp), %rdi
callq 0x8728
leaq 0xc0(%rsp), %rdi
callq 0x8728
leaq 0xa0(%rsp), %rdi
callq 0x8728
movq 0x1b8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x8d6d0
jmp 0x8a4b2
movl $0x50, %edi
callq 0x8340
movq %rax, (%rsp)
jmp 0x8a4c2
movq (%rsp), %rdi
movq 0x1b8(%rsp), %rdx
leaq 0xe0(%rsp), %rsi
callq 0x5c720
jmp 0x8a4dd
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x8a59f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a549
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a53c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x8728
leaq 0xc0(%rsp), %rdi
callq 0x8728
leaq 0xa0(%rsp), %rdi
callq 0x8728
jmp 0x8a5bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x8a590
movq (%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
movl $0x50, %esi
callq 0x8350
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x8a5bb
jmp 0x8a5a1
leaq 0x1d0(%rsp), %rdi
callq 0x49330
jmp 0x89fee
addq $0x318, %rsp            # imm = 0x318
retq
movq 0x2c8(%rsp), %rdi
callq 0x8670
nopl (%rax,%rax)