// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/dead_code_eliminate.cpp
// Function: DeadCodeEliminate::FindNoSideEffectFunc(SSAModule*, std::unordered_set<SSAFunction*, std::hash<SSAFunction*>, std::equal_to<SSAFunction*>, std::allocator<SSAFunction*>>&)
void DeadCodeEliminate::FindNoSideEffectFunc(SSAModule* m, std::unordered_set<SSAFunction*>& no_side_effect_funcs) {
  // NOTE: 这里并没有把副作用的信息记录到函数中
  // 没有副作用的函数为 1.函数自身没有对数组类型参数和全局量有赋值(store)行为 2.同时没有调用有副作用的函数
  // 具体来说 如果函数pointer类型的argument的使用没有store语句 用了的globalvariable的使用也只有load语句即满足第一点
  no_side_effect_funcs.clear();
  for (auto func : m->GetFuncList()) {
    no_side_effect_funcs.insert(func);
  }

  // 每个builtin函数都是有副作用的
  for (auto builtin_func : m->GetBuiltinFuncList()) {
    no_side_effect_funcs.insert(builtin_func);
    RemoveFromNoSideEffectFuncs(builtin_func, no_side_effect_funcs);
  }

  for (auto func : m->GetFuncList()) {
    if (no_side_effect_funcs.find(func) == no_side_effect_funcs.end()) continue;
    bool has_side_effect = false;
    for (Argument* arg : func->GetValue()->GetArgList()) {
      for (Use* use : arg->GetUses()) {
        if (nullptr != dynamic_cast<StoreInst*>(use->GetUser())) goto setflag;
      }
    }
    for (GlobalVariable* glo : func->GetUsedGlobVarList()) {
      for (Use* use : glo->GetUses()) {
        auto inst = dynamic_cast<SSAInstruction*>(use->GetUser());
        MyAssert(nullptr != inst);
        if (func == inst->GetParent()->GetFunction()) {
          if (nullptr != dynamic_cast<StoreInst*>(inst)) goto setflag;
        }
      }
    }
    continue;
  setflag:
    has_side_effect = true;
    // propagate to caller
    RemoveFromNoSideEffectFuncs(func, no_side_effect_funcs);
  }
#ifdef DEBUG_DEAD_CODE_ELIMINATE_PROCESS
  std::cout << "No side effect functions: ";
  for (auto func : no_side_effect_funcs) {
    std::cout << func->GetFuncName() << " ";
  }
  std::cout << std::endl;
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r12
movq %rdi, %r14
movq %rsi, %rdi
callq 0x28b9a
leaq 0x40(%r14), %rbx
movq %rsp, %r15
movq %rbx, 0x8(%rsp)
movq (%rbx), %rbx
cmpq 0x8(%rsp), %rbx
je 0x39905
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x296c4
jmp 0x398e6
addq $0x58, %r14
movq %rsp, %r15
movq %r14, %rbx
movq (%rbx), %rbx
cmpq %r14, %rbx
je 0x39938
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x296c4
movq (%rsp), %rdi
movq %r12, %rsi
callq 0x3a1ae
jmp 0x3990f
leaq 0x46c81(%rip), %r15     # 0x805c0
leaq 0x46e7a(%rip), %r13     # 0x807c0
movq 0x8(%rsp), %rbx
movq %r12, 0x18(%rsp)
movq (%rbx), %rbx
cmpq 0x8(%rsp), %rbx
je 0x39a6e
movq 0x10(%rbx), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %rsp, %rsi
callq 0x3a44c
testq %rax, %rax
je 0x39950
movq %rbx, 0x20(%rsp)
movq (%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rax), %rbx
addq $0x48, %rbx
movq %rbx, %r12
movq (%r12), %r12
cmpq %rbx, %r12
je 0x399e8
movq 0x10(%r12), %r14
addq $0x10, %r14
movq %r14, %rbp
movq (%rbp), %rbp
cmpq %r14, %rbp
je 0x3998f
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x399a4
movq %r15, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x399a4
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r12
movq %r12, %rsi
callq 0x3a1ae
movq 0x20(%rsp), %rbx
jmp 0x39950
movq 0x10(%rsp), %rax
leaq 0x90(%rax), %r14
movq (%r14), %r14
testq %r14, %r14
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rbx
je 0x39950
movq 0x8(%r14), %rbx
addq $0x10, %rbx
movq %rbx, %r12
movq (%r12), %r12
cmpq %rbx, %r12
je 0x399f4
movq 0x10(%r12), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x39a7d
movq %r15, %rsi
leaq 0x46992(%rip), %rdx     # 0x803c8
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x39a7d
movq 0x60(%rax), %rcx
movq 0x10(%rsp), %rdx
cmpq (%rcx), %rdx
jne 0x39a15
movq %rax, %rdi
leaq 0x4696e(%rip), %rsi     # 0x803c8
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x39a15
jmp 0x399cc
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x47564(%rip), %rdi     # 0x80fe8
leaq 0x1c65d(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x1fca2(%rip), %rsi     # 0x59739
movq %rax, %rdi
callq 0x8240
leaq 0x1d87a(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0x96, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
nop