// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/Pass/constant_propagation.cpp
// Function: ConstantPropagation::Run()
void ConstantPropagation::Run() {
  auto m = dynamic_cast<IRModule *>(*m_);

  MyAssert(nullptr != m);

  bool changed = true;

  while (changed) {
    ReachDefine reachdefine = ReachDefine(m_);
    reachdefine.Run();
    changed = false;
    for (auto func : m->func_list_) {
      for (auto bb : func->bb_list_) {
        for (int i = 0; i < bb->ir_list_.size(); ++i) {
          auto ir = bb->ir_list_[i];
          auto map_this_ir = bb->use_def_chains_[i];

          auto process_opn = [&bb, &ir, &map_this_ir, &changed](ir::Opn *opn, int opn_num) {
            if (opn->type_ == ir::Opn::Type::Var) {
              auto opn_set = map_this_ir[opn];
              if (opn_set.size() == 1) {
                auto ir_opn = *opn_set.begin();
                if (ir_opn->op_ == ir::IR::OpKind::ASSIGN && ir_opn->opn1_.type_ == ir::Opn::Type::Imm) {
#ifdef PRINT_DEBUG
                  std::cout << "将变量: " << opn->name_ + "_#" + std::to_string(opn->scope_id_) << " 变为常数"
                            << ir_opn->opn1_.imm_num_ << std::endl;
#endif
                  changed = true;
                  if (opn_num == 1) {
                    ir->opn1_ = ir_opn->opn1_;
                  } else if (opn_num == 2) {
                    ir->opn2_ = ir_opn->opn1_;
                  }
                }
              }
            }
          };

          process_opn(&(ir->opn1_), 1);

          process_opn(&(ir->opn2_), 2);

          if (ir::IR::OpKind::ADD == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm && ir->opn2_.type_ == ir::Opn::Type::Imm) {
              int res = ir->opn1_.imm_num_ + ir->opn2_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
              // if(changed) printf("/*******\n");
            }
          } else if (ir::IR::OpKind::SUB == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm && ir->opn2_.type_ == ir::Opn::Type::Imm) {
              int res = ir->opn1_.imm_num_ - ir->opn2_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          } else if (ir::IR::OpKind::MUL == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm && ir->opn2_.type_ == ir::Opn::Type::Imm) {
              int res = ir->opn1_.imm_num_ * ir->opn2_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          } else if (ir::IR::OpKind::DIV == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm && ir->opn2_.type_ == ir::Opn::Type::Imm) {
              // 没有加对除0的限制，应该没有必要
              int res = ir->opn1_.imm_num_ / ir->opn2_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          } else if (ir::IR::OpKind::MOD == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm && ir->opn2_.type_ == ir::Opn::Type::Imm) {
              int res = ir->opn1_.imm_num_ % ir->opn2_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          } else if (ir::IR::OpKind::NOT == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm) {
              int res = !ir->opn1_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          } else if (ir::IR::OpKind::NEG == ir->op_) {
            if (ir->opn1_.type_ == ir::Opn::Type::Imm) {
              int res = -ir->opn1_.imm_num_;
              ir::Opn opn1 = ir::Opn(ir::Opn::Type::Imm, res);
              ir::Opn res_opn = ir->res_;
              bb->ir_list_[i] = new ir::IR(ir::IR::OpKind::ASSIGN, opn1, res_opn);
              changed = true;
            }
          }
        }
      }
    }
    // if(changed) printf("/*******\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rdi, 0xb0(%rsp)
movq 0x8(%rdi), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x30f15
leaq 0x4ee30(%rip), %rsi     # 0x7f648
leaq 0x4f851(%rip), %rdx     # 0x80070
xorl %ecx, %ecx
callq 0x82c0
movq %rax, 0xa0(%rsp)
testq %rax, %rax
je 0x30f15
leaq 0xf(%rsp), %rax
movb $0x1, (%rax)
leaq 0x5045a(%rip), %rax     # 0x80ca0
addq $0x10, %rax
movq %rax, 0xa8(%rsp)
movb $0x1, %al
leaq 0x4b0(%rsp), %r12
leaq 0x110(%rsp), %r13
testb $0x1, %al
je 0x30f03
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xe0(%rsp)
leaq 0xf8(%rsp), %rax
movq %rax, 0xe8(%rsp)
andq $0x0, 0xf0(%rsp)
xorl %eax, %eax
movb %al, 0xf8(%rsp)
movb %al, 0x108(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x51fb2
movb $0x0, 0xf(%rsp)
movq 0xa0(%rsp), %rax
movq 0x28(%rax), %rcx
movq 0x30(%rax), %rax
movq %rax, 0xb8(%rsp)
movq %rcx, %rax
cmpq 0xb8(%rsp), %rax
je 0x30eed
movq %rax, 0xc0(%rsp)
movq (%rax), %rax
movq 0x30(%rax), %rcx
movq 0x38(%rax), %rax
movq %rax, 0xc8(%rsp)
cmpq 0xc8(%rsp), %rcx
je 0x30edc
movq %rcx, 0xd0(%rsp)
movq (%rcx), %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x8(%rax), %rcx
movq 0x10(%rax), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x30ecb
movq (%rcx,%r15,8), %rcx
movq %rcx, 0x10(%rsp)
movq 0x218(%rax), %rsi
addq %rbp, %rsi
movq %r12, %rdi
callq 0x311c2
leaq 0x18(%rsp), %rax
movq %rax, 0x110(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x118(%rsp)
movq %r12, 0x120(%rsp)
leaq 0xf(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x10(%rsp), %rsi
addq $0x8, %rsi
movq %r13, %rdi
pushq $0x1
popq %rdx
callq 0x3110e
movq 0x10(%rsp), %rsi
addq $0x48, %rsi
movq %r13, %rdi
pushq $0x2
popq %rdx
callq 0x3110e
movq 0x10(%rsp), %rcx
movl (%rcx), %eax
cmpq $0x6, %rax
ja 0x30eb2
leaq 0x28c24(%rip), %rdx     # 0x595f8
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
cmpl $0x1, 0x48(%rcx)
jne 0x30eb2
movl 0x4c(%rcx), %edx
addl 0xc(%rcx), %edx
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x2b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x470(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x2b0(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x478(%rsp), %rdi
callq 0x8578
leaq 0x2b8(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
cmpl $0x1, 0x48(%rcx)
jne 0x30eb2
movl 0xc(%rcx), %eax
cltd
idivl 0x4c(%rcx)
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x370(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x1b0(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x378(%rsp), %rdi
callq 0x8578
leaq 0x1b8(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
cmpl $0x1, 0x48(%rcx)
jne 0x30eb2
movl 0x4c(%rcx), %edx
imull 0xc(%rcx), %edx
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x3f0(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x230(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x3f8(%rsp), %rdi
callq 0x8578
leaq 0x238(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
cmpl $0x1, 0x48(%rcx)
jne 0x30eb2
movl 0xc(%rcx), %eax
cltd
idivl 0x4c(%rcx)
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
movl %eax, %edx
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x3b0(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x1f0(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x3b8(%rsp), %rdi
callq 0x8578
leaq 0x1f8(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
cmpl $0x1, 0x48(%rcx)
jne 0x30eb2
movl 0xc(%rcx), %edx
subl 0x4c(%rcx), %edx
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x270(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x430(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x270(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x438(%rsp), %rdi
callq 0x8578
leaq 0x278(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
xorl %edx, %edx
cmpl $0x0, 0xc(%rcx)
sete %dl
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x330(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x170(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x338(%rsp), %rdi
callq 0x8578
leaq 0x178(%rsp), %rdi
jmp 0x30e94
cmpl $0x1, 0x8(%rcx)
jne 0x30eb2
xorl %edx, %edx
subl 0xc(%rcx), %edx
leaq 0x60(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movq 0x10(%rsp), %rsi
movl $0x88, %eax
addq %rax, %rsi
leaq 0x20(%rsp), %rdi
callq 0x1da22
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1da22
leaq 0x2f0(%rsp), %r14
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1da22
movq %rbx, %rdi
pushq $0xc
popq %rsi
leaq 0x130(%rsp), %rdx
movq %r14, %rcx
callq 0x1aa52
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq %rbx, (%rax,%r15,8)
leaq 0x2f8(%rsp), %rdi
callq 0x8578
leaq 0x138(%rsp), %rdi
callq 0x8578
movb $0x1, 0xf(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x8578
leaq 0x68(%rsp), %rdi
callq 0x8578
movq %r12, %rdi
callq 0x20022
incq %r15
movq 0x18(%rsp), %rax
addq $0x38, %rbp
jmp 0x30932
movq 0xd0(%rsp), %rcx
addq $0x8, %rcx
jmp 0x3090f
movq 0xc0(%rsp), %rax
addq $0x8, %rax
jmp 0x308e6
leaq 0xd8(%rsp), %rdi
callq 0x311ae
movb 0xf(%rsp), %al
jmp 0x30864
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x500cc(%rip), %rdi     # 0x80fe8
leaq 0x251c5(%rip), %rsi     # 0x560e8
callq 0x8240
leaq 0x286fb(%rip), %rsi     # 0x5962a
movq %rax, %rdi
callq 0x8240
leaq 0x263e2(%rip), %rsi     # 0x57320
movq %rax, %rdi
callq 0x8240
pushq $0xb
popq %rsi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movl $0xff, %edi
callq 0x8300
jmp 0x310e4
jmp 0x310b2
movq %rax, %r14
jmp 0x30f9b
jmp 0x310e9
jmp 0x310c4
jmp 0x310e9
movq %rax, %r14
jmp 0x30fcc
movq %rax, %r14
jmp 0x30ff3
movq %rax, %r14
leaq 0x478(%rsp), %rdi
callq 0x8578
leaq 0x2b8(%rsp), %rdi
jmp 0x310ab
jmp 0x310c4
jmp 0x310d3
jmp 0x310e9
jmp 0x310b2
movq %rax, %r14
leaq 0x3f8(%rsp), %rdi
callq 0x8578
leaq 0x238(%rsp), %rdi
jmp 0x310ab
jmp 0x310d3
jmp 0x310b2
movq %rax, %r14
leaq 0x378(%rsp), %rdi
callq 0x8578
leaq 0x1b8(%rsp), %rdi
jmp 0x310ab
movq %rax, %r14
jmp 0x31038
jmp 0x310c4
jmp 0x310d3
jmp 0x310e9
movq %rax, %r14
jmp 0x3105b
jmp 0x310c4
jmp 0x310e9
jmp 0x310c4
movq %rax, %r14
leaq 0x438(%rsp), %rdi
callq 0x8578
leaq 0x278(%rsp), %rdi
jmp 0x310ab
jmp 0x310b2
jmp 0x310d3
jmp 0x310b2
movq %rax, %r14
leaq 0x3b8(%rsp), %rdi
callq 0x8578
leaq 0x1f8(%rsp), %rdi
jmp 0x310ab
jmp 0x310d3
movq %rax, %r14
leaq 0x338(%rsp), %rdi
callq 0x8578
jmp 0x3107e
jmp 0x310b2
movq %rax, %r14
leaq 0x178(%rsp), %rdi
jmp 0x310ab
jmp 0x310d3
jmp 0x310e9
jmp 0x310c4
movq %rax, %r14
jmp 0x310a3
movq %rax, %r14
leaq 0x2f8(%rsp), %rdi
callq 0x8578
leaq 0x138(%rsp), %rdi
callq 0x8578
jmp 0x310b5
movq %rax, %r14
movl $0xe0, %esi
movq %rbx, %rdi
callq 0x8260
jmp 0x310c7
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x8578
jmp 0x310d6
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x8578
jmp 0x310ec
jmp 0x310e9
movq %rax, %r14
jmp 0x310f9
movq %rax, %r14
leaq 0x4b0(%rsp), %rdi
callq 0x20022
leaq 0xd8(%rsp), %rdi
callq 0x311ae
movq %r14, %rdi
callq 0x84d0