// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/generate_arm_from_ssa.cpp
// Function: GenerateArmFromSSA::ConvertMod2And(ArmBasicBlock*, arm::Reg*, Value*, int)
bool GenerateArmFromSSA::ConvertMod2And(ArmBasicBlock* armbb, Reg* rd, Value* val, int imm) {
  // 如果是0 应该生成报错
  if (0 == imm) {
    MyAssert(0);
  }
  auto eval_n = [](int imm) {
    int n = 0;
    while (imm > 1) {
      imm >>= 1;
      ++n;
    }
    return n;
  };
  // 如果是2的幂次方 生成一条and rd rm n-1的指令
  // FIXME: 这样做并不适用于被除数是负数的情况
  // if (0 == (imm & (imm - 1))) {
  //   std::cout << "Warning Mod" << std::endl;
  //   auto rm = ResolveValue2Reg(armbb, val);
  //   new BinaryInst(BinaryInst::OpCode::AND, rd, rm, ResolveImm2Operand2(armbb, imm - 1), armbb);
  //   return true;
  // }
  if (0 == (imm & (imm - 1))) {
    // 1. asr vreg, rn, #31;  i.e. mov vreg, rn, ASR #31;
    auto vreg = NewVirtualReg();
    auto rn = ResolveValue2Reg(armbb, val);
    new Move(vreg, new Operand2(rn, new Shift(Shift::OpCode::ASR, 31)), armbb);
    // 2. add vreg2, rn, vreg, lsr #(32-n);
    auto vreg2 = NewVirtualReg();
    auto n = eval_n(imm);
    auto op2 = new Operand2(vreg, new Shift(Shift::OpCode::LSR, 32 - n));
    new BinaryInst(BinaryInst::OpCode::ADD, vreg2, rn, op2, armbb);
    // 3. bic vreg2, vreg2, #(2^n-1);  i.e. bfc vreg2, #0, #n;
    new BinaryInst(BinaryInst::OpCode::BIC, vreg2, vreg2, ResolveImm2Operand2(armbb, imm - 1), armbb);
    // 4. sub rd, rn, vreg2
    new BinaryInst(BinaryInst::OpCode::SUB, rd, rn, new Operand2(vreg2), armbb);
  }
  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
testl %r8d, %r8d
je 0x3d8d8
movl %r8d, %r15d
leal -0x1(%r15), %eax
testl %eax, %r8d
jne 0x3d8c7
movq %rcx, %r13
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movl %eax, 0x14(%rsp)
callq 0x3cb82
movq %rax, 0x18(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x3d242
movq %rax, %rbp
pushq $0x28
popq %rdi
callq 0x8250
movq %rax, 0x8(%rsp)
pushq $0x18
popq %rdi
callq 0x8250
movq %rax, %r13
pushq $0x18
popq %rdi
callq 0x8250
andl $0x0, (%rax)
movb $0x1, 0x4(%rax)
movl $0x1f, 0x8(%rax)
andq $0x0, 0x10(%rax)
movb $0x0, (%r13)
andl $0x0, 0x4(%r13)
movq %rbp, 0x8(%r13)
movq %rax, 0x10(%r13)
pushq $0x1
popq %r9
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x3fbc8
movq %rbp, 0x8(%rsp)
movq %r14, 0x20(%rsp)
movq %r12, %rdi
callq 0x3cb82
movq %rax, %r13
pushq $0x20
popq %rbp
cmpl $0x2, %r15d
jl 0x3d7f0
shrl %r15d
decl %ebp
jmp 0x3d7e3
pushq $0x18
popq %r14
movq %r14, %rdi
callq 0x8250
movq %rax, %r15
movq %r14, %rdi
callq 0x8250
movl $0x2, (%rax)
movb $0x1, 0x4(%rax)
movl %ebp, 0x8(%rax)
andq $0x0, 0x10(%rax)
movb $0x0, (%r15)
andl $0x0, 0x4(%r15)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%r15)
movq %rax, 0x10(%r15)
pushq $0x38
popq %rdi
callq 0x8250
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
movq 0x8(%rsp), %rbp
movq %rbp, %rcx
movq %r15, %r8
movq %rbx, %r9
callq 0x3fd3c
pushq $0x38
popq %rdi
callq 0x8250
movq %rax, %r14
movq %r12, %rdi
movq %rbx, %rsi
movl 0x14(%rsp), %edx
xorl %ecx, %ecx
callq 0x3ccee
movq 0x20(%rsp), %r15
pushq $0x9
popq %rsi
movq %r14, %rdi
movq %r13, %rdx
movq %r13, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x3fd3c
pushq $0x38
popq %rdi
callq 0x8250
movq %rax, %r14
pushq $0x18
popq %rdi
callq 0x8250
movb $0x0, (%rax)
andl $0x0, 0x4(%rax)
movq %r13, 0x8(%rax)
andq $0x0, 0x10(%rax)
pushq $0x1
popq %rsi
movq %r14, %rdi
movq %r15, %rdx
movq %rbp, %rcx
movq %rax, %r8
movq %rbx, %r9
callq 0x3fd3c
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x43709(%rip), %rdi     # 0x80fe8
leaq 0x18802(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x1bf02(%rip), %rsi     # 0x597f4
movq %rax, %rdi
callq 0x8240
leaq 0x19a1f(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
movq %rax, %rdi
movl $0xf6, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
jmp 0x3d947
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r15, %rdi
jmp 0x3d95d
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r13, %rdi
callq 0x8260
jmp 0x3d955
jmp 0x3d947
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r14, %rdi
jmp 0x3d95d
movq %rax, %rbx
pushq $0x28
popq %rsi
movq 0x8(%rsp), %rdi
callq 0x8260
movq %rbx, %rdi
callq 0x84d0