// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/simplify_armcode.cpp
// Function: SimplifyArm::Run()
void SimplifyArm::Run() {
  auto m = dynamic_cast<ArmModule*>(*(this->m_));
  MyAssert(nullptr != m);
  for (auto func : m->func_list_) {
    for (auto bb_it = func->bb_list_.begin(); bb_it != func->bb_list_.end(); ++bb_it) {
      // for (auto bb : func->bb_list_) {
      auto bb = *bb_it;
      for (auto it = bb->inst_list_.begin(); it != bb->inst_list_.end();) {
        // (*it)->EmitCode(std::cout);
        // Move语句 两种简化可能
        if (auto src_inst = dynamic_cast<Move*>(*it)) {
          // 1 删除自己到自己的mov语句
          auto mov_op2 = src_inst->op2_;
          if (!mov_op2->is_imm_ && nullptr == mov_op2->shift_ && src_inst->rd_->reg_id_ == mov_op2->reg_->reg_id_) {
            // 删了之后检查下一条指令
            it = bb->inst_list_.erase(it);
            continue;
          }
          // 2 带偏移的 rd只作为一个无偏移的op2被使用的 所有使用和定值都在一个块内的 mov语句 可以和其所有使用合为一句
          // NOTE: 可以在一定程度上更利于寄存器分配 TODO: 需要确保使用中间没有新的定值
          // e.g. mov rd, rn LSL #2; add rd2, rn2, rd; -> add rd2, rn2, rn LSL #2
          //      mov rd, rn LSL #2; ldr/str rd2, [rn2, rd]; -> ldr/str rd2, [rn2, rn LSL #2]
          auto mov_rd = src_inst->rd_;
          if (mov_op2->HasShift() && mov_rd->reg_id_ > 15 /*4?*/ &&
              src_inst->cond_ == Cond::AL) {  // NOTE HERE! 预着色的寄存器可能有其他作用
            bool can_delete = true;
            for (auto used_inst : mov_rd->GetUsedInsts()) {
              // 如果有一条语句不是把它用作一个没有偏移的op2 那么就不执行替换
              if (!used_inst->HasUsedAsOp2WithoutShift(mov_rd) || used_inst->parent_ != bb) {
                can_delete = false;
                break;
              }
            }
            if (can_delete) {
              // bb->EmitCode(std::cout);
              // 替换所有使用
              for (auto used_inst : mov_rd->GetUsedInsts()) {
                used_inst->ReplaceOp2With(mov_op2);
              }
              // 删除move语句
              // std::cout << "will delete:", src_inst->EmitCode(std::cout);
              it = bb->inst_list_.erase(it);
              continue;
            }
          }
          ++it;
        }
        // 3 代数化简
        else if (auto src_inst = dynamic_cast<BinaryInst*>(*it)) {
          // 2.1 add 0 sub 0 转换成mov指令 但是不能在sp_fixup中的指令
          if (src_inst->opcode_ == BinaryInst::OpCode::ADD || src_inst->opcode_ == BinaryInst::OpCode::SUB) {
            if (src_inst->op2_->is_imm_ && 0 == src_inst->op2_->imm_num_ && !func->sp_fixup_.count(src_inst)) {
              it = bb->inst_list_.erase(it);
              it = bb->inst_list_.insert(
                  it, new Move(false, Cond::AL, src_inst->rd_, new Operand2(src_inst->rn_), bb, false, false));
              // new Move(src_inst->rd_, new Operand2(src_inst->rn_), src_inst);
              // it = std::find(bb->inst_list_.begin(), bb->inst_list_.end(), src_inst);
              // MyAssert(it != bb->inst_list_.end());
              // it = bb->inst_list_.erase(it);
              // 继续检查新加的这条mov指令
              continue;
            }
          }
          ++it;
        }
        // 4 无条件跳转到下一个基本的跳转语句化简
        else if (auto src_inst = dynamic_cast<Branch*>(*it)) {
          if (src_inst->cond_ == Cond::AL && bb_it + 1 != func->bb_list_.end() &&
              src_inst->label_ == (*(bb_it + 1))->label_) {
            it = bb->inst_list_.erase(it);
            continue;
          }
          ++it;
        }
        // 其他无法简化直接跳过
        else {
          ++it;
        }
      }
      // std::cout << "BB End" << std::endl;
    }
    // std::cout << "Function End" << std::endl;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x8(%rdi), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x540b2
leaq 0x2b883(%rip), %rsi     # 0x7f648
leaq 0x2b864(%rip), %rdx     # 0x7f630
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x540b2
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
movq %rax, 0x30(%rsp)
leaq 0x2b808(%rip), %rbx     # 0x7f5f8
cmpq 0x30(%rsp), %rcx
je 0x540a3
movq (%rcx), %rax
movq 0x38(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq %rax, 0x28(%rsp)
addq $0xa0, %rax
movq %rax, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rdx
cmpq 0x40(%rax), %rdx
je 0x5409a
movq %rdx, %rax
movq (%rdx), %rcx
leaq 0x30(%rcx), %rdx
movq %rdx, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq 0x30(%rcx), %r12
addq $0x8, %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x38(%rax), %r12
je 0x54090
movq (%r12), %r15
testq %r15, %r15
je 0x53fec
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x2b6be(%rip), %rdx     # 0x7f538
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x53eaa
movq 0x18(%rax), %r15
movq 0x20(%rax), %rbp
movq 0x10(%rbp), %rcx
cmpb $0x0, (%rbp)
je 0x53f7f
testq %rcx, %rcx
jne 0x53f84
jmp 0x5401e
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x2b621(%rip), %rdx     # 0x7f4d8
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x53fcd
movq %rax, %rbp
cmpl $0x1, 0x14(%rax)
ja 0x53fec
movq 0x30(%rbp), %rax
cmpb $0x1, (%rax)
jne 0x53fec
cmpl $0x0, 0x4(%rax)
jne 0x53fec
movq %rbp, 0x20(%rsp)
movq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x54116
testq %rax, %rax
jne 0x53fec
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x30460
movq %rax, %r14
pushq $0x28
popq %rdi
callq 0x8250
movq %rax, %r12
movq 0x20(%rbp), %r15
pushq $0x18
popq %rdi
callq 0x8250
movq 0x28(%rbp), %rcx
movb $0x0, (%rax)
andl $0x0, 0x4(%rax)
movq %rcx, 0x8(%rax)
andq $0x0, 0x10(%rax)
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r15, %rcx
movq %rax, %r8
movq 0x18(%rsp), %r9
pushq $0x0
pushq $0x0
callq 0x2d762
popq %rax
popq %rcx
movq %r12, 0x20(%rsp)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
leaq 0x20(%rsp), %rdx
callq 0xad0a
movq %rax, %r12
jmp 0x53e51
testq %rcx, %rcx
je 0x53ff5
cmpl $0x10, 0x38(%r15)
jl 0x5401e
cmpl $0x0, 0x10(%rax)
jne 0x5401e
leaq 0x10(%r15), %r13
movq %r13, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x5405c
movq 0x8(%rbx), %r14
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x28(%rax)
testb %al, %al
je 0x5401e
movq 0x18(%rsp), %rax
cmpq %rax, 0x8(%r14)
je 0x53fa0
jmp 0x5401e
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x2b576(%rip), %rdx     # 0x7f550
xorl %ecx, %ecx
callq 0x82c0
testq %rax, %rax
je 0x53fec
cmpl $0x0, 0x10(%rax)
je 0x5402e
addq $0x8, %r12
jmp 0x53e51
movl 0x38(%r15), %eax
movq 0x8(%rbp), %rcx
cmpl 0x38(%rcx), %eax
jne 0x5401e
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x30460
movq %rax, %r12
leaq 0x2b5df(%rip), %rbx     # 0x7f5f8
jmp 0x53e51
addq $0x8, %r12
leaq 0x2b5cf(%rip), %rbx     # 0x7f5f8
jmp 0x53e51
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %rdx
cmpq 0x40(%rcx), %rdx
je 0x53fec
addq $0x18, %rax
movq 0x10(%rsp), %rcx
movq (%rcx), %rsi
addq $0x8, %rsi
movq %rax, %rdi
callq 0xe1ca
testb %al, %al
je 0x53fec
jmp 0x5407b
leaq 0x2b595(%rip), %rbx     # 0x7f5f8
movq (%r13), %r13
testq %r13, %r13
je 0x5407b
movq 0x8(%r13), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
callq *0x30(%rax)
jmp 0x54063
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x30460
movq %rax, %r12
jmp 0x53e51
movq 0x40(%rsp), %rcx
jmp 0x53e1c
addq $0x8, %rcx
jmp 0x53df0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2cf2f(%rip), %rdi     # 0x80fe8
leaq 0x2028(%rip), %rsi      # 0x560e8
callq 0x8240
leaq 0x5ace(%rip), %rsi      # 0x59b9a
movq %rax, %rdi
callq 0x8240
leaq 0x3245(%rip), %rsi      # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0x9
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
movq %rax, %rbx
pushq $0x28
popq %rsi
movq %r12, %rdi
callq 0x8260
movq %rbx, %rdi
callq 0x84d0