// File: luooofan[P]hblzbd_compiler/src/Pass/ir_liveness_analysis.cpp
// Function: IRLivenessAnalysis::Run4Func(IRFunction*)
void IRLivenessAnalysis::Run4Func(IRFunction *f) {
  for (auto bb : f->bb_list_) {
    bb->livein_.clear();
    bb->liveout_.clear();
    bb->def_.clear();
    bb->use_.clear();

    for (auto ir : bb->ir_list_) {
      auto [def, use] = GetDefUse(ir);

      for (auto &u : use) {
        if (bb->def_.find(u) ==
            bb->def_.end()) {  // 如果在use之前没有def，则将其插入use中，说明该变量在bb中首次出现是以use的形式出现
          bb->use_.insert(u);
        }
      }  // for use

      for (auto &d : def) {
        if (bb->use_.find(d) ==
            bb->use_.end()) {  // 如果在def之前没有use，则将其插入def中，说明该变量首次出现是以def形式出现
          bb->def_.insert(d);
        }
      }  // for def
    }    // for ir
    bb->livein_ = bb->use_;
  }  // for bb

  bool changed = true;
  while (changed) {
    changed = false;
    for (auto bb : f->bb_list_) {
      std::unordered_set<std::string> new_out;

      for (auto succ : bb->succ_) {  // 每个bb的liveout是其后继bb的livein的并集
        new_out.insert(succ->livein_.begin(), succ->livein_.end());
      }

      if (new_out != bb->liveout_) {
        changed = true;
        bb->liveout_ = new_out;

        for (auto s : bb->liveout_) {  // IN[B] = use[B]U(out[B] - def[B])
          if (bb->def_.find(s) == bb->def_.end()) {
            bb->livein_.insert(s);
          }
        }
      }
    }  // for bb
  }    // while changed
}

// Assembly:
subq $0x248, %rsp            # imm = 0x248
movq %rdi, 0x240(%rsp)
movq 0x240(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x238(%rsp)
movq 0x238(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x230(%rsp)
movq 0x238(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x228(%rsp)
leaq 0x230(%rsp), %rdi
leaq 0x228(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xc962b
jmp 0xc9a09
leaq 0x230(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x220(%rsp)
movq 0x220(%rsp), %rdi
addq $0xc8, %rdi
callq 0xca080
movq 0x220(%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0xca080
movq 0x220(%rsp), %rdi
addq $0x58, %rdi
callq 0xca080
movq 0x220(%rsp), %rdi
addq $0x90, %rdi
callq 0xca080
movq 0x220(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x218(%rsp)
movq 0x218(%rsp), %rdi
callq 0x1b070
movq %rax, 0x210(%rsp)
movq 0x218(%rsp), %rdi
callq 0x1aeb0
movq %rax, 0x208(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x493c0
testb $0x1, %al
jne 0xc96ec
jmp 0xc99d4
leaq 0x210(%rsp), %rdi
callq 0x1b4d0
movq (%rax), %rax
movq %rax, 0x200(%rsp)
movq 0x200(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
movl $0x1, %edx
callq 0xc83e0
leaq 0x1d0(%rsp), %rdi
callq 0x8f320
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x8f330
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x1b8(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0xa3ec0
movq %rax, 0x1b0(%rsp)
movq 0x1b8(%rsp), %rdi
callq 0xa3ef0
movq %rax, 0x1a8(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xa3e20
testb $0x1, %al
jne 0xc97a0
jmp 0xc9891
leaq 0x1b0(%rsp), %rdi
callq 0xa4c00
movq %rax, 0x1a0(%rsp)
movq 0x220(%rsp), %rdi
addq $0x58, %rdi
movq 0x1a0(%rsp), %rsi
callq 0x8f940
movq %rax, 0x40(%rsp)
jmp 0xc97d5
movq 0x40(%rsp), %rax
movq %rax, 0x198(%rsp)
movq 0x220(%rsp), %rdi
addq $0x58, %rdi
callq 0x8f6e0
movq %rax, 0x180(%rsp)
leaq 0x198(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x8f920
testb $0x1, %al
jne 0xc9816
jmp 0xc987d
movq 0x220(%rsp), %rdi
addq $0x90, %rdi
movq 0x1a0(%rsp), %rsi
callq 0x8fa20
movb %dl, 0x37(%rsp)
movq %rax, 0x38(%rsp)
jmp 0xc983d
movb 0x37(%rsp), %al
movq 0x38(%rsp), %rcx
movq %rcx, 0x170(%rsp)
movb %al, 0x178(%rsp)
jmp 0xc987d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x8f530
jmp 0xc9d72
jmp 0xc987f
leaq 0x1b0(%rsp), %rdi
callq 0xa4bd0
jmp 0xc9782
movq 0x1c8(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rdi
callq 0xa3ec0
movq %rax, 0x160(%rsp)
movq 0x168(%rsp), %rdi
callq 0xa3ef0
movq %rax, 0x158(%rsp)
leaq 0x160(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0xa3e20
testb $0x1, %al
jne 0xc98e9
jmp 0xc99b5
leaq 0x160(%rsp), %rdi
callq 0xa4c00
movq %rax, 0x150(%rsp)
movq 0x220(%rsp), %rdi
addq $0x90, %rdi
movq 0x150(%rsp), %rsi
callq 0x8f940
movq %rax, 0x28(%rsp)
jmp 0xc9921
movq 0x28(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x220(%rsp), %rdi
addq $0x90, %rdi
callq 0x8f6e0
movq %rax, 0x140(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x8f920
testb $0x1, %al
jne 0xc9965
jmp 0xc99a1
movq 0x220(%rsp), %rdi
addq $0x58, %rdi
movq 0x150(%rsp), %rsi
callq 0x8fa20
movb %dl, 0x1f(%rsp)
movq %rax, 0x20(%rsp)
jmp 0xc9989
movb 0x1f(%rsp), %al
movq 0x20(%rsp), %rcx
movq %rcx, 0x130(%rsp)
movb %al, 0x138(%rsp)
jmp 0xc99a3
leaq 0x160(%rsp), %rdi
callq 0xa4bd0
jmp 0xc98cb
leaq 0x1d0(%rsp), %rdi
callq 0x8f530
leaq 0x210(%rsp), %rdi
callq 0x49460
jmp 0xc96ce
movq 0x220(%rsp), %rsi
addq $0x90, %rsi
movq 0x220(%rsp), %rdi
addq $0xc8, %rdi
callq 0xca090
leaq 0x230(%rsp), %rdi
callq 0x49330
jmp 0xc960d
movb $0x1, 0x12f(%rsp)
testb $0x1, 0x12f(%rsp)
je 0xc9d6a
movb $0x0, 0x12f(%rsp)
movq 0x240(%rsp), %rax
addq $0x30, %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rdi
callq 0x1cc40
movq %rax, 0x118(%rsp)
movq 0x120(%rsp), %rdi
callq 0x1cac0
movq %rax, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xc9a83
jmp 0xc9d65
leaq 0x118(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0x108(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x18d80
movq 0x108(%rsp), %rax
addq $0x40, %rax
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x1cc40
movq %rax, 0xc0(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x1cac0
movq %rax, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x492e0
testb $0x1, %al
jne 0xc9b04
jmp 0xc9ba0
leaq 0xc0(%rsp), %rdi
callq 0x49320
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
addq $0xc8, %rdi
callq 0x8f6b0
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rdi
addq $0xc8, %rdi
callq 0x8f6e0
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rsi
movq 0xa0(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
callq 0xca0c0
jmp 0xc9b73
jmp 0xc9b75
leaq 0xc0(%rsp), %rdi
callq 0x49330
jmp 0xc9ae6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
jmp 0xc9d56
movq 0x108(%rsp), %rsi
addq $0x100, %rsi            # imm = 0x100
leaq 0xd0(%rsp), %rdi
callq 0xca100
movb %al, 0x1e(%rsp)
jmp 0xc9bc2
movb 0x1e(%rsp), %al
testb $0x1, %al
jne 0xc9bcf
jmp 0xc9d37
movb $0x1, 0x12f(%rsp)
movq 0x108(%rsp), %rdi
addq $0x100, %rdi            # imm = 0x100
leaq 0xd0(%rsp), %rsi
callq 0xca090
jmp 0xc9bf5
movq 0x108(%rsp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x8f6b0
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0x8f6e0
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x8f710
testb $0x1, %al
jne 0xc9c53
jmp 0xc9d35
leaq 0x90(%rsp), %rdi
callq 0x8f730
movq %rax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x81a0
jmp 0xc9c6f
movq 0x108(%rsp), %rdi
addq $0x58, %rdi
leaq 0x68(%rsp), %rsi
callq 0x8f940
movq %rax, 0x10(%rsp)
jmp 0xc9c8c
movq 0x10(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x108(%rsp), %rdi
addq $0x58, %rdi
callq 0x8f6e0
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x8f920
testb $0x1, %al
jne 0xc9cc1
jmp 0xc9d19
movq 0x108(%rsp), %rdi
addq $0xc8, %rdi
leaq 0x68(%rsp), %rsi
callq 0x8fa20
movb %dl, 0x7(%rsp)
movq %rax, 0x8(%rsp)
jmp 0xc9ce5
movb 0x7(%rsp), %al
movq 0x8(%rsp), %rcx
movq %rcx, 0x48(%rsp)
movb %al, 0x50(%rsp)
jmp 0xc9d19
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x190(%rsp)
movl %eax, 0x18c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x8728
jmp 0xc9d56
leaq 0x68(%rsp), %rdi
callq 0x8728
leaq 0x90(%rsp), %rdi
callq 0x8f7d0
jmp 0xc9c35
jmp 0xc9d37
leaq 0xd0(%rsp), %rdi
callq 0x35900
leaq 0x118(%rsp), %rdi
callq 0x49330
jmp 0xc9a65
leaq 0xd0(%rsp), %rdi
callq 0x35900
jmp 0xc9d72
jmp 0xc9a11
addq $0x248, %rsp            # imm = 0x248
retq
movq 0x190(%rsp), %rdi
callq 0x8670
nop