// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq (%rdi), %r14
testq %r14, %r14
je 0x2ca2
leaq 0x60(%r13), %r15
leaq 0x68(%r13), %r12
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
incl %ebx
cmpl 0x10(%r13), %ebx
jge 0x2ca2
movq 0x8(%r14), %rax
testq %rax, %rax
je 0x2c8e
cmpl $0x0, (%rax)
jne 0x2c8e
movl 0x8(%rax), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x307c
movq %r13, %rdi
movq %r14, %rsi
callq 0x30c6
movq 0x28(%r14), %r14
testq %r14, %r14
jne 0x2c6a
cmpl $0x0, 0x68(%r13)
jle 0x2cc6
xorl %ebx, %ebx
movq 0x60(%r13), %rax
movl (%rax,%rbx,4), %esi
movq %r13, %rdi
callq 0x2f3f
incq %rbx
movslq 0x68(%r13), %rax
cmpq %rax, %rbx
jl 0x2cab
cmpl $0x0, 0x58(%r13)
jle 0x2cea
xorl %ebx, %ebx
movq 0x50(%r13), %rax
movl (%rax,%rbx,4), %esi
movq %r13, %rdi
callq 0x2f3f
incq %rbx
movslq 0x58(%r13), %rax
cmpq %rax, %rbx
jl 0x2ccf
movslq 0x78(%r13), %r14
movl $0x4, %esi
movq %r14, %rdi
callq 0x20b0
movq %rax, 0x10(%rsp)
movl $0x4, %esi
movq %r14, %rdi
callq 0x20b0
movq %rax, %r12
movq %r13, 0x8(%rsp)
movq (%r13), %r13
testq %r13, %r13
je 0x2dc7
xorl %ebp, %ebp
movq 0x8(%rsp), %rbx
movq 0x8(%rsp), %rax
cmpl 0x10(%rax), %ebp
jge 0x2dc7
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x2d76
cmpl $0x0, (%rax)
jne 0x2d76
cmpl $0x12, (%r13)
je 0x2d76
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2f3f
cltq
movq 0x10(%rsp), %rcx
movl %ebp, (%rcx,%rax,4)
movq 0x8(%r13), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2f3f
cltq
movl %ebp, (%r12,%rax,4)
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x2d97
cmpl $0x0, (%rax)
jne 0x2d97
movl 0x8(%rax), %esi
movq 0x8(%rsp), %rdi
callq 0x2f3f
cltq
movl %ebp, (%r12,%rax,4)
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x2db8
cmpl $0x0, (%rax)
jne 0x2db8
movl 0x8(%rax), %esi
movq 0x8(%rsp), %rdi
callq 0x2f3f
cltq
movl %ebp, (%r12,%rax,4)
incl %ebp
movq 0x28(%r13), %r13
testq %r13, %r13
jne 0x2d29
movq 0x8(%rsp), %rbp
movq (%rbp), %rax
xorl %ebx, %ebx
testq %rax, %rax
je 0x2de9
movq 0x10(%rsp), %rdi
incl %ebx
movq 0x20(%rax), %rax
testq %rax, %rax
jne 0x2ddc
jmp 0x2dee
movq 0x10(%rsp), %rdi
testl %r14d, %r14d
jle 0x2e2a
leaq 0x644e(%rip), %r13      # 0x9248
xorl %r15d, %r15d
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 0x10(%rsp), %rdi
incq %r15
cmpq %r15, %r14
jne 0x2dfd
testl %r14d, %r14d
jle 0x2eb3
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq %rbx, %r15
je 0x2ea3
movl (%r12,%r15,4), %eax
cmpl (%rdi,%rbx,4), %eax
jle 0x2ea3
movl (%rdi,%r15,4), %eax
cmpl (%r12,%rbx,4), %eax
jge 0x2ea3
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rax
movq (%rax,%r15,8), %rbp
movq (%rax,%rbx,8), %r13
movslq 0x18(%r13), %rax
testq %rax, %rax
jle 0x2e7e
movq 0x10(%r13), %rcx
xorl %edx, %edx
cmpq %rbp, (%rcx,%rdx,8)
je 0x2ea3
incq %rdx
cmpq %rdx, %rax
jne 0x2e70
movl (%r13), %esi
movl (%rbp), %edx
leaq 0x63e3(%rip), %rdi      # 0x926f
xorl %eax, %eax
callq 0x2080
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3e04
movq 0x10(%rsp), %rdi
incq %r15
cmpq %r14, %r15
jne 0x2e38
incq %rbx
cmpq %r14, %rbx
jne 0x2e35
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