// File: luooofan[P]hblzbd_compiler/src/Pass/reach_define.cpp
// Function: ReachDefine::GetUseDefChain(IRModule*)
void ReachDefine::GetUseDefChain(IRModule *m) {
  for (auto func : m->func_list_) {
    for (auto bb : func->bb_list_) {
      bb->use_def_chains_.clear();
      for (auto ir : bb->ir_list_) {
        std::unordered_map<ir::Opn *, std::unordered_set<ir::IR *>> map_this_ir;
        auto process_opn = [&bb, &map_this_ir](ir::IR *ir, ir::Opn *opn) {
          if (opn->type_ != ir::Opn::Type::Var) return;
          if (opn->scope_id_ == 0) return;
          ir::IR *def_opn = finddef(bb, ir, opn);
          std::unordered_set<ir::IR *> opn_set;

          if (nullptr != def_opn) {
            opn_set.insert(def_opn);
            map_this_ir.emplace(opn, opn_set);
          } else {
            for (auto in : bb->reach_in_) {
              if (isSameOpn(in->res_, *opn)) {
                opn_set.insert(in);
              }
            }
            map_this_ir.emplace(opn, opn_set);
          }
        };
        if (ir::IR::OpKind::ADD == ir->op_ || ir::IR::OpKind::SUB == ir->op_ || ir::IR::OpKind::MUL == ir->op_ ||
            ir::IR::OpKind::DIV == ir->op_ || ir::IR::OpKind::MOD == ir->op_) {
          map_this_ir.clear();
          process_opn(ir, &(ir->opn1_));
          process_opn(ir, &(ir->opn2_));

          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::NOT == ir->op_ || ir::IR::OpKind::NEG == ir->op_) {
          map_this_ir.clear();
          process_opn(ir, &(ir->opn1_));

          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::LABEL == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::PARAM == ir->op_) {
          map_this_ir.clear();
          process_opn(ir, &(ir->opn1_));

          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::CALL == ir->op_) {
          map_this_ir.clear();

          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::RET == ir->op_) {
          map_this_ir.clear();
          // TODO: 关于return语句返回立即数的处理
          if (ir->opn1_.type_ == ir::Opn::Type::Var || ir->opn1_.type_ == ir::Opn::Type::Array) {
            process_opn(ir, &(ir->opn1_));
          }
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::GOTO == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::ASSIGN == ir->op_) {
          map_this_ir.clear();
          process_opn(ir, &(ir->opn1_));
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::JEQ == ir->op_ || ir::IR::OpKind::JNE == ir->op_ || ir::IR::OpKind::JLT == ir->op_ ||
                   ir::IR::OpKind::JLE == ir->op_ || ir::IR::OpKind::JGT == ir->op_ || ir::IR::OpKind::JGE == ir->op_) {
          map_this_ir.clear();
          process_opn(ir, &(ir->opn1_));
          process_opn(ir, &(ir->opn2_));

          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::VOID == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::ASSIGN_OFFSET == ir->op_) {
          map_this_ir.clear();

          process_opn(ir, &(ir->opn1_));
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::PHI == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
          printf("我就不信我这个pass会有PHI\n");
        } else if (ir::IR::OpKind::ALLOCA == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
        } else if (ir::IR::OpKind::DECLARE == ir->op_) {
          map_this_ir.clear();
          bb->use_def_chains_.push_back(map_this_ir);
        } else {
          MyAssert(0);
        }
      }
    }
  }
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq 0x118(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rdi
callq 0x1d3c0
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rdi
callq 0x1d240
movq %rax, 0x100(%rsp)
leaq 0x108(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x49210
testb $0x1, %al
jne 0xf08a3
jmp 0xf1041
leaq 0x108(%rsp), %rdi
callq 0x49250
movq (%rax), %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
addq $0x30, %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x1cc40
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x1cac0
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xf0917
jmp 0xf102d
leaq 0xe8(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf27e0
movq 0xd8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x1b070
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x1aeb0
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x493c0
testb $0x1, %al
jne 0xf099f
jmp 0xf1019
leaq 0xc8(%rsp), %rdi
callq 0x1b4d0
movq (%rax), %rax
movq %rax, 0xb8(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xf2800
leaq 0xd8(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xb8(%rsp), %rcx
xorl %eax, %eax
cmpl (%rcx), %eax
je 0xf0a34
movq 0xb8(%rsp), %rcx
movl $0x1, %eax
cmpl (%rcx), %eax
je 0xf0a34
movq 0xb8(%rsp), %rcx
movl $0x2, %eax
cmpl (%rcx), %eax
je 0xf0a34
movq 0xb8(%rsp), %rcx
movl $0x3, %eax
cmpl (%rcx), %eax
je 0xf0a34
movq 0xb8(%rsp), %rcx
movl $0x4, %eax
cmpl (%rcx), %eax
jne 0xf0aba
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0a5c
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x48, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0a77
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0a95
jmp 0xf0ffa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x4a730
jmp 0xf1049
movq 0xb8(%rsp), %rcx
movl $0x5, %eax
cmpl (%rcx), %eax
je 0xf0adc
movq 0xb8(%rsp), %rcx
movl $0x6, %eax
cmpl (%rcx), %eax
jne 0xf0b27
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0b04
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0b22
jmp 0xf0ff8
movq 0xb8(%rsp), %rcx
movl $0x7, %eax
cmpl (%rcx), %eax
jne 0xf0b6a
leaq 0x80(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0xf2810
movq 0x58(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0b65
jmp 0xf0ff6
movq 0xb8(%rsp), %rcx
movl $0x8, %eax
cmpl (%rcx), %eax
jne 0xf0bc6
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0ba3
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0bc1
jmp 0xf0ff4
movq 0xb8(%rsp), %rcx
movl $0x9, %eax
cmpl (%rcx), %eax
jne 0xf0c09
leaq 0x80(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0xf2810
movq 0x50(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0c04
jmp 0xf0ff2
movq 0xb8(%rsp), %rcx
movl $0xa, %eax
cmpl (%rcx), %eax
jne 0xf0c83
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rax
cmpl $0x0, 0x8(%rax)
je 0xf0c43
movq 0xb8(%rsp), %rax
cmpl $0x5, 0x8(%rax)
jne 0xf0c60
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0c5e
jmp 0xf0c60
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0c7e
jmp 0xf0ff0
movq 0xb8(%rsp), %rcx
movl $0xb, %eax
cmpl (%rcx), %eax
jne 0xf0cc6
leaq 0x80(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xf2810
movq 0x48(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0cc1
jmp 0xf0fee
movq 0xb8(%rsp), %rcx
movl $0xc, %eax
cmpl (%rcx), %eax
jne 0xf0d22
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0cff
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0d1d
jmp 0xf0fec
movq 0xb8(%rsp), %rcx
movl $0xd, %eax
cmpl (%rcx), %eax
je 0xf0d88
movq 0xb8(%rsp), %rcx
movl $0xe, %eax
cmpl (%rcx), %eax
je 0xf0d88
movq 0xb8(%rsp), %rcx
movl $0xf, %eax
cmpl (%rcx), %eax
je 0xf0d88
movq 0xb8(%rsp), %rcx
movl $0x10, %eax
cmpl (%rcx), %eax
je 0xf0d88
movq 0xb8(%rsp), %rcx
movl $0x11, %eax
cmpl (%rcx), %eax
je 0xf0d88
movq 0xb8(%rsp), %rcx
movl $0x12, %eax
cmpl (%rcx), %eax
jne 0xf0dee
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0db0
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x48, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0dcb
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0de9
jmp 0xf0fea
movq 0xb8(%rsp), %rcx
movl $0x13, %eax
cmpl (%rcx), %eax
jne 0xf0e31
leaq 0x80(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xf2810
movq 0x40(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0e2c
jmp 0xf0fe8
movq 0xb8(%rsp), %rcx
movl $0x14, %eax
cmpl (%rcx), %eax
jne 0xf0e8d
leaq 0x80(%rsp), %rdi
callq 0xf2810
movq 0xb8(%rsp), %rsi
movq %rsi, %rdx
addq $0x8, %rdx
leaq 0x70(%rsp), %rdi
callq 0xf12b0
jmp 0xf0e6a
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
leaq 0x80(%rsp), %rsi
callq 0xf2820
jmp 0xf0e88
jmp 0xf0fe6
movq 0xb8(%rsp), %rcx
movl $0x15, %eax
cmpl (%rcx), %eax
jne 0xf0ee0
leaq 0x80(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xf2810
movq 0x38(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0ecb
leaq 0xccdc(%rip), %rdi      # 0xfdbae
xorl %eax, %eax
callq 0x8070
jmp 0xf0edb
jmp 0xf0fe4
movq 0xb8(%rsp), %rcx
movl $0x16, %eax
cmpl (%rcx), %eax
jne 0xf0f23
leaq 0x80(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xf2810
movq 0x30(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0f1e
jmp 0xf0fe2
movq 0xb8(%rsp), %rcx
movl $0x17, %eax
cmpl (%rcx), %eax
jne 0xf0f63
leaq 0x80(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xf2810
movq 0x28(%rsp), %rsi
movq 0xd8(%rsp), %rdi
addq $0x218, %rdi            # imm = 0x218
callq 0xf2820
jmp 0xf0f61
jmp 0xf0fe0
movq 0x5b07e(%rip), %rdi     # 0x14bfe8
leaq 0x9187(%rip), %rsi      # 0xfa0f8
callq 0x8330
movq %rax, 0x20(%rsp)
jmp 0xf0f7d
movq 0x20(%rsp), %rdi
leaq 0xcc0b(%rip), %rsi      # 0xfdb94
callq 0x8330
movq %rax, 0x18(%rsp)
jmp 0xf0f95
movq 0x18(%rsp), %rdi
leaq 0xa449(%rip), %rsi      # 0xfb3ea
callq 0x8330
movq %rax, 0x10(%rsp)
jmp 0xf0fad
movq 0x10(%rsp), %rdi
movl $0xda, %esi
callq 0x8650
movq %rax, 0x8(%rsp)
jmp 0xf0fc3
movq 0x8(%rsp), %rdi
movq 0x5afb1(%rip), %rsi     # 0x14bf80
callq 0x8380
jmp 0xf0fd6
movl $0xff, %edi
callq 0x8460
jmp 0xf0fe2
jmp 0xf0fe4
jmp 0xf0fe6
jmp 0xf0fe8
jmp 0xf0fea
jmp 0xf0fec
jmp 0xf0fee
jmp 0xf0ff0
jmp 0xf0ff2
jmp 0xf0ff4
jmp 0xf0ff6
jmp 0xf0ff8
jmp 0xf0ffa
leaq 0x80(%rsp), %rdi
callq 0x4a730
leaq 0xc8(%rsp), %rdi
callq 0x49460
jmp 0xf0981
jmp 0xf101b
leaq 0xe8(%rsp), %rdi
callq 0x49330
jmp 0xf08f9
jmp 0xf102f
leaq 0x108(%rsp), %rdi
callq 0x49260
jmp 0xf0885
addq $0x128, %rsp            # imm = 0x128
retq
movq 0x68(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)