// File: /workspace/github_cpp_makefile_star2_O1/luooofan[P]hblzbd_compiler/src/Pass/dead_code_eliminate.cpp
// Function: DeadCodeEliminate::DeleteDeadStore(SSAFunction*)
void DeadCodeEliminate::DeleteDeadStore(SSAFunction* func) {
#ifdef DEBUG_DEAD_STORE_ELIMINATE_PROCESS
  std::cout << "Dead Store Eliminate for Function: " << func->GetFuncName() << std::endl;
#endif

  for (auto bb : func->GetBasicBlocks()) {
#ifdef DEBUG_DEAD_STORE_ELIMINATE_PROCESS
    std::cout << "  BB: " << bb->GetLabel() << std::endl;
#endif

    for (auto it = bb->GetInstList().begin(); it != bb->GetInstList().end(); ++it) {
      auto store_inst = dynamic_cast<StoreInst*>(*it);
      if (nullptr != store_inst) {
        auto it2 = ++it;
        --it;
        for (; it2 != bb->GetInstList().end(); ++it2) {
          // 如果是一条load指令 并且操作同一块内存区域 除非两个都是立即数且不相同 否则直接break
          if (auto src_inst = dynamic_cast<LoadInst*>(*it2)) {
            if (store_inst->GetNumOperands() - 1 == src_inst->GetNumOperands() &&
                src_inst->GetOperand(0) == store_inst->GetOperand(1)) {
              if (store_inst->GetNumOperands() == 3) {
                auto constint = dynamic_cast<ConstantInt*>(store_inst->GetOperand(2));
                auto constint2 = dynamic_cast<ConstantInt*>(src_inst->GetOperand(1));
                if (nullptr != constint && nullptr != constint2 && constint->GetImm() != constint2->GetImm()) {
                  continue;
                }
              }
              break;
            }
          }
          // 如果是一条call指令 并且调用的是有副作用的函数 直接break
          else if (auto src_inst = dynamic_cast<CallInst*>(*it2)) {
            if (!no_side_effect_funcs.count(dynamic_cast<FunctionValue*>(src_inst->GetOperand(0))->GetFunction()))
              break;
          }
          // 如果是一条操作同一内存位置的store指令 则前面这条store可删除
          else if (auto src_inst = dynamic_cast<StoreInst*>(*it2)) {
            if (store_inst->GetNumOperands() == src_inst->GetNumOperands() &&
                src_inst->GetOperand(1) == store_inst->GetOperand(1)) {
              if (store_inst->GetNumOperands() == 3) {
                auto constint = dynamic_cast<ConstantInt*>(store_inst->GetOperand(2));
                auto constint2 = dynamic_cast<ConstantInt*>(src_inst->GetOperand(2));
                if (store_inst->GetOperand(2) == src_inst->GetOperand(2) ||
                    (nullptr != constint && nullptr != constint2 && constint->GetImm() == constint2->GetImm())) {
#ifdef DEBUG_DEAD_STORE_ELIMINATE_PROCESS
                  std::cout << "  find dead store: ", store_inst->Print(std::cout);
                  std::cout << "               ==> ", src_inst->Print(std::cout);
#endif

                  it = bb->GetInstList().erase(it);
                  --it;
                  delete store_inst;
                  break;
                }
              } else {
#ifdef DEBUG_DEAD_STORE_ELIMINATE_PROCESS
                std::cout << "  find dead store: ", store_inst->Print(std::cout);
                std::cout << "               ==> ", src_inst->Print(std::cout);
#endif

                it = bb->GetInstList().erase(it);
                --it;
                delete store_inst;
                break;
              }
            }
          }
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, 0x20(%rsp)
movq 0x30(%rsi), %rax
addq $0x30, %rsi
movq %rsi, 0x28(%rsp)
cmpq %rsi, %rax
je 0x49cc7
addq $0x38, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movq 0x10(%rax), %rax
leaq 0x28(%rax), %rcx
movq %rax, 0x38(%rsp)
movq 0x28(%rax), %r12
movq %rcx, 0x8(%rsp)
cmpq %rcx, %r12
je 0x49cb4
movq 0x10(%r12), %rdi
testq %rdi, %rdi
je 0x49945
leaq 0x45ac6(%rip), %rsi     # 0x8f3f8
leaq 0x45eb7(%rip), %rdx     # 0x8f7f0
xorl %ecx, %ecx
callq 0x8320
movq %rax, %rbp
jmp 0x49947
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x49ca5
movq (%r12), %r13
movq 0x8(%r13), %r12
cmpq 0x8(%rsp), %r13
je 0x49ca5
movq 0x10(%r13), %r14
testq %r14, %r14
je 0x49989
movq %r14, %rdi
leaq 0x45a82(%rip), %rsi     # 0x8f3f8
leaq 0x45e33(%rip), %rdx     # 0x8f7b0
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r15
jmp 0x4998c
xorl %r15d, %r15d
testq %r15, %r15
je 0x49a33
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
shrq $0x4, %rax
decl %eax
movq 0x50(%r15), %rcx
subq 0x48(%r15), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jne 0x49b71
xorl %ebx, %ebx
movq %r15, %rdi
xorl %esi, %esi
callq 0x33480
movq %rax, %r14
movq %rbp, %rdi
movl $0x1, %esi
callq 0x33480
cmpq %rax, %r14
jne 0x49c98
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
movabsq $0xffffffff0, %rcx      # imm = 0xFFFFFFFF0
andq %rcx, %rax
movb $0x1, %bl
cmpq $0x30, %rax
jne 0x49c98
movq %rbp, %rdi
movl $0x2, %esi
callq 0x33480
testq %rax, %rax
je 0x49b21
movq %rax, %rdi
leaq 0x45a93(%rip), %rsi     # 0x8f4b0
leaq 0x45ab4(%rip), %rdx     # 0x8f4d8
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r14
jmp 0x49b24
testq %r14, %r14
je 0x49a52
movq %r14, %rdi
leaq 0x459b6(%rip), %rsi     # 0x8f3f8
leaq 0x45ca7(%rip), %rdx     # 0x8f6f0
xorl %ecx, %ecx
callq 0x8320
jmp 0x49a54
xorl %eax, %eax
testq %rax, %rax
je 0x49aa6
movq %rax, %rdi
xorl %esi, %esi
callq 0x33480
movq %rax, %rdi
leaq 0x45a43(%rip), %rsi     # 0x8f4b0
leaq 0x45a94(%rip), %rdx     # 0x8f508
xorl %ecx, %ecx
callq 0x8320
movq 0x60(%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4a504
testq %rax, %rax
jne 0x49c90
movl $0x7, %ebx
jmp 0x49c92
testq %r14, %r14
je 0x49ac8
movq %r14, %rdi
leaq 0x45943(%rip), %rsi     # 0x8f3f8
leaq 0x45d34(%rip), %rdx     # 0x8f7f0
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r15
jmp 0x49acb
xorl %r15d, %r15d
xorl %ebx, %ebx
movb $0x1, %dl
testq %r15, %r15
je 0x49c8c
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
shrq $0x4, %rax
movq 0x50(%r15), %rcx
subq 0x48(%r15), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jne 0x49c8c
movq %r15, %rdi
movl $0x1, %esi
callq 0x33480
movq %rax, %r14
movq %rbp, %rdi
movl $0x1, %esi
callq 0x33480
cmpq %rax, %r14
je 0x49b78
movb $0x1, %dl
jmp 0x49c8c
xorl %r14d, %r14d
movq %r15, %rdi
movl $0x1, %esi
callq 0x33480
testq %rax, %rax
je 0x49b50
movq %rax, %rdi
leaq 0x45970(%rip), %rsi     # 0x8f4b0
leaq 0x45991(%rip), %rdx     # 0x8f4d8
xorl %ecx, %ecx
callq 0x8320
jmp 0x49b52
xorl %eax, %eax
testq %r14, %r14
je 0x49c98
testq %rax, %rax
je 0x49c98
movl 0x48(%r14), %ecx
cmpl 0x48(%rax), %ecx
je 0x49c98
xorl %ebx, %ebx
jmp 0x49c98
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
movabsq $0xffffffff0, %rcx      # imm = 0xFFFFFFFF0
andq %rcx, %rax
cmpq $0x30, %rax
jne 0x49c55
movq %rbp, %rdi
movl $0x2, %esi
callq 0x33480
testq %rax, %rax
je 0x49bc8
movq %rax, %rdi
leaq 0x458fd(%rip), %rsi     # 0x8f4b0
leaq 0x4591e(%rip), %rdx     # 0x8f4d8
xorl %ecx, %ecx
callq 0x8320
movq %rax, 0x18(%rsp)
jmp 0x49bd1
movq $0x0, 0x18(%rsp)
movq %r15, %rdi
movl $0x2, %esi
callq 0x33480
testq %rax, %rax
je 0x49c02
movq %rax, %rdi
leaq 0x458c3(%rip), %rsi     # 0x8f4b0
leaq 0x458e4(%rip), %rdx     # 0x8f4d8
xorl %ecx, %ecx
callq 0x8320
movq %rax, 0x10(%rsp)
jmp 0x49c0b
movq $0x0, 0x10(%rsp)
movq %rbp, %rdi
movl $0x2, %esi
callq 0x33480
movq %rax, %r14
movq %r15, %rdi
movl $0x2, %esi
callq 0x33480
cmpq %rax, %r14
je 0x49c55
movq 0x18(%rsp), %rax
testq %rax, %rax
je 0x49b1a
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x49b1a
movl 0x48(%rax), %eax
cmpl 0x48(%rcx), %eax
jne 0x49b1a
movq (%r12), %rbx
movq 0x38(%rsp), %rax
decq 0x38(%rax)
movq %r12, %rdi
callq 0x84d0
movl $0x18, %esi
movq %r12, %rdi
callq 0x82a0
movq 0x8(%rbx), %r12
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
movl $0x7, %ebx
xorl %edx, %edx
testb %dl, %dl
je 0x49c92
xorl %ebx, %ebx
cmpl $0x7, %ebx
sete %bl
testb %bl, %bl
jne 0x49ca5
movq (%r13), %r13
jmp 0x49958
movq (%r12), %r12
cmpq 0x8(%rsp), %r12
jne 0x49921
movq 0x30(%rsp), %rax
movq (%rax), %rax
cmpq 0x28(%rsp), %rax
jne 0x498fd
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq