// File: luooofan[P]hblzbd_compiler/src/Pass/arm_liveness_analysis.cpp
// Function: GetDefUse(arm::Instruction*)
std::pair<std::vector<RegId>, std::vector<RegId>> GetDefUse(Instruction *inst) {
  std::vector<RegId> def;
  std::vector<RegId> use;

  auto process_op2 = [&use](Operand2 *op2) {
    if (nullptr == op2) return;
    if (op2->is_imm_) return;
    use.push_back(op2->reg_->reg_id_);
    if (nullptr != op2->shift_ && !op2->shift_->is_imm_) {
      use.push_back(op2->shift_->shift_reg_->reg_id_);
    }
    return;
  };

  if (auto src_inst = dynamic_cast<BinaryInst *>(inst)) {
    if (nullptr != src_inst->rd_) {
      def.push_back(src_inst->rd_->reg_id_);
    }
    use.push_back(src_inst->rn_->reg_id_);
    process_op2(src_inst->op2_);
  } else if (auto src_inst = dynamic_cast<Move *>(inst)) {
    def.push_back(src_inst->rd_->reg_id_);
    process_op2(src_inst->op2_);
  } else if (auto src_inst = dynamic_cast<Branch *>(inst)) {
    // call可先视为对前面已经定义的参数寄存器的使用 再视作对调用者保护的寄存器的定义 不管之后这些寄存器有没有被使用
    // caller save regs: r0-r3, lr, ip
    // ret可视为对r0和lr的使用 但需要保证armcode是正确的 i.e. 如果前面有bl xxx指令的话 后面不能用bx lr指令返回
    // 如果b后面是个函数label就是call 如果b后面是lr就是ret
    auto &label = src_inst->label_;
    if (src_inst->IsRet()) {  // bx lr
      use.push_back(static_cast<RegId>(ArmReg::r0));
      use.push_back(static_cast<RegId>(ArmReg::lr));
    } else if (src_inst->IsCall()) {  // call
      int callee_param_num = ir::gFuncTable[label].shape_list_.size();
      for (RegId i = 0; i < std::min(callee_param_num, 4); ++i) {
        use.push_back(i);
      }
      for (RegId i = 0; i < 4; ++i) {
        def.push_back(i);
      }
      def.push_back(static_cast<RegId>(ArmReg::ip));
      def.push_back(static_cast<RegId>(ArmReg::lr));
    }
  } else if (auto src_inst = dynamic_cast<LdrStr *>(inst)) {
    if (src_inst->opkind_ == LdrStr::OpKind::LDR) {
      def.push_back(src_inst->rd_->reg_id_);
    } else {
      use.push_back(src_inst->rd_->reg_id_);
    }
    use.push_back(src_inst->rn_->reg_id_);
    process_op2(src_inst->offset_);
  } else if (auto src_inst = dynamic_cast<LdrPseudo *>(inst)) {
    def.push_back(src_inst->rd_->reg_id_);
  } else if (auto src_inst = dynamic_cast<PushPop *>(inst)) {
    if (src_inst->opkind_ == PushPop::OpKind::PUSH) {
      for (auto reg : src_inst->reg_list_) {
        use.push_back(reg->reg_id_);
      }
    } else {
      for (auto reg : src_inst->reg_list_) {
        if (reg->reg_id_ == static_cast<RegId>(ArmReg::pc)) {
          // 此时作为ret语句 视为对r0的使用以及对pc的定义
          use.push_back(static_cast<RegId>(ArmReg::r0));
        }
        def.push_back(reg->reg_id_);
      }
    }
  } else {  // 未实现其他指令
    MyAssert(0);
  }

  return {def, use};
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x98(%rsp)
movq %rdi, %rax
movq %rax, 0xa0(%rsp)
movq %rdi, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x35d90
leaq 0x168(%rsp), %rdi
callq 0x35d90
leaq 0x168(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, %rax
je 0x7b493
movq 0xa8(%rsp), %rdi
leaq 0xcf10f(%rip), %rsi     # 0x14a588
leaq 0xcefe8(%rip), %rdx     # 0x14a468
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x90(%rsp)
jmp 0x7b49f
xorl %eax, %eax
movq %rax, 0x90(%rsp)
jmp 0x7b49f
movq 0x90(%rsp), %rax
movq %rax, 0x158(%rsp)
cmpq $0x0, 0x158(%rsp)
je 0x7b561
movq 0x158(%rsp), %rcx
xorl %eax, %eax
cmpq 0x20(%rcx), %rax
je 0x7b522
movq 0x158(%rsp), %rax
movq 0x20(%rax), %rsi
addq $0x38, %rsi
leaq 0x180(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b4ed
jmp 0x7b522
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x35c60
leaq 0x180(%rsp), %rdi
callq 0x35c60
jmp 0x7bc47
movq 0x158(%rsp), %rax
movq 0x28(%rax), %rsi
addq $0x38, %rsi
leaq 0x168(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b541
movq 0x158(%rsp), %rax
movq 0x30(%rax), %rsi
leaq 0x160(%rsp), %rdi
callq 0x7bc60
jmp 0x7b55c
jmp 0x7bbfe
movq 0x198(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, %rax
je 0x7b5a0
movq 0x88(%rsp), %rdi
leaq 0xcf002(%rip), %rsi     # 0x14a588
leaq 0xcef3b(%rip), %rdx     # 0x14a4c8
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x80(%rsp)
jmp 0x7b5ac
xorl %eax, %eax
movq %rax, 0x80(%rsp)
jmp 0x7b5ac
movq 0x80(%rsp), %rax
movq %rax, 0x140(%rsp)
cmpq $0x0, 0x140(%rsp)
je 0x7b606
movq 0x140(%rsp), %rax
movq 0x18(%rax), %rsi
addq $0x38, %rsi
leaq 0x180(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b5e6
movq 0x140(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x160(%rsp), %rdi
callq 0x7bc60
jmp 0x7b601
jmp 0x7bbfc
movq 0x198(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, %rax
je 0x7b63c
movq 0x78(%rsp), %rdi
leaq 0xcef63(%rip), %rsi     # 0x14a588
leaq 0xceeb4(%rip), %rdx     # 0x14a4e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x70(%rsp)
jmp 0x7b645
xorl %eax, %eax
movq %rax, 0x70(%rsp)
jmp 0x7b645
movq 0x70(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
je 0x7b82f
movq 0x138(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x130(%rsp)
movq 0x138(%rsp), %rdi
callq 0xb0f0
movb %al, 0x6f(%rsp)
jmp 0x7b688
movb 0x6f(%rsp), %al
testb $0x1, %al
jne 0x7b692
jmp 0x7b6db
movl $0x0, 0x12c(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x12c(%rsp), %rsi
callq 0x36ea0
jmp 0x7b6b4
movl $0xe, 0x128(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x36ea0
jmp 0x7b6d6
jmp 0x7b82a
movq 0x138(%rsp), %rdi
callq 0xb070
movb %al, 0x6e(%rsp)
jmp 0x7b6ee
movb 0x6e(%rsp), %al
testb $0x1, %al
jne 0x7b6fb
jmp 0x7b828
movq 0x130(%rsp), %rsi
leaq 0xd0f36(%rip), %rdi     # 0x14c640
callq 0x7d0d0
movq %rax, 0x60(%rsp)
jmp 0x7b716
movq 0x60(%rsp), %rdi
addq $0x8, %rdi
callq 0x186d0
movl %eax, 0x124(%rsp)
movl $0x0, 0x120(%rsp)
movl 0x120(%rsp), %eax
movl %eax, 0x54(%rsp)
movl $0x4, 0x11c(%rsp)
leaq 0x124(%rsp), %rdi
leaq 0x11c(%rsp), %rsi
callq 0x7d100
movq %rax, 0x58(%rsp)
jmp 0x7b768
movl 0x54(%rsp), %eax
movq 0x58(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x7b7a1
leaq 0x168(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x1f2a0
jmp 0x7b78c
jmp 0x7b78e
movl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x120(%rsp)
jmp 0x7b736
movl $0x0, 0x118(%rsp)
cmpl $0x4, 0x118(%rsp)
jge 0x7b7e2
leaq 0x180(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x1f2a0
jmp 0x7b7cd
jmp 0x7b7cf
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x7b7ac
movl $0xc, 0x114(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x114(%rsp), %rsi
callq 0x36ea0
jmp 0x7b804
movl $0xe, 0x110(%rsp)
leaq 0x180(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x36ea0
jmp 0x7b826
jmp 0x7b828
jmp 0x7b82a
jmp 0x7bbfa
movq 0x198(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x7b865
movq 0x48(%rsp), %rdi
leaq 0xced3a(%rip), %rsi     # 0x14a588
leaq 0xceceb(%rip), %rdx     # 0x14a540
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x40(%rsp)
jmp 0x7b86e
xorl %eax, %eax
movq %rax, 0x40(%rsp)
jmp 0x7b86e
movq 0x40(%rsp), %rax
movq %rax, 0x108(%rsp)
cmpq $0x0, 0x108(%rsp)
je 0x7b919
movq 0x108(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x7b8b9
movq 0x108(%rsp), %rax
movq 0x20(%rax), %rsi
addq $0x38, %rsi
leaq 0x180(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b8b7
jmp 0x7b8da
movq 0x108(%rsp), %rax
movq 0x20(%rax), %rsi
addq $0x38, %rsi
leaq 0x168(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b8d8
jmp 0x7b8da
movq 0x108(%rsp), %rax
movq 0x28(%rax), %rsi
addq $0x38, %rsi
leaq 0x168(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b8f9
movq 0x108(%rsp), %rax
movq 0x38(%rax), %rsi
leaq 0x160(%rsp), %rdi
callq 0x7bc60
jmp 0x7b914
jmp 0x7bbf8
movq 0x198(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x7b94f
movq 0x38(%rsp), %rdi
leaq 0xcec50(%rip), %rsi     # 0x14a588
leaq 0xcec19(%rip), %rdx     # 0x14a558
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x30(%rsp)
jmp 0x7b958
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0x7b958
movq 0x30(%rsp), %rax
movq %rax, 0x100(%rsp)
cmpq $0x0, 0x100(%rsp)
je 0x7b994
movq 0x100(%rsp), %rax
movq 0x18(%rax), %rsi
addq $0x38, %rsi
leaq 0x180(%rsp), %rdi
callq 0x1f2a0
jmp 0x7b98f
jmp 0x7bbf6
movq 0x198(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x7b9ca
movq 0x28(%rsp), %rdi
leaq 0xcebd5(%rip), %rsi     # 0x14a588
leaq 0xcebb6(%rip), %rdx     # 0x14a570
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x20(%rsp)
jmp 0x7b9d3
xorl %eax, %eax
movq %rax, 0x20(%rsp)
jmp 0x7b9d3
movq 0x20(%rsp), %rax
movq %rax, 0xf8(%rsp)
cmpq $0x0, 0xf8(%rsp)
je 0x7bb79
movq 0xf8(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x7baa3
movq 0xf8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xcdb0
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0xce60
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xce20
testb $0x1, %al
jne 0x7ba5a
jmp 0x7ba9e
leaq 0xe8(%rsp), %rdi
callq 0xce90
movq (%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rsi
addq $0x38, %rsi
leaq 0x168(%rsp), %rdi
callq 0x1f2a0
jmp 0x7ba8d
jmp 0x7ba8f
leaq 0xe8(%rsp), %rdi
callq 0xcea0
jmp 0x7ba3f
jmp 0x7bb77
movq 0xf8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0xcdb0
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
callq 0xce60
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0xce20
testb $0x1, %al
jne 0x7bafc
jmp 0x7bb75
leaq 0xc8(%rsp), %rdi
callq 0xce90
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpl $0xf, 0x38(%rax)
jne 0x7bb46
movl $0x0, 0xb4(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0xb4(%rsp), %rsi
callq 0x36ea0
jmp 0x7bb44
jmp 0x7bb46
movq 0xb8(%rsp), %rsi
addq $0x38, %rsi
leaq 0x180(%rsp), %rdi
callq 0x1f2a0
jmp 0x7bb61
jmp 0x7bb63
leaq 0xc8(%rsp), %rdi
callq 0xcea0
jmp 0x7bae1
jmp 0x7bb77
jmp 0x7bbf4
movq 0xd0468(%rip), %rdi     # 0x14bfe8
leaq 0x7e571(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x18(%rsp)
jmp 0x7bb93
movq 0x18(%rsp), %rdi
leaq 0x81b7e(%rip), %rsi     # 0xfd71d
callq 0x8330
movq %rax, 0x10(%rsp)
jmp 0x7bbab
movq 0x10(%rsp), %rdi
leaq 0x7f833(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x8(%rsp)
jmp 0x7bbc3
movq 0x8(%rsp), %rdi
movl $0x4a, %esi
callq 0x8650
movq %rax, (%rsp)
jmp 0x7bbd8
movq (%rsp), %rdi
movq 0xd039d(%rip), %rsi     # 0x14bf80
callq 0x8380
jmp 0x7bbea
movl $0xff, %edi
callq 0x8460
jmp 0x7bbf6
jmp 0x7bbf8
jmp 0x7bbfa
jmp 0x7bbfc
jmp 0x7bbfe
movq 0x98(%rsp), %rdi
leaq 0x180(%rsp), %rsi
leaq 0x168(%rsp), %rdx
callq 0x7d140
jmp 0x7bc1d
leaq 0x168(%rsp), %rdi
callq 0x35c60
leaq 0x180(%rsp), %rdi
callq 0x35c60
movq 0xa0(%rsp), %rax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0x150(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)