// File: /workspace/github_cpp_makefile_star2_O1/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 0x4dfc1
movl %r8d, %r15d
leal -0x1(%r15), %eax
testl %eax, %r8d
jne 0x4dfb0
movq %rcx, %r13
movq %rsi, %r14
movq %rdi, %r12
movl %eax, 0xc(%rsp)
movq %rdx, 0x20(%rsp)
movl $0x40, %edi
callq 0x8290
movq %rax, %rbx
movl 0x50(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x50(%r12)
leaq 0x30(%rbx), %rcx
movq %rcx, (%rbx)
movq $0x1, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movl $0x3f800000, 0x20(%rbx) # imm = 0x3F800000
movups %xmm0, 0x28(%rbx)
movl %eax, 0x38(%rbx)
movq %r12, %rdi
movq %r14, (%rsp)
movq %r14, %rsi
movq %r13, %rdx
callq 0x4d592
movq %rax, %r14
movl $0x28, %edi
callq 0x8290
movq %rax, %r13
movl $0x18, %edi
callq 0x8290
movq %rax, %rbp
movl $0x18, %edi
callq 0x8290
xorl %ecx, %ecx
movl %ecx, (%rax)
movb $0x1, 0x4(%rax)
movl $0x1f, 0x8(%rax)
movq $0x0, 0x10(%rax)
movb $0x0, (%rbp)
movl %ecx, 0x4(%rbp)
movq %r14, 0x8(%rbp)
movq %rax, 0x10(%rbp)
movq %r13, %rdi
movq (%rsp), %rsi
movl $0x1, %edx
callq 0xa6a8
leaq 0x406ec(%rip), %rax     # 0x8e4c0
leaq 0x10(%rax), %rcx
movq %rcx, (%r13)
movw $0x0, 0x14(%r13)
movq %rbx, 0x18(%r13)
movq %rbp, 0x20(%r13)
movq %r13, %rdi
callq *0x28(%rax)
movl $0x40, %edi
callq 0x8290
movq %rax, %r13
movl 0x50(%r12), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x50(%r12)
leaq 0x30(%r13), %rcx
movq %rcx, (%r13)
movq $0x1, 0x8(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13)
movl $0x3f800000, 0x20(%r13) # imm = 0x3F800000
movups %xmm0, 0x28(%r13)
movl %eax, 0x38(%r13)
movl $0x20, %ebp
cmpl $0x2, %r15d
jl 0x4de4f
movl %r15d, %eax
shrl %eax
decl %ebp
cmpl $0x3, %r15d
movl %eax, %r15d
ja 0x4de42
movq %r14, 0x18(%rsp)
movl $0x18, %edi
callq 0x8290
movq %rax, %r15
movl $0x18, %edi
callq 0x8290
movl $0x2, (%rax)
movb $0x1, 0x4(%rax)
movl %ebp, 0x8(%rax)
movq $0x0, 0x10(%rax)
movb $0x0, (%r15)
movl $0x0, 0x4(%r15)
movq %rbx, 0x8(%r15)
movq %rax, 0x10(%r15)
movl $0x38, %edi
callq 0x8290
movq %rax, %rbp
movq %rax, %rdi
movq (%rsp), %rsi
movl $0x1, %edx
callq 0xa6a8
leaq 0x405a7(%rip), %rax     # 0x8e460
leaq 0x10(%rax), %r14
movq %r14, (%rbp)
movl $0x0, 0x14(%rbp)
movb $0x0, 0x18(%rbp)
movq %r13, 0x20(%rbp)
movq 0x18(%rsp), %rcx
movq %rcx, 0x28(%rbp)
movq %r15, 0x30(%rbp)
movq 0x28(%rax), %rax
movq %rbp, %rdi
movq %rax, 0x10(%rsp)
callq *%rax
movl $0x38, %edi
callq 0x8290
movq %rax, %r15
movq %r12, %rdi
movq (%rsp), %rbp
movq %rbp, %rsi
movl 0xc(%rsp), %edx
xorl %ecx, %ecx
callq 0x4cd8a
movq %rax, %rbx
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xa6a8
movq %r14, (%r15)
movl $0x9, 0x14(%r15)
movb $0x0, 0x18(%r15)
movq %r13, 0x20(%r15)
movq %r13, 0x28(%r15)
movq %rbx, 0x30(%r15)
movq %r15, %rdi
callq *0x10(%rsp)
movl $0x38, %edi
callq 0x8290
movq %rax, %r15
movl $0x18, %edi
callq 0x8290
movq 0x20(%rsp), %r12
movq %rax, %rbx
movb $0x0, (%rax)
movl $0x0, 0x4(%rax)
movq %r13, 0x8(%rax)
movq $0x0, 0x10(%rax)
movq %r15, %rdi
movq %rbp, %rsi
movl $0x1, %edx
callq 0xa6a8
movq %r14, (%r15)
movl $0x1, 0x14(%r15)
movb $0x0, 0x18(%r15)
movq %r12, 0x20(%r15)
movq 0x18(%rsp), %rax
movq %rax, 0x28(%r15)
movq %rbx, 0x30(%r15)
movq %r15, %rdi
callq *0x10(%rsp)
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x42028(%rip), %rdi     # 0x8fff0
leaq 0x21199(%rip), %rsi     # 0x6f168
callq 0x8280
leaq 0x248b5(%rip), %rsi     # 0x72890
movq %rax, %rdi
callq 0x8280
leaq 0x223d6(%rip), %rsi     # 0x703c0
movq %rax, %rdi
callq 0x8280
movq %rax, %rdi
movl $0xf6, %esi
callq 0x8520
movq %rax, %rdi
callq 0x8180
movl $0xff, %edi
callq 0x8380
movq %rax, %rbx
movl $0x18, %esi
jmp 0x4e044
movq %rax, %rbx
movl $0x18, %esi
movq %rbp, %rdi
callq 0x82a0
jmp 0x4e04c
movq %rax, %rbx
movl $0x38, %esi
movq %rbp, %rdi
jmp 0x4e054
jmp 0x4e03c
movq %rax, %rbx
movl $0x38, %esi
movq %r15, %rdi
jmp 0x4e054
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540
nop