// File: /workspace/github_c_makefile_star2_O2/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
xorl %ebx, %ebx
movq %rdi, %r14
movq (%r14), %r14
testq %r14, %r14
je 0x2c0d
cmpl 0x10(%rbp), %ebx
jge 0x2c0d
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2fb1
movq %rbp, %rdi
movq %r14, %rsi
callq 0x301e
incl %ebx
addq $0x28, %r14
jmp 0x2be2
xorl %ebx, %ebx
movslq 0x68(%rbp), %rax
cmpq %rax, %rbx
jge 0x2c2c
movq 0x60(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %rbp, %rdi
callq 0x2ea9
incq %rbx
jmp 0x2c0f
xorl %ebx, %ebx
movslq 0x58(%rbp), %rax
cmpq %rax, %rbx
jge 0x2c4b
movq 0x50(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %rbp, %rdi
callq 0x2ea9
incq %rbx
jmp 0x2c2e
movslq 0x78(%rbp), %r15
movl %r15d, %r12d
pushq $0x4
popq %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x20a0
movq %rax, 0x8(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x20a0
movq %rax, %r15
movq %rbp, %r13
xorl %ebp, %ebp
movq %r13, (%rsp)
movq (%rsp), %rbx
movq (%r13), %r13
testq %r13, %r13
je 0x2d25
movq (%rsp), %rax
cmpl 0x10(%rax), %ebp
jge 0x2d25
movq 0x8(%r13), %rax
testq %rax, %rax
je 0x2cda
cmpl $0x0, (%rax)
jne 0x2cda
cmpl $0x12, (%r13)
je 0x2cda
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2ea9
cltq
movq 0x8(%rsp), %rcx
movl %ebp, (%rcx,%rax,4)
movq 0x8(%r13), %rax
movl 0x8(%rax), %esi
movq %rbx, %rdi
callq 0x2ea9
cltq
movl %ebp, (%r15,%rax,4)
movq 0x10(%r13), %rax
testq %rax, %rax
je 0x2cfa
cmpl $0x0, (%rax)
jne 0x2cfa
movl 0x8(%rax), %esi
movq (%rsp), %rdi
callq 0x2ea9
cltq
movl %ebp, (%r15,%rax,4)
movq 0x18(%r13), %rax
testq %rax, %rax
je 0x2d1a
cmpl $0x0, (%rax)
jne 0x2d1a
movl 0x8(%rax), %esi
movq (%rsp), %rdi
callq 0x2ea9
cltq
movl %ebp, (%r15,%rax,4)
addq $0x28, %r13
incl %ebp
jmp 0x2c81
xorl %ebx, %ebx
movq (%rsp), %rbp
movq %rbp, %rax
movq 0x8(%rsp), %rdi
movq (%rax), %rax
testq %rax, %rax
je 0x2d43
incl %ebx
addq $0x20, %rax
jmp 0x2d33
testl %r12d, %r12d
movl $0x0, %eax
cmovgl %r12d, %eax
movq %rax, 0x10(%rsp)
leaq 0x54ed(%rip), %r13      # 0x8248
xorl %r14d, %r14d
cmpq %r14, 0x10(%rsp)
je 0x2d8f
movq 0x70(%rbp), %rax
movq (%rax,%r14,8), %rax
movl (%rax), %esi
movl (%rdi,%r14,4), %edx
addl %ebx, %edx
movl (%r15,%r14,4), %ecx
addl %ebx, %ecx
movq %r13, %rdi
xorl %eax, %eax
callq 0x2080
movq 0x8(%rsp), %rdi
incq %r14
jmp 0x2d5e
xorl %ebx, %ebx
cmpq 0x10(%rsp), %rbx
je 0x2e25
xorl %r14d, %r14d
cmpq %r12, %r14
je 0x2e1d
cmpq %rbx, %r14
je 0x2e18
movl (%r15,%r14,4), %eax
cmpl (%rdi,%rbx,4), %eax
jle 0x2e18
movl (%rdi,%r14,4), %eax
cmpl (%r15,%rbx,4), %eax
jge 0x2e18
movq (%rsp), %rax
movq 0x70(%rax), %rax
movq (%rax,%r14,8), %r13
movq (%rax,%rbx,8), %rbp
movl 0x18(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovlel %ecx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x2df3
movq 0x10(%rbp), %rdx
leaq 0x1(%rcx), %rsi
cmpq %r13, (%rdx,%rcx,8)
movq %rsi, %rcx
jne 0x2ddb
jmp 0x2e18
movl (%rbp), %esi
movl (%r13), %edx
leaq 0x546e(%rip), %rdi      # 0x826f
xorl %eax, %eax
callq 0x2080
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3c80
movq 0x8(%rsp), %rdi
incq %r14
jmp 0x2d9f
incq %rbx
jmp 0x2d91
movq %rdi, %rbx
movq %r15, %rdi
callq 0x2030
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030