// File: luooofan[P]hblzbd_compiler/src/Pass/reach_define.cpp
// Function: ReachDefine::Run4Func(IRFunction*)
void ReachDefine::Run4Func(IRFunction *f) {
  for (auto bb : f->bb_list_) {
    bb->gen_.clear();
    bb->kill_.clear();
    bb->reach_in_.clear();
    bb->reach_out_.clear();

    // 反向遍历bb的ir_list，对于得到的gen和kill
    // 如果kill中的元素不在bb的gen中，说明它未被后面的生成语句取代，所以才可以加入到bb的kill中
    // 而对于gen中的元素，如果被后面的kill杀死，则就不能加进去
    for (auto iter = bb->ir_list_.rbegin(); iter != bb->ir_list_.rend(); ++iter) {
      auto ir = *iter;

      auto [gen, kill] = GetGenKill(ir, f);

      for (auto k : kill) {
        if (bb->gen_.find(k) == bb->gen_.end()) {
          bb->kill_.insert(k);
        }
      }

      for (auto g : gen) {
        if (bb->kill_.find(g) == bb->kill_.end()) {
          bb->gen_.insert(g);
        }
      }

      bb->reach_out_ = bb->gen_;
    }
  }

  bool changed = true;
  while (changed) {
    changed = false;
    for (auto bb : f->bb_list_) {
      std::unordered_set<ir::IR *> new_in;

      for (auto pred : bb->pred_) {
        new_in.insert(pred->reach_out_.begin(), pred->reach_out_.end());
      }

      if (new_in != bb->reach_in_) {
        changed = true;
        bb->reach_in_ = new_in;

        for (auto s : bb->reach_in_) {
          if (bb->kill_.find(s) == bb->kill_.end()) {
            bb->reach_out_.insert(s);
          }
        }
        for (auto g : bb->gen_) {
          bb->reach_out_.insert(g);
        }
      }
    }
  }
}

// Assembly:
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rdi, 0x2b0(%rsp)
movq %rsi, 0x2a8(%rsp)
movq 0x2b0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x2a8(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x2a0(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x298(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x290(%rsp)
leaq 0x298(%rsp), %rdi
leaq 0x290(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xf0030
jmp 0xf041b
leaq 0x298(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x288(%rsp)
movq 0x288(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
callq 0xf2890
movq 0x288(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
callq 0xf2890
movq 0x288(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
callq 0xf2890
movq 0x288(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
callq 0xf2890
movq 0x288(%rsp), %rsi
addq $0x8, %rsi
leaq 0x280(%rsp), %rdi
callq 0xd4f50
movq 0x288(%rsp), %rsi
addq $0x8, %rsi
leaq 0x278(%rsp), %rdi
callq 0xf28d0
leaq 0x280(%rsp), %rdi
leaq 0x278(%rsp), %rsi
callq 0xf28a0
testb $0x1, %al
jne 0xf00e8
jmp 0xf0407
leaq 0x280(%rsp), %rdi
callq 0xd4fa0
movq 0x58(%rsp), %rsi
movq (%rax), %rax
movq %rax, 0x270(%rsp)
movq 0x270(%rsp), %rdx
movq 0x2a8(%rsp), %rcx
leaq 0x200(%rsp), %rdi
callq 0xf15a0
leaq 0x200(%rsp), %rdi
callq 0xf2920
movq %rax, 0x1f8(%rsp)
leaq 0x200(%rsp), %rdi
callq 0xf2930
movq %rax, 0x1f0(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x1e8(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x845a0
movq %rax, 0x1e0(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0xf2490
movq %rax, 0x1d8(%rsp)
leaq 0x1e0(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0xf24c0
testb $0x1, %al
jne 0xf01a4
jmp 0xf029e
leaq 0x1e0(%rsp), %rdi
callq 0x845d0
movq (%rax), %rax
movq %rax, 0x1d0(%rsp)
movq 0x288(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
leaq 0x1d0(%rsp), %rsi
callq 0xf2960
movq %rax, 0x50(%rsp)
jmp 0xf01df
movq 0x50(%rsp), %rax
movq %rax, 0x1c8(%rsp)
movq 0x288(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
callq 0xf2490
movq %rax, 0x1b0(%rsp)
leaq 0x1c8(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0xf2940
testb $0x1, %al
jne 0xf0223
jmp 0xf028a
movq 0x288(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
leaq 0x1d0(%rsp), %rsi
callq 0xf2710
movb %dl, 0x47(%rsp)
movq %rax, 0x48(%rsp)
jmp 0xf024a
movb 0x47(%rsp), %al
movq 0x48(%rsp), %rcx
movq %rcx, 0x1a0(%rsp)
movb %al, 0x1a8(%rsp)
jmp 0xf028a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x200(%rsp), %rdi
callq 0xf29c0
jmp 0xf0821
jmp 0xf028c
leaq 0x1e0(%rsp), %rdi
callq 0xf24e0
jmp 0xf0186
movq 0x1f8(%rsp), %rax
movq %rax, 0x198(%rsp)
movq 0x198(%rsp), %rdi
callq 0x845a0
movq %rax, 0x190(%rsp)
movq 0x198(%rsp), %rdi
callq 0xf2490
movq %rax, 0x188(%rsp)
leaq 0x190(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0xf24c0
testb $0x1, %al
jne 0xf02f6
jmp 0xf03c8
leaq 0x190(%rsp), %rdi
callq 0x845d0
movq (%rax), %rax
movq %rax, 0x180(%rsp)
movq 0x288(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
leaq 0x180(%rsp), %rsi
callq 0xf2960
movq %rax, 0x38(%rsp)
jmp 0xf0331
movq 0x38(%rsp), %rax
movq %rax, 0x178(%rsp)
movq 0x288(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
callq 0xf2490
movq %rax, 0x170(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0xf2940
testb $0x1, %al
jne 0xf0375
jmp 0xf03b4
movq 0x288(%rsp), %rdi
addq $0x138, %rdi            # imm = 0x138
leaq 0x180(%rsp), %rsi
callq 0xf2710
movb %dl, 0x2f(%rsp)
movq %rax, 0x30(%rsp)
jmp 0xf039c
movb 0x2f(%rsp), %al
movq 0x30(%rsp), %rcx
movq %rcx, 0x160(%rsp)
movb %al, 0x168(%rsp)
jmp 0xf03b6
leaq 0x190(%rsp), %rdi
callq 0xf24e0
jmp 0xf02d8
movq 0x288(%rsp), %rdi
movq %rdi, %rsi
addq $0x138, %rsi            # imm = 0x138
addq $0x1e0, %rdi            # imm = 0x1E0
callq 0xf2990
jmp 0xf03e8
leaq 0x200(%rsp), %rdi
callq 0xf29c0
leaq 0x280(%rsp), %rdi
callq 0xf29f0
jmp 0xf00b1
jmp 0xf0409
leaq 0x298(%rsp), %rdi
callq 0x49330
jmp 0xf0012
movb $0x1, 0x15f(%rsp)
testb $0x1, 0x15f(%rsp)
je 0xf0819
movb $0x0, 0x15f(%rsp)
movq 0x2a8(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x150(%rsp)
movq 0x150(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x148(%rsp)
movq 0x150(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xf0495
jmp 0xf0814
leaq 0x148(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x138(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x18d90
movq 0x138(%rsp), %rax
addq $0x28, %rax
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1cc40
movq %rax, 0xf0(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1cac0
movq %rax, 0xe8(%rsp)
leaq 0xf0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xf0516
jmp 0xf05bf
leaq 0xf0(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
callq 0x845a0
movq %rax, 0xd8(%rsp)
movq 0xe0(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
callq 0xf2490
movq %rax, 0xd0(%rsp)
movq 0xd8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
leaq 0x100(%rsp), %rdi
callq 0xf2a20
jmp 0xf0585
jmp 0xf0587
leaq 0xf0(%rsp), %rdi
callq 0x49330
jmp 0xf04f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x494f0
jmp 0xf0821
movq 0x138(%rsp), %rsi
addq $0x1a8, %rsi            # imm = 0x1A8
leaq 0x100(%rsp), %rdi
callq 0xf2a60
movb %al, 0x2e(%rsp)
jmp 0xf05e1
movb 0x2e(%rsp), %al
testb $0x1, %al
jne 0xf05ee
jmp 0xf07f5
movb $0x1, 0x15f(%rsp)
movq 0x138(%rsp), %rdi
addq $0x1a8, %rdi            # imm = 0x1A8
leaq 0x100(%rsp), %rsi
callq 0xf2990
jmp 0xf0614
movq 0x138(%rsp), %rax
addq $0x1a8, %rax            # imm = 0x1A8
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x845a0
movq %rax, 0xc0(%rsp)
movq 0xc8(%rsp), %rdi
callq 0xf2490
movq %rax, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xf24c0
testb $0x1, %al
jne 0xf0672
jmp 0xf0744
leaq 0xc0(%rsp), %rdi
callq 0x845d0
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0x138(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
leaq 0xb0(%rsp), %rsi
callq 0xf2960
movq %rax, 0x20(%rsp)
jmp 0xf06ad
movq 0x20(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x138(%rsp), %rdi
addq $0x170, %rdi            # imm = 0x170
callq 0xf2490
movq %rax, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0xf2940
testb $0x1, %al
jne 0xf06f1
jmp 0xf0730
movq 0x138(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
leaq 0xb0(%rsp), %rsi
callq 0xf2710
movb %dl, 0x17(%rsp)
movq %rax, 0x18(%rsp)
jmp 0xf0718
movb 0x17(%rsp), %al
movq 0x18(%rsp), %rcx
movq %rcx, 0x90(%rsp)
movb %al, 0x98(%rsp)
jmp 0xf0732
leaq 0xc0(%rsp), %rdi
callq 0xf24e0
jmp 0xf0654
movq 0x138(%rsp), %rax
addq $0x138, %rax            # imm = 0x138
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rdi
callq 0x845a0
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rdi
callq 0xf2490
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xf24c0
testb $0x1, %al
jne 0xf0799
jmp 0xf07f3
leaq 0x80(%rsp), %rdi
callq 0x845d0
movq (%rax), %rax
movq %rax, 0x70(%rsp)
movq 0x138(%rsp), %rdi
addq $0x1e0, %rdi            # imm = 0x1E0
leaq 0x70(%rsp), %rsi
callq 0xf2710
movb %dl, 0x7(%rsp)
movq %rax, 0x8(%rsp)
jmp 0xf07d2
movb 0x7(%rsp), %al
movq 0x8(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movb %al, 0x68(%rsp)
leaq 0x80(%rsp), %rdi
callq 0xf24e0
jmp 0xf0781
jmp 0xf07f5
leaq 0x100(%rsp), %rdi
callq 0x494f0
leaq 0x148(%rsp), %rdi
callq 0x49330
jmp 0xf0477
jmp 0xf0423
addq $0x2b8, %rsp            # imm = 0x2B8
retq
movq 0x1c0(%rsp), %rdi
callq 0x8670
nop