// File: /workspace/github_cpp_makefile_star2_O3/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 $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movq %rdi, %r14
movl $0x98, %edi
callq 0x8290
movq %rax, %r13
movq 0x40(%rbx), %r15
leaq 0xa8(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
leaq 0x98(%rsp), %rdi
callq 0xc6dc
leaq 0x98(%rsp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x45188
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x403d5
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x120(%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 %r13, %rdx
callq 0x40dee
movq %r14, %rdi
movq %r13, %rsi
callq 0x40eec
movq %r14, 0x10(%rsp)
movq %r13, 0x18(%rsp)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
movq %rcx, 0x80(%rsp)
cmpq %rcx, %rax
je 0x406b2
movq 0x10(%rsp), %rdx
leaq 0xe0(%rdx), %rcx
movq %rcx, 0x78(%rsp)
leaq 0xa8(%rdx), %rcx
movq %rcx, 0x70(%rsp)
leaq 0x20(%rsp), %rbp
leaq 0xb8(%rsp), %r13
leaq 0x90(%rsp), %rbx
movq %r13, %r14
movq %rax, 0x88(%rsp)
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movl $0xb8, %edi
callq 0x8290
movq %rax, %r12
movq 0x8(%rsp), %r13
leaq 0x8(%r13), %r15
movq %rax, %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
callq 0x452c6
movq %r13, 0x20(%rsp)
movq %r12, 0x28(%rsp)
leaq 0xf0(%rsp), %rdi
movq %rbp, %rsi
callq 0x46b7c
movl $0x68, %edi
callq 0x8290
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x334f0
movq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0x463ba
movq %r13, (%rax)
movq 0x70(%rsp), %rdi
callq 0x46e0e
leaq 0xe8(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq $0x1, 0xc0(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0xd8(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %r14, %r13
movq %r14, %rcx
callq 0x418aa
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x45a7c
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rsi
movq 0x50(%rsp), %rcx
cmpq 0x38(%rax), %rsi
je 0x405a6
movq 0x60(%rsp), %rax
addq $-0x8, %rax
cmpq %rax, %rcx
je 0x40599
movq (%rsi), %rax
movq %rax, (%rcx)
movq 0x50(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x50(%rsp)
jmp 0x405a6
movq %rbp, %rdi
callq 0x46eb4
movq 0x50(%rsp), %rcx
movq 0x30(%rsp), %rax
cmpq %rax, %rcx
je 0x40676
movq (%rax), %r15
movq 0x40(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x405cb
addq $0x8, %rax
jmp 0x405fd
movq 0x38(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x82a0
movq 0x48(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x48(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x40(%rsp)
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x422b0
movq 0x258(%r15), %r13
movq 0x260(%r15), %r15
cmpq %r15, %r13
je 0x40663
movq (%r13), %rax
movq %rax, 0x90(%rsp)
movq 0x50(%rsp), %rcx
movq 0x60(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x40652
movq %rax, (%rcx)
addq $0x8, 0x50(%rsp)
jmp 0x4065d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x46eb4
addq $0x8, %r13
jmp 0x40623
movq 0x30(%rsp), %rax
cmpq %rax, 0x50(%rsp)
movq %r14, %r13
jne 0x405b4
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rcx
callq 0x43808
movq %rbp, %rdi
callq 0x46e5c
movq %r13, %rdi
callq 0x45bf2
movq 0x88(%rsp), %rax
addq $0x8, %rax
cmpq 0x80(%rsp), %rax
jne 0x40469
leaq 0xf0(%rsp), %rdi
callq 0x45c62
movq 0x18(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x98(%rsp), %rdi
cmpq %r12, %rdi
je 0x406fb
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x406fb
movq %rax, %rbx
movl $0x98, %esi
movq %r13, %rdi
callq 0x82a0
jmp 0x40764
jmp 0x40736
movq %rax, %rbx
movl $0xb8, %esi
movq %r12, %rdi
jmp 0x40726
jmp 0x40736
movq %rax, %rbx
movl $0x68, %esi
movq %r13, %rdi
callq 0x82a0
jmp 0x40757
jmp 0x4072f
movq %rax, %rbx
jmp 0x4074a
jmp 0x4073d
movq %rax, %rbx
jmp 0x40757
jmp 0x4073d
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x46e5c
leaq 0xb8(%rsp), %rdi
callq 0x45bf2
leaq 0xf0(%rsp), %rdi
callq 0x45c62
movq %rbx, %rdi
callq 0x8540