// File: /workspace/github_cpp_makefile_star2_O1/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 0x8290
movq %rax, %r13
movq 0x40(%rbx), %r15
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0xca10
leaq 0x88(%rsp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x4591c
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x40b73
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x82a0
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 %r13, %rdx
callq 0x41572
movq %r14, %rdi
movq %r13, %rsi
callq 0x41670
movq %r13, 0x8(%rsp)
movq 0x28(%rbx), %rax
movq 0x30(%rbx), %rcx
movq %rcx, 0x70(%rsp)
cmpq %rcx, %rax
je 0x40e1b
leaq 0xe0(%r14), %rcx
movq %rcx, 0x68(%rsp)
leaq 0xa8(%r14), %rcx
movq %rcx, 0x60(%rsp)
leaq 0x10(%rsp), %rbp
leaq 0x80(%rsp), %rbx
movq %rax, 0x78(%rsp)
movq (%rax), %rax
movq %rax, (%rsp)
movl $0xb8, %edi
callq 0x8290
movq %rax, %r12
movq (%rsp), %r13
leaq 0x8(%r13), %r15
movq %rax, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
callq 0x45a5a
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x4733c
movl $0x68, %edi
callq 0x8290
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3339c
movq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x46b20
movq %r13, (%rax)
movq 0x60(%rsp), %rdi
callq 0x47626
leaq 0xd8(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq $0x1, 0xb0(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, 0xc8(%rsp) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq %r14, %rdi
movq (%rsp), %rsi
movq %r12, %rdx
leaq 0xa8(%rsp), %rcx
callq 0x42062
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
callq 0x461e8
movq (%rsp), %rax
movq 0x30(%rax), %rsi
cmpq 0x38(%rax), %rsi
je 0x40d18
movq 0x40(%rsp), %rax
movq 0x50(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x40d10
movq (%rsi), %rcx
movq %rcx, (%rax)
addq $0x8, 0x40(%rsp)
jmp 0x40d18
movq %rbp, %rdi
callq 0x476c4
movq 0x20(%rsp), %rax
cmpq %rax, 0x40(%rsp)
je 0x40dde
movq (%rax), %r15
movq 0x30(%rsp), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x40d3f
addq $0x8, %rax
jmp 0x40d71
movq 0x28(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
callq 0x82a0
movq 0x38(%rsp), %rax
leaq 0x8(%rax), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x28(%rsp)
leaq 0x200(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
leaq 0xa8(%rsp), %rcx
callq 0x42a42
movq 0x258(%r15), %r13
movq 0x260(%r15), %r15
cmpq %r15, %r13
je 0x40d18
movq (%r13), %rax
movq %rax, 0x80(%rsp)
movq 0x40(%rsp), %rcx
movq 0x50(%rsp), %rdx
addq $-0x8, %rdx
cmpq %rdx, %rcx
je 0x40dcd
movq %rax, (%rcx)
addq $0x8, 0x40(%rsp)
jmp 0x40dd8
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x476c4
addq $0x8, %r13
jmp 0x40d9a
movq %r14, %rdi
movq (%rsp), %rsi
leaq 0xa8(%rsp), %rcx
callq 0x43fb2
movq %rbp, %rdi
callq 0x47674
leaq 0xa8(%rsp), %rdi
callq 0x4635e
movq 0x78(%rsp), %rax
addq $0x8, %rax
cmpq 0x70(%rsp), %rax
jne 0x40bf2
leaq 0xe0(%rsp), %rdi
callq 0x463ce
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
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x40e64
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x40e64
movq %rax, %rbx
movl $0x98, %esi
movq %r13, %rdi
callq 0x82a0
jmp 0x40ecd
jmp 0x40e9f
movq %rax, %rbx
movl $0xb8, %esi
movq %r12, %rdi
jmp 0x40e8f
jmp 0x40e9f
movq %rax, %rbx
movl $0x68, %esi
movq %r13, %rdi
callq 0x82a0
jmp 0x40ec0
jmp 0x40e98
movq %rax, %rbx
jmp 0x40eb3
jmp 0x40ea6
movq %rax, %rbx
jmp 0x40ec0
jmp 0x40ea6
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x47674
leaq 0xa8(%rsp), %rdi
callq 0x4635e
leaq 0xe0(%rsp), %rdi
callq 0x463ce
movq %rbx, %rdi
callq 0x8540
nop