// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/convert_ssa.cpp
// Function: ConvertSSA::ConstructSSA(IRModule*)
SSAModule* ConvertSSA::ConstructSSA(IRModule* module) {
  // 由ir module直接构建ssa module 构建的过程中把ir中的label语句删掉 转化成了基本块的label
  // NOTE: 可能会出现一些空的bb 有些基本块是unnamed
  SSAModule* ssamodule = new SSAModule(module->global_scope_, module->name_);  // FIXME: terrible design
  std::unordered_map<IRFunction*, SSAFunction*> func_map;

  // process all global variable
  this->ProcessGlobalVariable(module, ssamodule);
  this->AddBuiltInFunction(ssamodule);

  // for every ir func
  for (auto func : module->func_list_) {
#ifdef DEBUG_CONSTRUCT_SSA_PROCESS
    std::cout << "--Process Func:" << func->name_ << std::endl;
#endif

    SSAFunction* ssafunc = new SSAFunction(func->name_, ssamodule);
    func_map.insert({func, ssafunc});
    glob_map[func->name_] = new FunctionValue(func->name_, ssafunc);

    work_map.clear();
    std::unordered_map<IRBasicBlock*, SSABasicBlock*> bb_map;
    this->GenerateSSABasicBlocks(func, ssafunc, bb_map);

    // for every ir basicblock
    // NOTE: 需要保证转化SSA时dom tree未发生变化 每次新填充到glob map和work map中的不能删除 为了之后填充phi结点用
    std::deque<IRBasicBlock*> q;
    if (!func->bb_list_.empty()) q.push_back(func->bb_list_.front());
    while (!q.empty()) {
      auto bb = q.front();
      q.pop_front();
      ConstructSSA4BB(bb, ssafunc, bb_map);
      for (auto dom : bb->doms_) {
        q.push_back(dom);
      }
    }

    // for every ir basicblock
    // for (auto bb : func->bb_list_) {
    //   ConstructSSA4BB(bb, ssafunc, bb_map);
    // }

    FillPhiInst(func, ssafunc, bb_map);
  }  // end of ir function loop

  return ssamodule;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movq %rdi, %r14
movl $0x98, %edi
callq 0x8250
movq %rax, %r12
movq 0x40(%rbx), %r15
leaq 0x8(%rbx), %rsi
leaq 0x70(%rsp), %rdi
callq 0x8390
leaq 0x70(%rsp), %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x35b9a
leaq 0x70(%rsp), %rdi
callq 0x8578
leaq 0x110(%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 %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x32af0
movq %r14, %rdi
movq %r12, %rsi
callq 0x32b9c
movq %r12, 0x8(%rsp)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
movq %rcx, 0x20(%rsp)
leaq 0xe0(%r14), %rcx
movq %rcx, 0x18(%rsp)
leaq 0xa8(%r14), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x90(%rsp), %rbp
leaq 0x30(%rsp), %rbx
cmpq 0x20(%rsp), %rax
je 0x325a5
movq %rax, 0x28(%rsp)
movq (%rax), %rax
movq %rax, (%rsp)
movl $0xb8, %edi
callq 0x8250
movq %rax, %r12
movq (%rsp), %r13
leaq 0x8(%r13), %r15
movq %rax, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq 0x35c42
movq %r13, 0x90(%rsp)
movq %r12, 0x98(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x3770e
pushq $0x68
popq %rdi
callq 0x8250
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x268e2
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x36e30
movq %r13, (%rax)
movq 0x10(%rsp), %rdi
callq 0x37a40
leaq 0x68(%rsp), %rax
movq %rax, 0x38(%rsp)
movq $0x1, 0x40(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0x58(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq %r14, %rdi
movq (%rsp), %rsi
movq %r12, %rdx
leaq 0x38(%rsp), %rcx
callq 0x3330c
movq %rbp, %rdi
callq 0x3637a
movq (%rsp), %rax
movq 0x30(%rax), %rsi
cmpq 0x38(%rax), %rsi
je 0x32517
movq %rbp, %rdi
callq 0x35d06
movq 0xa0(%rsp), %rax
cmpq %rax, 0xc0(%rsp)
je 0x32574
movq (%rax), %r15
movq %rbp, %rdi
callq 0x35d28
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
leaq 0x38(%rsp), %rcx
callq 0x33844
movq 0x258(%r15), %r13
movq 0x260(%r15), %r15
cmpq %r15, %r13
je 0x32517
movq (%r13), %rax
movq %rax, 0x30(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x35d06
addq $0x8, %r13
jmp 0x32555
movq %r14, %rdi
movq (%rsp), %rsi
leaq 0x38(%rsp), %rcx
callq 0x3488c
movq %rbp, %rdi
callq 0x37aa2
leaq 0x38(%rsp), %rdi
callq 0x36552
movq 0x28(%rsp), %rax
addq $0x8, %rax
jmp 0x32430
leaq 0xe0(%rsp), %rdi
callq 0x365fa
movq 0x8(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x8578
jmp 0x325db
movq %rax, %rbx
movl $0x98, %esi
movq %r12, %rdi
callq 0x8260
jmp 0x32642
jmp 0x32614
jmp 0x325f9
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r13, %rdi
jmp 0x32609
movq %rax, %rbx
jmp 0x3262b
movq %rax, %rbx
movl $0xb8, %esi
movq %r12, %rdi
callq 0x8260
jmp 0x32635
jmp 0x32614
jmp 0x3261b
movq %rax, %rbx
jmp 0x32635
jmp 0x3261b
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x37aa2
leaq 0x38(%rsp), %rdi
callq 0x36552
leaq 0xe0(%rsp), %rdi
callq 0x365fa
movq %rbx, %rdi
callq 0x84d0