// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %r13
movq %rdi, %r14
movq %rsi, %rdi
callq 0x36272
leaq 0x40(%r14), %rbp
movq 0x40(%r14), %rbx
cmpq %rbp, %rbx
je 0x494ad
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x368b8
movq (%rbx), %rbx
cmpq %rbp, %rbx
jne 0x49489
movq %rbp, 0x18(%rsp)
movq 0x58(%r14), %rbx
addq $0x58, %r14
cmpq %r14, %rbx
je 0x494fa
leaq 0x8(%rsp), %r15
leaq 0x10(%rsp), %r12
movq 0x10(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x368b8
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x49ebc
movq (%rbx), %rbx
cmpq %r14, %rbx
jne 0x494c9
movq %r13, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r10
cmpq %rax, %r10
je 0x496d6
leaq 0x460d9(%rip), %r12     # 0x8f5f0
movq 0x10(%r10), %r13
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdi
movq %r13, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %rcx
movq (%rsi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0x49579
movq (%rdx), %r8
movq %rdx, %rax
cmpq 0x8(%r8), %r13
je 0x49579
movq %r8, %r9
movq (%r8), %r8
testq %r8, %r8
je 0x49577
movq 0x8(%r8), %rsi
movq %rsi, %rax
xorl %edx, %edx
divq %rdi
movl $0x0, %eax
cmpq %rcx, %rdx
jne 0x49579
movq %r9, %rax
cmpq %rsi, %r13
jne 0x4954c
jmp 0x49579
xorl %eax, %eax
movq %r10, 0x28(%rsp)
testq %rax, %rax
je 0x496c3
cmpq $0x0, (%rax)
je 0x496c3
movq 0x28(%r13), %r14
addq $0x48, %r14
movq %r14, 0x30(%rsp)
movq (%r14), %r14
cmpq 0x30(%rsp), %r14
je 0x49602
movq 0x10(%r14), %rbp
movq 0x10(%rbp), %r15
addq $0x10, %rbp
movl $0xa, %eax
cmpq %rbp, %r15
sete %bl
je 0x495fb
movq 0x10(%r15), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x495e4
movq %r12, %rsi
leaq 0x46218(%rip), %rdx     # 0x8f7f0
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
jne 0x495f6
movq (%r15), %r15
cmpq %rbp, %r15
sete %bl
jne 0x495c1
movl $0xa, %eax
jmp 0x495fb
movl $0xc, %eax
testb $0x1, %bl
jne 0x4959e
jmp 0x49607
movl $0x8, %eax
cmpl $0xc, %eax
je 0x496b6
cmpl $0x8, %eax
jne 0x496c3
leaq 0x90(%r13), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x496c3
movq 0x8(%rbx), %r14
addq $0x10, %r14
movq %r14, %rbp
movq (%rbp), %rbp
cmpq %r14, %rbp
je 0x496a3
movq 0x10(%rbp), %rax
movq 0x8(%rax), %rdi
testq %rdi, %rdi
je 0x49660
movq %r12, %rsi
leaq 0x45da1(%rip), %rdx     # 0x8f3f8
xorl %ecx, %ecx
callq 0x8320
jmp 0x49662
xorl %eax, %eax
testq %rax, %rax
je 0x496e5
movq 0x60(%rax), %rcx
cmpq (%rcx), %r13
je 0x49677
xorl %eax, %eax
movb $0x1, %r15b
jmp 0x4969c
xorl %r15d, %r15d
movq %rax, %rdi
leaq 0x45d74(%rip), %rsi     # 0x8f3f8
leaq 0x46165(%rip), %rdx     # 0x8f7f0
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
je 0x49670
movl $0xc, %eax
testb %r15b, %r15b
jne 0x49637
jmp 0x496a8
movl $0xf, %eax
cmpl $0xf, %eax
je 0x49620
cmpl $0xc, %eax
jne 0x496c3
movq %r13, %rdi
movq 0x20(%rsp), %rsi
callq 0x49ebc
movq 0x28(%rsp), %r10
movq (%r10), %r10
cmpq 0x18(%rsp), %r10
jne 0x49517
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x46904(%rip), %rdi     # 0x8fff0
leaq 0x25a75(%rip), %rsi     # 0x6f168
callq 0x8280
leaq 0x290d6(%rip), %rsi     # 0x727d5
movq %rax, %rdi
callq 0x8280
leaq 0x26cb2(%rip), %rsi     # 0x703c0
movq %rax, %rdi
callq 0x8280
movq %rax, %rdi
movl $0x96, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
nop