// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %rdi, 0x10(%rsp)
movq 0x30(%rsi), %rax
addq $0x30, %rsi
movq %rsi, 0x18(%rsp)
cmpq %rsi, %rax
je 0x49338
addq $0x38, 0x10(%rsp)
leaq 0x45426(%rip), %rbx     # 0x8e3c8
movq %rax, 0x28(%rsp)
movq 0x10(%rax), %rax
leaq 0x28(%rax), %r13
movq %rax, 0x20(%rsp)
movq 0x28(%rax), %r14
cmpq %r13, %r14
je 0x49325
movq 0x10(%r14), %rdi
testq %rdi, %rdi
je 0x4931d
movq %rbx, %rsi
leaq 0x457e8(%rip), %rdx     # 0x8e7c0
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
je 0x4931d
movq (%r14), %r15
movq 0x8(%r15), %r14
cmpq %r13, %r15
je 0x4931d
movq %rax, %rbp
movq %r14, (%rsp)
movq 0x10(%r15), %r14
testq %r14, %r14
je 0x4924b
movq %r13, %r12
movq %r14, %rdi
leaq 0x453af(%rip), %r13     # 0x8e3c8
movq %r13, %rsi
leaq 0x4575d(%rip), %rdx     # 0x8e780
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
je 0x490d3
movq %rax, %rbx
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
shrq $0x4, %rax
decl %eax
movq 0x50(%rbx), %rcx
subq 0x48(%rbx), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
movq %r12, %r13
jne 0x4924b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x335d4
movq %rax, %r14
movq %rbp, %rdi
movl $0x1, %esi
callq 0x335d4
cmpq %rax, %r14
jne 0x4924b
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
movabsq $0xffffffff0, %rcx      # imm = 0xFFFFFFFF0
andq %rcx, %rax
cmpq $0x30, %rax
jne 0x49312
movq %rbp, %rdi
movl $0x2, %esi
callq 0x335d4
testq %rax, %rax
je 0x491f4
movq %rax, %rdi
leaq 0x453c3(%rip), %rsi     # 0x8e480
leaq 0x453e4(%rip), %rdx     # 0x8e4a8
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r14
jmp 0x491f7
movq %r14, %rdi
movq %r13, %rsi
leaq 0x455e0(%rip), %rdx     # 0x8e6c0
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
je 0x49137
movq %rax, %rdi
xorl %esi, %esi
callq 0x335d4
movq %rax, %rdi
leaq 0x45380(%rip), %rsi     # 0x8e480
leaq 0x453d1(%rip), %rdx     # 0x8e4d8
xorl %ecx, %ecx
callq 0x8320
movq 0x60(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x49b3a
testq %rax, %rax
movq %r12, %r13
jne 0x4924b
jmp 0x49312
movq %r14, %rdi
movq %r13, %rsi
leaq 0x4567c(%rip), %rdx     # 0x8e7c0
xorl %ecx, %ecx
callq 0x8320
testq %rax, %rax
movq %r12, %r13
je 0x4924b
movq %rax, %rbx
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
shrq $0x4, %rax
movq 0x50(%rbx), %rcx
subq 0x48(%rbx), %rcx
shrq $0x4, %rcx
cmpl %ecx, %eax
jne 0x4924b
movq %rbx, %rdi
movl $0x1, %esi
callq 0x335d4
movq %rax, %r14
movq %rbp, %rdi
movl $0x1, %esi
callq 0x335d4
cmpq %rax, %r14
jne 0x4924b
movq 0x50(%rbp), %rax
subq 0x48(%rbp), %rax
movabsq $0xffffffff0, %rcx      # imm = 0xFFFFFFFF0
andq %rcx, %rax
cmpq $0x30, %rax
jne 0x492d6
movq %rbp, %rdi
movl $0x2, %esi
callq 0x335d4
testq %rax, %rax
je 0x4925c
movq %rax, %rdi
leaq 0x452a1(%rip), %rsi     # 0x8e480
leaq 0x452c2(%rip), %rdx     # 0x8e4a8
xorl %ecx, %ecx
callq 0x8320
movq %rax, 0x8(%rsp)
jmp 0x49265
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x335d4
testq %rax, %rax
je 0x49312
movq %rax, %rdi
leaq 0x45269(%rip), %rsi     # 0x8e480
leaq 0x4528a(%rip), %rdx     # 0x8e4a8
xorl %ecx, %ecx
callq 0x8320
testq %r14, %r14
leaq 0x45199(%rip), %rbx     # 0x8e3c8
je 0x49319
testq %rax, %rax
je 0x49319
movl 0x48(%r14), %ecx
cmpl 0x48(%rax), %ecx
je 0x49312
movq (%r15), %r15
cmpq %r13, %r15
jne 0x48fff
jmp 0x49312
movq $0x0, 0x8(%rsp)
movq %rbx, %rdi
movl $0x2, %esi
callq 0x335d4
testq %rax, %rax
je 0x49294
movq %rax, %rdi
leaq 0x451ff(%rip), %rsi     # 0x8e480
leaq 0x45220(%rip), %rdx     # 0x8e4a8
xorl %ecx, %ecx
callq 0x8320
movq %rax, %r12
jmp 0x49297
xorl %r12d, %r12d
movq %rbp, %rdi
movl $0x2, %esi
callq 0x335d4
movq %rax, %r14
movq %rbx, %rdi
movl $0x2, %esi
callq 0x335d4
cmpq %rax, %r14
movq 0x8(%rsp), %rax
je 0x492d6
testq %rax, %rax
je 0x4924b
testq %r12, %r12
je 0x4924b
movl 0x48(%rax), %eax
cmpl 0x48(%r12), %eax
jne 0x4924b
movq (%rsp), %r14
movq (%r14), %rbx
movq 0x20(%rsp), %rax
decq 0x38(%rax)
movq %r14, %rdi
callq 0x84d0
movl $0x18, %esi
movq %r14, %rdi
callq 0x82a0
movq 0x8(%rbx), %r14
movq (%rbp), %rax
movq %rbp, %rdi
callq *0x8(%rax)
leaq 0x450b8(%rip), %rbx     # 0x8e3c8
jmp 0x4931d
leaq 0x450af(%rip), %rbx     # 0x8e3c8
movq (%rsp), %r14
movq (%r14), %r14
jmp 0x48fb8
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpq 0x18(%rsp), %rax
jne 0x48fa2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop