// File: /workspace/github_c_makefile_star2_O3/Mithreindeir[P]ewcc/src/live.c
// Function: bb_set
void bb_set(struct bb *blk)
{
	struct ir_stmt *cur = blk->blk;

	int iter = 0;
	while (cur && iter < blk->len) {
		def(blk, cur);
		use(blk, cur);
		iter++;
		cur = cur->next;
	}

	for (int i = 0; i < blk->ndef; i++)
		(void)bb_get_treg(blk, blk->def[i]);
	for (int i = 0; i < blk->nuse; i++)
		(void)bb_get_treg(blk, blk->use[i]);

	cur = blk->blk;
	iter = 0;
	int *defd = calloc(blk->nvert, sizeof(int)), ndefd = blk->nvert;
	int *kill = calloc(blk->nvert, sizeof(int)), nkill = blk->nvert;

	while (cur && iter < blk->len) {
		/*Stores do not overwrite registers*/
		if (cur->result && cur->result->type == oper_reg && cur->type != stmt_store) {
			defd[bb_get_treg(blk, cur->result->val.virt_reg)] = iter;
			kill[bb_get_treg(blk, cur->result->val.virt_reg)] = iter;
		}
		/*set kill*/
		if (cur->arg1 && cur->arg1->type == oper_reg) {
			kill[bb_get_treg(blk, cur->arg1->val.virt_reg)] = iter;
		}
		if (cur->arg2 && cur->arg2->type == oper_reg) {
			kill[bb_get_treg(blk, cur->arg2->val.virt_reg)] = iter;
		}
		cur = cur->next;
		iter++;
	}
	/*Get the line number*/
	struct ir_stmt *cnt = blk->blk;
	int pl = 0;
	while (cnt) {
		pl++;
		cnt = cnt->prev;
	}

	for (int i = 0; i < nkill; i++)
		printf("R%d is defined at %d and killed at %d\n",blk->graph[i]->ocolor, pl+defd[i], pl+kill[i]);

	/*Construct interference graph*/
	for (int i = 0; i < ndefd; i++) {
		for (int j = 0; j < nkill; j++) {
			if (j == i) continue;
			if (kill[j] < (defd[i]+1) || (defd[j]+1) > kill[i])
				continue;
			struct vertex *v2 = blk->graph[j];
			struct vertex *v = blk->graph[i];
			int added = 0;
			for (int k = 0; k < v->num_siblings; k++) {
				if (v->siblings[k] == v2) {
					added = 1;
					break;
				}
			}
			if (!added) {
				printf("R%d interferes with R%d\n", v->ocolor, v2->ocolor);
				add_edge(v, v2);
			}
		}
	}

	free(kill);
	free(defd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbp
movq (%rdi), %r14
testq %r14, %r14
je 0x2cda
leaq 0x60(%rbp), %r15
leaq 0x68(%rbp), %r12
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
incl %ebx
cmpl 0x10(%rbp), %ebx
jge 0x2cda
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x2cc6
cmpl $0x0, (%rax)
jne 0x2cc6
movl 0x8(%rax), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x30ba
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3104
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x2ca3
cmpl $0x0, 0x68(%rbp)
jle 0x2cfd
xorl %ebx, %ebx
movq 0x60(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %rbp, %rdi
callq 0x2f89
incq %rbx
movslq 0x68(%rbp), %rax
cmpq %rax, %rbx
jl 0x2ce2
cmpl $0x0, 0x58(%rbp)
jle 0x2d20
xorl %ebx, %ebx
movq 0x50(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %rbp, %rdi
callq 0x2f89
incq %rbx
movslq 0x58(%rbp), %rax
cmpq %rax, %rbx
jl 0x2d05
movslq 0x78(%rbp), %r14
movl $0x4, %esi
movq %r14, %rdi
callq 0x20b0
movq %rax, 0x8(%rsp)
movl $0x4, %esi
movq %r14, %rdi
callq 0x20b0
movq %rax, %r12
movq (%rbp), %r13
testq %r13, %r13
movq %rbp, 0x10(%rsp)
je 0x2e28
xorl %ebp, %ebp
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rbx
movq 0x10(%rsp), %rax
cmpl 0x10(%rax), %ebp
jge 0x2e11
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x2db6
cmpl $0x0, (%rax)
jne 0x2db6
cmpl $0x12, (%r13)
je 0x2db6
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2f89
cltq
movq 0x8(%rsp), %rcx
movl %ebp, (%rcx,%rax,4)
movq 0x8(%r13), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2f89
movq 0x8(%rsp), %rdi
cltq
movl %ebp, (%r12,%rax,4)
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x2ddc
cmpl $0x0, (%rax)
jne 0x2ddc
movl 0x8(%rax), %esi
movq 0x10(%rsp), %rdi
callq 0x2f89
movq 0x8(%rsp), %rdi
cltq
movl %ebp, (%r12,%rax,4)
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x2e02
cmpl $0x0, (%rax)
jne 0x2e02
movl 0x8(%rax), %esi
movq 0x10(%rsp), %rdi
callq 0x2f89
movq 0x8(%rsp), %rdi
cltq
movl %ebp, (%r12,%rax,4)
incl %ebp
movq 0x28(%r13), %r13
testq %r13, %r13
jne 0x2d64
movq 0x10(%rsp), %rax
movq (%rax), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x2e2f
incl %ebx
movq 0x20(%rax), %rax
jmp 0x2e1b
xorl %ebx, %ebx
movq 0x8(%rsp), %rdi
testl %r14d, %r14d
jle 0x2efd
leaq 0x6409(%rip), %r13      # 0x9248
xorl %r15d, %r15d
movq 0x10(%rsp), %rbp
movq 0x70(%rbp), %rax
movq (%rax,%r15,8), %rax
movl (%rax), %esi
movl (%rdi,%r15,4), %edx
addl %ebx, %edx
movl (%r12,%r15,4), %ecx
addl %ebx, %ecx
movq %r13, %rdi
xorl %eax, %eax
callq 0x2080
movq 0x8(%rsp), %rdi
incq %r15
cmpq %r15, %r14
jne 0x2e47
testl %r14d, %r14d
jle 0x2efd
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %rbx, %r15
je 0x2eed
movl (%r12,%r15,4), %eax
cmpl (%rdi,%rbx,4), %eax
jle 0x2eed
movl (%rdi,%r15,4), %eax
cmpl (%r12,%rbx,4), %eax
jge 0x2eed
movq 0x10(%rsp), %rax
movq 0x70(%rax), %rax
movq (%rax,%r15,8), %rbp
movq (%rax,%rbx,8), %r13
movslq 0x18(%r13), %rax
testq %rax, %rax
jle 0x2ec8
movq 0x10(%r13), %rcx
xorl %edx, %edx
cmpq %rbp, (%rcx,%rdx,8)
je 0x2eed
incq %rdx
cmpq %rdx, %rax
jne 0x2eba
movl (%r13), %esi
movl (%rbp), %edx
leaq 0x6399(%rip), %rdi      # 0x926f
xorl %eax, %eax
callq 0x2080
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3de4
movq 0x8(%rsp), %rdi
incq %r15
cmpq %r14, %r15
jne 0x2e82
incq %rbx
cmpq %r14, %rbx
jne 0x2e7f
movq %rdi, %rbx
movq %r12, %rdi
callq 0x2030
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030