// File: /workspace/github_cpp_makefile_star2_O3/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 0x4a008
leaq 0x43654(%rip), %rsi     # 0x8d648
leaq 0x44075(%rip), %rdx     # 0x8e070
xorl %ecx, %ecx
callq 0x8320
movq %rax, (%rsp)
jmp 0x4a010
movq $0x0, (%rsp)
leaq 0x88(%rsp), %rcx
movq %rbx, -0x18(%rcx)
movq %rcx, -0x10(%rcx)
movq $0x0, -0x8(%rcx)
xorl %eax, %eax
movb %al, (%rcx)
movb %al, 0x10(%rcx)
leaq 0x44bda(%rip), %rax     # 0x8ec10
addq $0x10, %rax
movq %rax, -0x20(%rcx)
leaq 0x68(%rsp), %rdi
callq 0x59bda
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)
leaq 0x4551b(%rip), %rax     # 0x8f590
movq 0x10(%rax), %rbp
testq %rbp, %rbp
je 0x4a102
leaq 0x50(%rsp), %rbx
leaq 0x38(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x24ac8(%rip), %r15     # 0x6eb60
leaq 0xe0(%rsp), %r12
leaq 0x8(%rbp), %rsi
movq %r14, %rdi
callq 0x4a3f2
cmpl $0x121, 0x48(%rsp)      # imm = 0x121
jne 0x4a0da
movq %r14, %rdi
movq %r15, %rsi
callq 0x8100
testl %eax, %eax
je 0x4a0da
movq %r12, 0x20(%rsp)
movq %r12, %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
callq 0x4859a
movq %rbx, %rdi
callq 0x25ee2
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x4a0f9
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x4a0a0
movq (%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %rcx
je 0x4a2f8
movq %rcx, 0x18(%rsp)
movq (%rcx), %rax
movq 0x30(%rax), %r15
movq 0x38(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %r15
je 0x4a219
movq (%r15), %rbp
movq 0x8(%rbp), %r12
movq 0x10(%rbp), %r14
cmpq %r14, %r12
je 0x4a20a
movq (%r12), %r13
cmpl $0x9, (%r13)
jne 0x4a201
leaq 0x10(%r13), %rbx
leaq 0x45428(%rip), %rdi     # 0x8f590
movq %rbx, %rsi
callq 0x3ccd4
movq (%rax), %rcx
movq %rcx, 0x28(%rsp)
addq $0x8, %rax
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x25cf8
cmpl $0x121, 0x28(%rsp)      # imm = 0x121
jne 0x4a1f7
leaq 0x88(%r13), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x28816
movq %rbp, %rdi
movq %r13, %rsi
leaq 0xa0(%rsp), %rdx
callq 0x49c61
movl %eax, %r13d
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4a1e2
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %r13b, %r13b
je 0x4a1f7
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4a760
leaq 0x30(%rsp), %rdi
callq 0x25ee2
addq $0x8, %r12
jmp 0x4a145
addq $0x8, %r15
cmpq 0x8(%rsp), %r15
jne 0x4a13a
movq 0x18(%rsp), %rcx
addq $0x8, %rcx
cmpq 0x10(%rsp), %rcx
jne 0x4a11c
movq (%rsp), %rax
movq 0x28(%rax), %r12
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %r12
je 0x4a2f8
movq (%r12), %rbx
leaq 0x8(%rbx), %r15
leaq 0xe0(%rsp), %rdi
movq %r15, %rsi
callq 0x4826a
testq %rax, %rax
je 0x4a2e9
leaq 0x45321(%rip), %rdi     # 0x8f590
movq %r15, %rsi
callq 0x3ccd4
movl $0x122, (%rax)          # imm = 0x122
movq 0x30(%rbx), %rbp
movq 0x38(%rbx), %r14
cmpq %r14, %rbp
je 0x4a2e9
movq (%rbp), %rbx
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
cmpq %rax, %rcx
je 0x4a2e3
xorl %r13d, %r13d
movq (%rax,%r13,8), %rdx
cmpl $0xa, (%rdx)
jne 0x4a2d1
movl $0xe0, %edi
callq 0x8290
movq %rax, %r15
movq %rax, %rdi
movl $0xa, %esi
callq 0x259da
movq 0x8(%rbx), %rax
movq %r15, (%rax,%r13,8)
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
incq %r13
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r13, %rdx
ja 0x4a29e
addq $0x8, %rbp
jmp 0x4a285
addq $0x8, %r12
cmpq 0x8(%rsp), %r12
jne 0x4a247
leaq 0xe0(%rsp), %rdi
callq 0x25b24
leaq 0x4474c(%rip), %rax     # 0x8ea58
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x4a333
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x82a0
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x4a3bc
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x24ed2
jmp 0x4a3af
jmp 0x4a3ac
jmp 0x4a3ac
jmp 0x4a3ac
jmp 0x4a3ac
movq %rax, %rbx
movl $0xe0, %esi
movq %r15, %rdi
callq 0x82a0
jmp 0x4a3af
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
je 0x4a3a0
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x4a3a0
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x25ee2
jmp 0x4a3af
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x25b24
leaq 0x44695(%rip), %rax     # 0x8ea58
movq %rax, 0x68(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0x4a3ea
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540