// File: luooofan[P]hblzbd_compiler/src/Pass/arm_liveness_analysis.cpp
// Function: GetDefUsePtr(arm::Instruction*)
std::pair<std::vector<Reg *>, std::vector<Reg *>> GetDefUsePtr(Instruction *inst) {
  std::vector<Reg *> def;
  std::vector<Reg *> use;

  auto process_op2 = [&use](Operand2 *op2) {
    if (nullptr == op2) return;
    if (op2->is_imm_) return;
    use.push_back(op2->reg_);
    if (nullptr != op2->shift_ && !op2->shift_->is_imm_) {
      use.push_back(op2->shift_->shift_reg_);
    }
    return;
  };

  if (auto src_inst = dynamic_cast<BinaryInst *>(inst)) {
    if (nullptr != src_inst->rd_) {
      def.push_back(src_inst->rd_);
    }
    use.push_back(src_inst->rn_);
    process_op2(src_inst->op2_);
  } else if (auto src_inst = dynamic_cast<Move *>(inst)) {
    def.push_back(src_inst->rd_);
    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(new Reg(ArmReg::r0));
      use.push_back(new Reg(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(new Reg(i));
      }
      for (RegId i = 0; i < 4; ++i) {
        def.push_back(new Reg(i));
      }
      def.push_back(new Reg(ArmReg::ip));
      def.push_back(new Reg(ArmReg::lr));
    }
  } else if (auto src_inst = dynamic_cast<LdrStr *>(inst)) {
    if (src_inst->opkind_ == LdrStr::OpKind::LDR) {
      def.push_back(src_inst->rd_);
    } else {
      use.push_back(src_inst->rd_);
    }
    use.push_back(src_inst->rn_);
    process_op2(src_inst->offset_);
  } else if (auto src_inst = dynamic_cast<LdrPseudo *>(inst)) {
    def.push_back(src_inst->rd_);
  } 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);
      }
    } else {
      for (auto reg : src_inst->reg_list_) {
        if (reg->reg_id_ == static_cast<RegId>(ArmReg::pc)) {
          // 此时作为ret语句 视为对r0的使用以及对pc的定义
          use.push_back(new Reg(ArmReg::r0));
        }
        def.push_back(reg);
      }
    }
  } else {  // 未实现其他指令
    MyAssert(0);
  }

  return {def, use};
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0xe0(%rsp)
movq %rdi, %rax
movq %rax, 0xe8(%rsp)
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0x7d1b0
leaq 0x1d8(%rsp), %rdi
callq 0x7d1b0
leaq 0x1d8(%rsp), %rax
movq %rax, 0x1d0(%rsp)
movq 0x208(%rsp), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, %rax
je 0x7bd83
movq 0xf0(%rsp), %rdi
leaq 0xce81f(%rip), %rsi     # 0x14a588
leaq 0xce6f8(%rip), %rdx     # 0x14a468
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0xd8(%rsp)
jmp 0x7bd8f
xorl %eax, %eax
movq %rax, 0xd8(%rsp)
jmp 0x7bd8f
movq 0xd8(%rsp), %rax
movq %rax, 0x1c8(%rsp)
cmpq $0x0, 0x1c8(%rsp)
je 0x7be2f
movq 0x1c8(%rsp), %rcx
xorl %eax, %eax
cmpq 0x20(%rcx), %rax
je 0x7bdf4
movq 0x1c8(%rsp), %rsi
addq $0x20, %rsi
leaq 0x1f0(%rsp), %rdi
callq 0x7d1c0
jmp 0x7bdd9
jmp 0x7bdf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0x7c779
movq 0x1c8(%rsp), %rsi
addq $0x28, %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x7d1c0
jmp 0x7be0f
movq 0x1c8(%rsp), %rax
movq 0x30(%rax), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x7c7a0
jmp 0x7be2a
jmp 0x7c730
movq 0x208(%rsp), %rax
movq %rax, 0xd0(%rsp)
cmpq $0x0, %rax
je 0x7be6e
movq 0xd0(%rsp), %rdi
leaq 0xce734(%rip), %rsi     # 0x14a588
leaq 0xce66d(%rip), %rdx     # 0x14a4c8
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0xc8(%rsp)
jmp 0x7be7a
xorl %eax, %eax
movq %rax, 0xc8(%rsp)
jmp 0x7be7a
movq 0xc8(%rsp), %rax
movq %rax, 0x1b0(%rsp)
cmpq $0x0, 0x1b0(%rsp)
je 0x7bed0
movq 0x1b0(%rsp), %rsi
addq $0x18, %rsi
leaq 0x1f0(%rsp), %rdi
callq 0x7d1c0
jmp 0x7beb0
movq 0x1b0(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x7c7a0
jmp 0x7becb
jmp 0x7c72e
movq 0x208(%rsp), %rax
movq %rax, 0xc0(%rsp)
cmpq $0x0, %rax
je 0x7bf0f
movq 0xc0(%rsp), %rdi
leaq 0xce693(%rip), %rsi     # 0x14a588
leaq 0xce5e4(%rip), %rdx     # 0x14a4e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0xb8(%rsp)
jmp 0x7bf1b
xorl %eax, %eax
movq %rax, 0xb8(%rsp)
jmp 0x7bf1b
movq 0xb8(%rsp), %rax
movq %rax, 0x1a8(%rsp)
cmpq $0x0, 0x1a8(%rsp)
je 0x7c32b
movq 0x1a8(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a8(%rsp), %rdi
callq 0xb0f0
movb %al, 0xb7(%rsp)
jmp 0x7bf64
movb 0xb7(%rsp), %al
testb $0x1, %al
jne 0x7bf74
jmp 0x7c06a
movl $0x40, %edi
callq 0x8340
movq %rax, 0xa8(%rsp)
jmp 0x7bf88
movq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x77be0
jmp 0x7bf99
movq 0xa8(%rsp), %rax
movq %rax, 0x198(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x7d230
jmp 0x7bfc0
movl $0x40, %edi
callq 0x8340
movq %rax, 0xa0(%rsp)
jmp 0x7bfd4
movq 0xa0(%rsp), %rdi
movl $0xe, %esi
callq 0x77be0
jmp 0x7bfe8
movq 0xa0(%rsp), %rax
movq %rax, 0x190(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x7d230
jmp 0x7c00f
jmp 0x7c326
movq 0xa8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
movq 0xa0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
movq 0x1a8(%rsp), %rdi
callq 0xb070
movb %al, 0x9f(%rsp)
jmp 0x7c080
movb 0x9f(%rsp), %al
testb $0x1, %al
jne 0x7c090
jmp 0x7c324
movq 0x1a0(%rsp), %rsi
leaq 0xd05a1(%rip), %rdi     # 0x14c640
callq 0x7d0d0
movq %rax, 0x90(%rsp)
jmp 0x7c0ae
movq 0x90(%rsp), %rdi
addq $0x8, %rdi
callq 0x186d0
movl %eax, 0x18c(%rsp)
movl $0x0, 0x188(%rsp)
movl 0x188(%rsp), %eax
movl %eax, 0x84(%rsp)
movl $0x4, 0x184(%rsp)
leaq 0x18c(%rsp), %rdi
leaq 0x184(%rsp), %rsi
callq 0x7d100
movq %rax, 0x88(%rsp)
jmp 0x7c109
movl 0x84(%rsp), %eax
movq 0x88(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x7c1a8
movl $0x40, %edi
callq 0x8340
movq %rax, 0x78(%rsp)
jmp 0x7c131
movq 0x78(%rsp), %rdi
movl 0x188(%rsp), %esi
callq 0x77bb0
jmp 0x7c144
movq 0x78(%rsp), %rax
movq %rax, 0x178(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x7d230
jmp 0x7c168
jmp 0x7c16a
movl 0x188(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x188(%rsp)
jmp 0x7c0d1
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
movl $0x0, 0x174(%rsp)
cmpl $0x4, 0x174(%rsp)
jge 0x7c246
movl $0x40, %edi
callq 0x8340
movq %rax, 0x70(%rsp)
jmp 0x7c1d2
movq 0x70(%rsp), %rdi
movl 0x174(%rsp), %esi
callq 0x77bb0
jmp 0x7c1e5
movq 0x70(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x7d230
jmp 0x7c209
jmp 0x7c20b
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0x7c1b3
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
movl $0x40, %edi
callq 0x8340
movq %rax, 0x68(%rsp)
jmp 0x7c257
movq 0x68(%rsp), %rdi
movl $0xc, %esi
callq 0x77be0
jmp 0x7c268
movq 0x68(%rsp), %rax
movq %rax, 0x160(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x7d230
jmp 0x7c28c
movl $0x40, %edi
callq 0x8340
movq %rax, 0x60(%rsp)
jmp 0x7c29d
movq 0x60(%rsp), %rdi
movl $0xe, %esi
callq 0x77be0
jmp 0x7c2ae
movq 0x60(%rsp), %rax
movq %rax, 0x158(%rsp)
leaq 0x1f0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x7d230
jmp 0x7c2d2
jmp 0x7c324
movq 0x68(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
jmp 0x7c326
jmp 0x7c72c
movq 0x208(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpq $0x0, %rax
je 0x7c361
movq 0x58(%rsp), %rdi
leaq 0xce23e(%rip), %rsi     # 0x14a588
leaq 0xce1ef(%rip), %rdx     # 0x14a540
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x50(%rsp)
jmp 0x7c36a
xorl %eax, %eax
movq %rax, 0x50(%rsp)
jmp 0x7c36a
movq 0x50(%rsp), %rax
movq %rax, 0x150(%rsp)
cmpq $0x0, 0x150(%rsp)
je 0x7c409
movq 0x150(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x7c3b1
movq 0x150(%rsp), %rsi
addq $0x20, %rsi
leaq 0x1f0(%rsp), %rdi
callq 0x7d1c0
jmp 0x7c3af
jmp 0x7c3ce
movq 0x150(%rsp), %rsi
addq $0x20, %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x7d1c0
jmp 0x7c3cc
jmp 0x7c3ce
movq 0x150(%rsp), %rsi
addq $0x28, %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x7d1c0
jmp 0x7c3e9
movq 0x150(%rsp), %rax
movq 0x38(%rax), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x7c7a0
jmp 0x7c404
jmp 0x7c72a
movq 0x208(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x7c43f
movq 0x48(%rsp), %rdi
leaq 0xce160(%rip), %rsi     # 0x14a588
leaq 0xce129(%rip), %rdx     # 0x14a558
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x40(%rsp)
jmp 0x7c448
xorl %eax, %eax
movq %rax, 0x40(%rsp)
jmp 0x7c448
movq 0x40(%rsp), %rax
movq %rax, 0x148(%rsp)
cmpq $0x0, 0x148(%rsp)
je 0x7c480
movq 0x148(%rsp), %rsi
addq $0x18, %rsi
leaq 0x1f0(%rsp), %rdi
callq 0x7d1c0
jmp 0x7c47b
jmp 0x7c728
movq 0x208(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x7c4b6
movq 0x38(%rsp), %rdi
leaq 0xce0e9(%rip), %rsi     # 0x14a588
leaq 0xce0ca(%rip), %rdx     # 0x14a570
xorl %eax, %eax
movl %eax, %ecx
callq 0x8410
movq %rax, 0x30(%rsp)
jmp 0x7c4bf
xorl %eax, %eax
movq %rax, 0x30(%rsp)
jmp 0x7c4bf
movq 0x30(%rsp), %rax
movq %rax, 0x140(%rsp)
cmpq $0x0, 0x140(%rsp)
je 0x7c6a9
movq 0x140(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x7c58b
movq 0x140(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x138(%rsp)
movq 0x138(%rsp), %rdi
callq 0xcdb0
movq %rax, 0x130(%rsp)
movq 0x138(%rsp), %rdi
callq 0xce60
movq %rax, 0x128(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xce20
testb $0x1, %al
jne 0x7c546
jmp 0x7c586
leaq 0x130(%rsp), %rdi
callq 0xce90
movq (%rax), %rax
movq %rax, 0x120(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x7d1c0
jmp 0x7c575
jmp 0x7c577
leaq 0x130(%rsp), %rdi
callq 0xcea0
jmp 0x7c52b
jmp 0x7c6a7
movq 0x140(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
callq 0xcdb0
movq %rax, 0x110(%rsp)
movq 0x118(%rsp), %rdi
callq 0xce60
movq %rax, 0x108(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xce20
testb $0x1, %al
jne 0x7c5e7
jmp 0x7c6a5
leaq 0x110(%rsp), %rdi
callq 0xce90
movq (%rax), %rax
movq %rax, 0x100(%rsp)
movq 0x100(%rsp), %rax
cmpl $0xf, 0x38(%rax)
jne 0x7c67a
movl $0x40, %edi
callq 0x8340
movq %rax, 0x28(%rsp)
jmp 0x7c61e
movq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x77be0
jmp 0x7c62c
movq 0x28(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0x1d8(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x7d230
jmp 0x7c650
jmp 0x7c67a
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x7c779
leaq 0x1f0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x7d1c0
jmp 0x7c691
jmp 0x7c693
leaq 0x110(%rsp), %rdi
callq 0xcea0
jmp 0x7c5c9
jmp 0x7c6a7
jmp 0x7c726
movq 0xcf938(%rip), %rdi     # 0x14bfe8
leaq 0x7da41(%rip), %rsi     # 0xfa0f8
callq 0x8330
movq %rax, 0x20(%rsp)
jmp 0x7c6c3
movq 0x20(%rsp), %rdi
leaq 0x8104e(%rip), %rsi     # 0xfd71d
callq 0x8330
movq %rax, 0x18(%rsp)
jmp 0x7c6db
movq 0x18(%rsp), %rdi
leaq 0x7ed03(%rip), %rsi     # 0xfb3ea
callq 0x8330
movq %rax, 0x10(%rsp)
jmp 0x7c6f3
movq 0x10(%rsp), %rdi
movl $0x94, %esi
callq 0x8650
movq %rax, 0x8(%rsp)
jmp 0x7c709
movq 0x8(%rsp), %rdi
movq 0xcf86b(%rip), %rsi     # 0x14bf80
callq 0x8380
jmp 0x7c71c
movl $0xff, %edi
callq 0x8460
jmp 0x7c728
jmp 0x7c72a
jmp 0x7c72c
jmp 0x7c72e
jmp 0x7c730
movq 0xe0(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
callq 0x7d260
jmp 0x7c74f
leaq 0x1d8(%rsp), %rdi
callq 0xcce0
leaq 0x1f0(%rsp), %rdi
callq 0xcce0
movq 0xe8(%rsp), %rax
addq $0x218, %rsp            # imm = 0x218
retq
leaq 0x1d8(%rsp), %rdi
callq 0xcce0
leaq 0x1f0(%rsp), %rdi
callq 0xcce0
movq 0x1c0(%rsp), %rdi
callq 0x8670