// File: 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:
subq $0x208, %rsp            # imm = 0x208
movq %rdi, 0x200(%rsp)
movq 0x200(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
cmpq $0x0, %rax
je 0xf66a5
movq 0xb0(%rsp), %rdi
leaq 0x53f4d(%rip), %rsi     # 0x14a5d8
leaq 0x53f2e(%rip), %rdx     # 0x14a5c0
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0xa8(%rsp)
jmp 0xf66b1
xorl %eax, %eax
movq %rax, 0xa8(%rsp)
jmp 0xf66b1
movq 0xa8(%rsp), %rax
movq %rax, 0x1f8(%rsp)
xorl %eax, %eax
cmpq 0x1f8(%rsp), %rax
jne 0xf6724
movq 0x55914(%rip), %rdi     # 0x14bfe8
leaq 0x3a1d(%rip), %rsi      # 0xfa0f8
callq 0x8330
movq %rax, %rdi
leaq 0x74f6(%rip), %rsi      # 0xfdbe0
callq 0x8330
movq %rax, %rdi
leaq 0x4cf1(%rip), %rsi      # 0xfb3ea
callq 0x8330
movq %rax, %rdi
movl $0x9, %esi
callq 0x8650
movq %rax, %rdi
movq 0x5586b(%rip), %rsi     # 0x14bf80
callq 0x8380
movl $0xff, %edi
callq 0x8460
movq 0x1f8(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x1f0(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x10b00
movq %rax, 0x1e8(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x10b30
movq %rax, 0x1e0(%rsp)
leaq 0x1e8(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x10b60
testb $0x1, %al
jne 0xf6780
jmp 0xf6ffd
leaq 0x1e8(%rsp), %rdi
callq 0x10ba0
movq (%rax), %rax
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rdi
addq $0x38, %rdi
callq 0x10bd0
movq %rax, 0x1d0(%rsp)
movq 0x1d8(%rsp), %rdi
addq $0x38, %rdi
callq 0x10c00
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x10c30
testb $0x1, %al
jne 0xf67e8
jmp 0xf6fe9
leaq 0x1d0(%rsp), %rdi
callq 0x10c70
movq (%rax), %rax
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rdi
addq $0x30, %rdi
callq 0xcaa0
movq %rax, 0x1b8(%rsp)
movq 0x1c0(%rsp), %rdi
addq $0x30, %rdi
callq 0xcad0
movq %rax, 0x1b0(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xcb00
testb $0x1, %al
jne 0xf6850
jmp 0xf6fd5
leaq 0x1b8(%rsp), %rdi
callq 0xf5c0
movq (%rax), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, %rax
je 0xf6897
movq 0xa0(%rsp), %rdi
leaq 0x53d0b(%rip), %rsi     # 0x14a588
leaq 0x53c44(%rip), %rdx     # 0x14a4c8
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x98(%rsp)
jmp 0xf68a3
xorl %eax, %eax
movq %rax, 0x98(%rsp)
jmp 0xf68a3
movq 0x98(%rsp), %rax
movq %rax, 0x1a8(%rsp)
cmpq $0x0, 0x1a8(%rsp)
je 0xf6b9b
movq 0x1a8(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rax
testb $0x1, (%rax)
jne 0xf6972
movq 0x1a0(%rsp), %rcx
xorl %eax, %eax
cmpq 0x10(%rcx), %rax
jne 0xf6972
movq 0x1a8(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x38(%rax), %eax
movq 0x1a0(%rsp), %rcx
movq 0x8(%rcx), %rcx
cmpl 0x38(%rcx), %eax
jne 0xf6972
movq 0x1c0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x90(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0xcb80
movq 0x90(%rsp), %rdi
movq 0x190(%rsp), %rsi
callq 0x80b80
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x1b8(%rsp)
jmp 0xf6819
movq 0x1a8(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x188(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0xe8e0
testb $0x1, %al
jne 0xf699c
jmp 0xf6b89
movq 0x188(%rsp), %rax
cmpl $0xf, 0x38(%rax)
jle 0xf6b89
movq 0x1a8(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xf6b89
movb $0x1, 0x187(%rsp)
movq 0x188(%rsp), %rdi
callq 0xf7020
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
callq 0xf7030
movq %rax, 0x170(%rsp)
movq 0x178(%rsp), %rdi
callq 0xf7060
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x80ce0
testb $0x1, %al
jne 0xf6a22
jmp 0xf6a87
leaq 0x170(%rsp), %rdi
callq 0xf7090
movq (%rax), %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rdi
movq 0x188(%rsp), %rsi
movq (%rdi), %rax
callq *0x28(%rax)
testb $0x1, %al
jne 0xf6a56
jmp 0xf6a6c
movq 0x160(%rsp), %rax
movq 0x8(%rax), %rax
cmpq 0x1c0(%rsp), %rax
je 0xf6a76
movb $0x0, 0x187(%rsp)
jmp 0xf6a87
jmp 0xf6a78
leaq 0x170(%rsp), %rdi
callq 0xf70b0
jmp 0xf6a07
testb $0x1, 0x187(%rsp)
je 0xf6b87
movq 0x188(%rsp), %rdi
callq 0xf7020
movq %rax, 0x158(%rsp)
movq 0x158(%rsp), %rdi
callq 0xf7030
movq %rax, 0x150(%rsp)
movq 0x158(%rsp), %rdi
callq 0xf7060
movq %rax, 0x148(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x80ce0
testb $0x1, %al
jne 0xf6aef
jmp 0xf6b2c
leaq 0x150(%rsp), %rdi
callq 0xf7090
movq (%rax), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rdi
movq 0x1a0(%rsp), %rsi
movq (%rdi), %rax
callq *0x30(%rax)
leaq 0x150(%rsp), %rdi
callq 0xf70b0
jmp 0xf6ad4
movq 0x1c0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x88(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0xcb80
movq 0x88(%rsp), %rdi
movq 0x130(%rsp), %rsi
callq 0x80b80
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x1b8(%rsp)
jmp 0xf6819
jmp 0xf6b89
leaq 0x1b8(%rsp), %rdi
callq 0xf8f0
jmp 0xf6fd0
leaq 0x1b8(%rsp), %rdi
callq 0xf5c0
movq (%rax), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, %rax
je 0xf6bdf
movq 0x80(%rsp), %rdi
leaq 0x539c0(%rip), %rsi     # 0x14a588
leaq 0x53899(%rip), %rdx     # 0x14a468
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x78(%rsp)
jmp 0xf6be8
xorl %eax, %eax
movq %rax, 0x78(%rsp)
jmp 0xf6be8
movq 0x78(%rsp), %rax
movq %rax, 0x128(%rsp)
cmpq $0x0, 0x128(%rsp)
je 0xf6e32
movq 0x128(%rsp), %rax
cmpl $0x0, 0x14(%rax)
je 0xf6c24
movq 0x128(%rsp), %rax
cmpl $0x1, 0x14(%rax)
jne 0xf6e20
movq 0x128(%rsp), %rax
movq 0x30(%rax), %rcx
xorl %eax, %eax
testb $0x1, (%rcx)
movb %al, 0x77(%rsp)
je 0xf6c8d
movq 0x128(%rsp), %rax
movq 0x30(%rax), %rdx
xorl %eax, %eax
xorl %ecx, %ecx
cmpl 0x4(%rdx), %ecx
movb %al, 0x77(%rsp)
jne 0xf6c8d
movq 0x1d8(%rsp), %rdi
addq $0xa0, %rdi
movq 0x128(%rsp), %rax
movq %rax, 0x120(%rsp)
leaq 0x120(%rsp), %rsi
callq 0xf70e0
cmpq $0x0, %rax
setne %al
xorb $-0x1, %al
movb %al, 0x77(%rsp)
movb 0x77(%rsp), %al
testb $0x1, %al
jne 0xf6c9a
jmp 0xf6e1e
movq 0x1c0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x40(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
movq %rsi, 0x48(%rsp)
callq 0xcb80
movq 0x40(%rsp), %rdi
movq 0x110(%rsp), %rsi
callq 0x80b80
movq 0x48(%rsp), %rsi
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq 0x1c0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x50(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xcb80
movl $0x28, %edi
callq 0x8340
movq %rax, 0x58(%rsp)
movq 0x128(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x60(%rsp)
movl $0x18, %edi
callq 0x8340
movq %rax, 0x68(%rsp)
jmp 0xf6d43
movq 0x68(%rsp), %rdi
movq 0x128(%rsp), %rax
movq 0x28(%rax), %rsi
callq 0x78090
jmp 0xf6d5b
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %rcx
movq 0x58(%rsp), %rdi
movq 0x1c0(%rsp), %r9
movq %rsp, %rax
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
xorl %edx, %edx
movl %edx, %esi
callq 0x77f30
jmp 0xf6d8d
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x100(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0xcb40
movq %rax, 0x108(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x1b8(%rsp)
jmp 0xf6819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
jmp 0xf6e0a
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf0(%rsp)
movl %eax, 0xec(%rsp)
movl $0x18, %esi
callq 0x8350
movq 0x58(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0xf7005
jmp 0xf6e20
leaq 0x1b8(%rsp), %rdi
callq 0xf8f0
jmp 0xf6fce
leaq 0x1b8(%rsp), %rdi
callq 0xf5c0
movq (%rax), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0xf6e70
movq 0x38(%rsp), %rdi
leaq 0x5372f(%rip), %rsi     # 0x14a588
leaq 0x53680(%rip), %rdx     # 0x14a4e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x30(%rsp)
jmp 0xf6e79
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0xf6e79
movq 0x30(%rsp), %rax
movq %rax, 0xe0(%rsp)
cmpq $0x0, 0xe0(%rsp)
je 0xf6fbf
movq 0xe0(%rsp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x10(%rcx)
movb %al, 0x2f(%rsp)
jne 0xf6f51
leaq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x81b90
movq %rax, 0xd8(%rsp)
movq 0x1d8(%rsp), %rdi
addq $0x38, %rdi
callq 0x10c00
movq %rax, 0xd0(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x10c30
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x2f(%rsp)
jne 0xf6f04
jmp 0xf6f51
movq 0xe0(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x20(%rsp)
leaq 0x1d0(%rsp), %rdi
movl $0x1, %esi
callq 0x81b90
movq %rax, 0xc8(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x10c70
movq 0x20(%rsp), %rdi
movq (%rax), %rsi
addq $0x8, %rsi
callq 0x1a650
movb %al, 0x2f(%rsp)
movb 0x2f(%rsp), %al
testb $0x1, %al
jne 0xf6f5b
jmp 0xf6fb0
movq 0x1c0(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x18(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0xcb80
movq 0x18(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x80b80
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x1b8(%rsp)
jmp 0xf6819
leaq 0x1b8(%rsp), %rdi
callq 0xf8f0
jmp 0xf6fcc
leaq 0x1b8(%rsp), %rdi
callq 0xf8f0
jmp 0xf6fce
jmp 0xf6fd0
jmp 0xf6819
jmp 0xf6fd7
leaq 0x1d0(%rsp), %rdi
callq 0x10c80
jmp 0xf67b1
jmp 0xf6feb
leaq 0x1e8(%rsp), %rdi
callq 0x10bb0
jmp 0xf6762
addq $0x208, %rsp            # imm = 0x208
retq
movq 0xf0(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)
nopl (%rax)