// File: luooofan[P]hblzbd_compiler/src/Pass/dead_code_eliminate.cpp
// Function: DeadCodeEliminate::DeleteDeadFunc(SSAModule*)
void DeadCodeEliminate::DeleteDeadFunc(SSAModule* m) {
#ifdef DEBUG_DEAD_CODE_ELIMINATE_PROCESS
  std::cout << "Try To Remove Function: " << std::endl;
#endif
  std::unordered_set<SSAFunction*> worklist;
  for (auto func : m->GetFuncList()) {
    if ("main" == func->GetFuncName()) continue;
    worklist.insert(func);
  }
  while (!worklist.empty()) {
    auto func = *worklist.begin();
    worklist.erase(func);
    if (!func->IsCalled()) {
      // 应该把该函数调用的函数加入工作表
      for (auto call_func : func->GetCallFuncList()) {
        if ("main" == call_func->GetFuncName()) continue;
        worklist.insert(call_func);
      }
      // 说明该函数没被调用过 是dead function可以被删除 应该维护的信息全交由Remove函数完成
#ifdef DEBUG_DEAD_CODE_ELIMINATE_PROCESS
      std::cout << "  Remove Function: " << func->GetFuncName() << std::endl;
#endif
      func->Remove();
      delete func;
    }
  }
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x50250
movq 0x128(%rsp), %rdi
callq 0xa0ee0
movq %rax, 0x68(%rsp)
jmp 0x9f3e8
movq 0x68(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x624d0
movq %rax, 0xd0(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x62500
movq %rax, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x62530
testb $0x1, %al
jne 0x9f43d
jmp 0x9f4fc
leaq 0xd0(%rsp), %rdi
callq 0x62550
movq (%rax), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x62ad0
movq %rax, 0x60(%rsp)
jmp 0x9f469
movq 0x60(%rsp), %rsi
leaq 0x5b662(%rip), %rdi     # 0xfaad7
callq 0x37680
movb %al, 0x5f(%rsp)
jmp 0x9f480
movb 0x5f(%rsp), %al
testb $0x1, %al
jne 0x9f48a
jmp 0x9f4b2
jmp 0x9f4ea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x62bf0
jmp 0x9f6d0
leaq 0xf0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x62980
movb %dl, 0x4f(%rsp)
movq %rax, 0x50(%rsp)
jmp 0x9f4d2
movb 0x4f(%rsp), %al
movq 0x50(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
movb %al, 0xb8(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x62570
jmp 0x9f41f
jmp 0x9f4fe
leaq 0xf0(%rsp), %rdi
callq 0xa0ef0
xorb $-0x1, %al
testb $0x1, %al
jne 0x9f516
jmp 0x9f6bb
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x62a30
movq %rax, 0xa0(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x62ab0
movq 0x40(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
leaq 0xa8(%rsp), %rsi
callq 0xa0f10
jmp 0x9f55c
movq 0xa8(%rsp), %rdi
callq 0x61560
movb %al, 0x3f(%rsp)
jmp 0x9f56f
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x9f6b6
jmp 0x9f57d
movq 0xa8(%rsp), %rdi
callq 0xa0f40
movq %rax, 0x30(%rsp)
jmp 0x9f591
movq 0x30(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x62a30
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0x62a60
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x62a90
testb $0x1, %al
jne 0x9f5e6
jmp 0x9f679
leaq 0x90(%rsp), %rdi
callq 0x62ab0
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
callq 0x62ad0
movq %rax, 0x28(%rsp)
jmp 0x9f612
movq 0x28(%rsp), %rsi
leaq 0x5b4b9(%rip), %rdi     # 0xfaad7
callq 0x37680
movb %al, 0x27(%rsp)
jmp 0x9f629
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x9f633
jmp 0x9f635
jmp 0x9f667
leaq 0xf0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x62980
movb %dl, 0x17(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x9f655
movb 0x17(%rsp), %al
movq 0x18(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movb %al, 0x78(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x62ae0
jmp 0x9f5c8
movq 0xa8(%rsp), %rdi
callq 0x61580
jmp 0x9f688
movq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x9f6b4
movq 0x8(%rsp), %rdi
callq 0x61730
movq 0x8(%rsp), %rdi
movl $0xb8, %esi
callq 0x8350
jmp 0x9f6b6
jmp 0x9f4fe
leaq 0xf0(%rsp), %rdi
callq 0x62bf0
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xe0(%rsp), %rdi
callq 0x8670
nopl (%rax)