// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/dead_function_eliminate.cpp
// Function: DeadFunctionEliminate::Run()
void DeadFunctionEliminate::Run() {
  auto m = dynamic_cast<IRModule *>(*m_);
  IRLivenessAnalysis livenessanalysis = IRLivenessAnalysis(m_);
  livenessanalysis.Run();

  std::unordered_set<std::string> dead_ret_function;

  for (auto func_item : ir::gFuncTable) {
    if (func_item.second.ret_type_ == INT && func_item.first != "main") {
      dead_ret_function.insert(func_item.first);
    }
  }

  // #ifdef PRINT_ALL_INT_FUNCTION
  //   for(auto func_name : dead_ret_function){
  //     std::cout << func_name << std::endl;
  //   }
  // #endif

  for (auto func : m->func_list_) {
    for (auto bb : func->bb_list_) {
      for (auto ir : bb->ir_list_) {
        if (ir->op_ == ir::IR::OpKind::CALL) {
#ifdef PRINT_CALL_IR
          ir->PrintIR();
#endif
          auto func_table_item = ir::gFuncTable[ir->opn1_.name_];
          if (func_table_item.ret_type_ == INT) {
#ifdef PRINT_DEBUG
            if (!isLiveness(bb, ir, ir->res_)) {
              ir->PrintIR();
              printf("/*********   不活跃，可以删除\n");
            } else if (dead_ret_function.find(ir->opn1_.name_) != dead_ret_function.end()) {
              ir->PrintIR();
              printf("/*********   活跃，不可以删除\n");
            }
#endif
            if (!isLiveness(bb, ir, ir->res_)) {
            } else {
              dead_ret_function.erase(ir->opn1_.name_);
              // std::cout << ir->opn1_.name_ << std::endl;
            }
          }
        }
      }  // for ir
    }    // for bb
  }      // for func

#ifdef PRINT_DEAD_RET_FUNCTION
  print_set(dead_ret_function);
#endif

  for (auto func : m->func_list_) {
    if (dead_ret_function.find(func->name_) != dead_ret_function.end()) {
      ir::gFuncTable[func->name_].ret_type_ = VOID;
      for (auto bb : func->bb_list_) {
        for (int i = 0; i < bb->ir_list_.size(); ++i) {
          auto ir = bb->ir_list_[i];

          if (ir->op_ == ir::IR::OpKind::RET) {
            bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::RET);
          }
        }
      }
    }
  }

#ifdef PRINT_FUNCTABLE
  ir::PrintFuncTable();
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x8(%rdi), %rbx
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x3ab8b
leaq 0x44ad0(%rip), %rsi     # 0x7f648
leaq 0x454f1(%rip), %rdx     # 0x80070
xorl %ecx, %ecx
callq 0x82c0
movq %rax, %r13
jmp 0x3ab8e
xorl %r13d, %r13d
leaq 0xc0(%rsp), %rax
movq %rbx, -0x18(%rax)
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
movb %cl, 0x10(%rax)
leaq 0x4605f(%rip), %rcx     # 0x80c10
addq $0x10, %rcx
movq %rcx, -0x20(%rax)
leaq 0xa0(%rsp), %rdi
callq 0x46e8e
leaq 0x98(%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)
leaq 0x4696d(%rip), %r12     # 0x81560
addq $0x10, %r12
leaq 0x28(%rsp), %rbx
leaq 0x1bebd(%rip), %r14     # 0x56ac0
leaq 0x68(%rsp), %r15
movq (%r12), %r12
testq %r12, %r12
je 0x3ac4c
leaq 0x8(%r12), %rsi
movq %rbx, %rdi
callq 0x3ae9e
cmpl $0x121, 0x48(%rsp)      # imm = 0x121
jne 0x3ac42
movq %rbx, %rdi
movq %r14, %rsi
callq 0xa45f
testb %al, %al
je 0x3ac42
movq %r15, %rdi
movq %rbx, %rsi
callq 0x392a2
movq %rbx, %rdi
callq 0x1a656
jmp 0x3ac08
movq 0x28(%r13), %rax
movq %r13, 0x10(%rsp)
movq 0x30(%r13), %rcx
movq %rcx, 0x18(%rsp)
leaq 0xd8(%rsp), %rbp
cmpq 0x18(%rsp), %rax
je 0x3ad4a
movq %rax, 0x20(%rsp)
movq (%rax), %rax
movq 0x30(%rax), %rcx
movq 0x38(%rax), %rax
movq %rax, (%rsp)
cmpq (%rsp), %rcx
je 0x3ad3c
movq %rcx, 0x8(%rsp)
movq (%rcx), %rbx
movq 0x8(%rbx), %r12
movq 0x10(%rbx), %r13
cmpq %r13, %r12
je 0x3ad2e
movq (%r12), %r14
cmpl $0x9, (%r14)
jne 0x3ad25
leaq 0x10(%r14), %r15
leaq 0x468a3(%rip), %rdi     # 0x81560
movq %r15, %rsi
callq 0x2f4de
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x1b0f4
cmpl $0x121, 0x28(%rsp)      # imm = 0x121
jne 0x3ad1b
leaq 0x88(%r14), %rsi
movq %rbp, %rdi
callq 0x1da22
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x3a9b0
movl %eax, %r14d
leaq 0xe0(%rsp), %rdi
callq 0x8578
testb %r14b, %r14b
je 0x3ad1b
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x3b18e
leaq 0x30(%rsp), %rdi
callq 0x1b336
addq $0x8, %r12
jmp 0x3ac9f
movq 0x8(%rsp), %rcx
addq $0x8, %rcx
jmp 0x3ac85
movq 0x20(%rsp), %rax
addq $0x8, %rax
jmp 0x3ac66
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
movq %rax, (%rsp)
pushq $0xa
popq %rbp
cmpq (%rsp), %rcx
je 0x3ae03
movq %rcx, 0x8(%rsp)
movq (%rcx), %rbx
leaq 0x8(%rbx), %r15
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x38ee2
testq %rax, %rax
je 0x3adf5
leaq 0x467d3(%rip), %rdi     # 0x81560
movq %r15, %rsi
callq 0x2f4de
movl $0x122, (%rax)          # imm = 0x122
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %rbx
cmpq %rbx, %r14
je 0x3adf5
movq (%r14), %r13
xorl %r12d, %r12d
movq 0x8(%r13), %rax
movq 0x10(%r13), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x3adef
movq (%rax,%r12,8), %rax
cmpl $0xa, (%rax)
jne 0x3adea
movl $0xe0, %edi
callq 0x8250
movq %rax, %r15
movq %rax, %rdi
movl %ebp, %esi
callq 0x1ada6
movq 0x8(%r13), %rax
movq %r15, (%rax,%r12,8)
incq %r12
jmp 0x3adae
addq $0x8, %r14
jmp 0x3ada3
movq 0x8(%rsp), %rcx
addq $0x8, %rcx
jmp 0x3ad5e
leaq 0x68(%rsp), %rdi
callq 0x1aeba
leaq 0xa0(%rsp), %rdi
callq 0x311ae
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3ae88
jmp 0x3ae7b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x1a656
jmp 0x3ae7e
jmp 0x3ae7b
jmp 0x3ae7b
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x8578
jmp 0x3ae6f
movq %rax, %rbx
movl $0xe0, %esi
movq %r15, %rdi
callq 0x8260
jmp 0x3ae7e
jmp 0x3ae7b
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1b336
jmp 0x3ae7e
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x1aeba
leaq 0xa0(%rsp), %rdi
callq 0x311ae
movq %rbx, %rdi
callq 0x84d0
nop