// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/Cerebron.cc
// Function: CerebronPredictor::evictPolicy(int, bool)
int CerebronPredictor::evictPolicy(int set, bool inNVM)
{
	int assoc_victim = -1;
	assert(m_replacementPolicyNVM_ptr != NULL);
	assert(m_replacementPolicySRAM_ptr != NULL);

	CacheEntry* entry = NULL;
	if(inNVM)
	{	
		assoc_victim = m_replacementPolicyNVM_ptr->evictPolicy(set);
		entry = m_tableNVM[set][assoc_victim];
	}
	else
	{
		assoc_victim = m_replacementPolicySRAM_ptr->evictPolicy(set);
		entry = m_tableSRAM[set][assoc_victim];		
	}

	if(!entry->isValid)
		return assoc_victim;

	allocDecision des = getDecision(entry);
	if(simu_parameters.perceptron_drawFeatureMaps)
	{
		bool global_error = false;
		
		if(( des == ALLOCATE_IN_SRAM && inNVM ) || \
			( des == ALLOCATE_IN_NVM && !inNVM ))
			global_error = true;

		int hash1 = hashing_function1(m_features_hash[0] , entry->address , entry->missPC);
		int hash2 = (m_features.size() > 1) ? hashing_function1(m_features_hash[1], entry->address , entry->missPC) : 0;
		if(global_error)
			stats_global_error[hash1][hash2].first++;
		else
			stats_global_error[hash1][hash2].second++;
		
	
		for(unsigned i = 0; i < m_features.size() ; i++)
		{
			int hash = hashing_function1( m_features_hash[i] , entry->address , entry->missPC);

			bool local_error = false;
			if( des == ALLOCATE_IN_NVM && entry->PHC_allocation_pred[i].second == ALLOCATE_IN_SRAM)
				local_error = true;
			else if(  des == ALLOCATE_IN_SRAM && entry->PHC_allocation_pred[i].second == ALLOCATE_IN_NVM )
			 	local_error = true;
			 					
			if(local_error)	
				stats_local_error[i][hash].first++;
			else 
				stats_local_error[i][hash].second++;
				
		}

	}
		
	if( (des == ALLOCATE_IN_NVM && !inNVM) || \
		(des == ALLOCATE_IN_SRAM && inNVM) )
	{
		if(simu_parameters.printDebug)
		{
			string a = inNVM ? "NVM" : "SRAM"; 
			debug_file << cpt_time << ": Eviction of 0x" << std::hex  << entry->address \
				   << std::dec << " , wrongly allocated in " << a << ", cost=" << entry->cost_value << endl;
			
			for(unsigned i = 0; i < m_features.size() ; i++)
			{
				int hash = hashing_function1( m_features_hash[i] , entry->address , entry->missPC);
				int confidence = m_features[i]->getConfidence(hash, false);
				allocDecision des = m_features[i]->getAllocDecision(hash, false);
				debug_file << "\t-Feature " << i << "\t" << str_allocDecision[des] << "\t" << confidence << endl;
			}
		}
		
		stats_wrong_alloc++;
	}
	else
		stats_good_alloc++;
		
	/* Trigger the learning process */ 
	if( entry->isLearning_weight && !simu_parameters.Cerebron_fastlearning)
		doLearning(entry, inNVM);
	

	if(entry->nbWrite == 0 && entry->nbRead == 0)
		stats_nbDeadLine[stats_nbDeadLine.size()-1]++;

	evictRecording(set , assoc_victim , inNVM);	
	return assoc_victim;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq 0x60(%rdi), %rax
testq %rax, %rax
je 0x8844
movq %rdi, %r14
movq 0x68(%rdi), %rdi
testq %rdi, %rdi
je 0x8863
movl %edx, %r15d
movl %esi, %ebp
movslq %esi, %rbx
testb %r15b, %r15b
cmovneq %rax, %rdi
movzbl %r15b, %r13d
movq (%rdi), %rax
movl %ebx, %esi
callq *0x20(%rax)
movq 0x8(%r14,%r13,8), %rcx
movq (%rcx), %rsi
imulq $0x18, %rbx, %rcx
movl %eax, %ebx
movslq %eax, %rdx
movq (%rsi,%rcx), %rax
movq (%rax,%rdx,8), %r12
cmpb $0x1, (%r12)
jne 0x8830
movl %r15d, 0x1c(%rsp)
leaq 0x35b5d(%rip), %rax     # 0x3df50
cmpb $0x1, 0x226(%rax)
jne 0x8418
movq %rax, %rcx
movq 0x138(%r12), %rax
cmpq 0x140(%r12), %rax
movq %rcx, %rax
seta %r15b
jmp 0x842e
movsd 0x130(%r12), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
setbe %r15b
movl %r15d, %edx
xorb 0x1c(%rsp), %dl
cmpb $0x0, 0x1cc(%rax)
je 0x84d7
movb %dl, 0xf(%rsp)
movl %ebx, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movl %ebp, 0x14(%rsp)
movq 0x288(%r14), %rsi
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
callq 0x9f3a
movq 0x8(%r12), %rsi
movq 0x20(%r12), %rdx
movq %rbx, %rdi
callq 0x11de8
movl %eax, %ebx
leaq 0x70(%rsp), %rdi
callq 0x5618
movq 0x278(%r14), %rax
subq 0x270(%r14), %rax
xorl %r13d, %r13d
cmpq $0x9, %rax
jb 0x84e9
movq 0x288(%r14), %rsi
addq $0x28, %rsi
leaq 0x48(%rsp), %rbp
movq %rbp, %rdi
callq 0x9f3a
movq 0x8(%r12), %rsi
movq 0x20(%r12), %rdx
movq %rbp, %rdi
callq 0x11de8
movl %eax, %ebp
leaq 0x48(%rsp), %rdi
callq 0x5618
movslq %ebp, %rax
jmp 0x84eb
movl $0x430, %ecx            # imm = 0x430
testb %dl, %dl
jne 0x85e1
jmp 0x87da
xorl %eax, %eax
movslq %ebx, %rcx
movq 0x490(%r14), %rdx
imulq $0x18, %rcx, %rcx
movq (%rdx,%rcx), %rcx
leaq (%rcx,%rax,8), %rdx
leaq (%rcx,%rax,8), %rax
addq $0x4, %rax
cmpb $0x0, 0xf(%rsp)
cmovneq %rdx, %rax
incl (%rax)
leaq 0xc0(%rsp), %rbx
movl %r13d, %r13d
movq 0x278(%r14), %rax
subq 0x270(%r14), %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x85bd
imulq $0x28, %r13, %rsi
addq 0x288(%r14), %rsi
movq %rbx, %rdi
callq 0x9f3a
movq 0x8(%r12), %rsi
movq 0x20(%r12), %rdx
movq %rbx, %rdi
callq 0x11de8
movl %eax, %ebp
movq %rbx, %rdi
callq 0x5618
movq 0xd0(%r12), %rax
movl 0x4(%rax,%r13,8), %ecx
movslq %ebp, %rax
testb %r15b, %r15b
je 0x859b
testl %ecx, %ecx
je 0x85a0
movq 0x478(%r14), %rcx
imulq $0x18, %r13, %rdx
movq (%rcx,%rdx), %rcx
leaq (%rcx,%rax,8), %rax
addq $0x4, %rax
jmp 0x85b3
cmpl $0x1, %ecx
jne 0x8582
movq 0x478(%r14), %rcx
imulq $0x18, %r13, %rdx
shlq $0x3, %rax
addq (%rcx,%rdx), %rax
incl (%rax)
incl %r13d
jmp 0x851c
movl $0x430, %ecx            # imm = 0x430
cmpb $0x0, 0xf(%rsp)
movl 0x14(%rsp), %ebp
movq 0x20(%rsp), %r13
movl 0x10(%rsp), %ebx
leaq 0x35975(%rip), %rax     # 0x3df50
je 0x87da
movl $0x438, %ecx            # imm = 0x438
cmpb $0x1, (%rax)
jne 0x87da
leaq 0x21206(%rip), %rax     # 0x297fc
leaq 0x21208(%rip), %rsi     # 0x29805
cmpb $0x0, 0x1c(%rsp)
cmovneq %rax, %rsi
leaq 0x28(%rsp), %rdi
leaq 0x1b(%rsp), %rdx
callq 0x5140
movl %ebx, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movl %ebp, 0x14(%rsp)
leaq 0x358ef(%rip), %rax     # 0x3df18
movq (%rax), %rsi
leaq 0x34e2d(%rip), %rdi     # 0x3d460
callq 0x5200
leaq 0x20bcf(%rip), %rsi     # 0x2920e
movq %rax, %rdi
callq 0x52c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq 0x8(%r12), %rsi
movq %rax, %rdi
callq 0x5200
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x20b9a(%rip), %rsi     # 0x2921f
movq %rax, %rdi
callq 0x52c0
leaq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x5290
leaq 0x20b97(%rip), %rsi     # 0x29238
movq %rax, %rdi
callq 0x52c0
movsd 0x130(%r12), %xmm0
movq %rax, %rdi
callq 0x54f0
movq %rax, %rdi
callq 0x5190
xorl %r13d, %r13d
leaq 0x98(%rsp), %rbx
movl %r13d, %r13d
movq 0x278(%r14), %rax
subq 0x270(%r14), %rax
sarq $0x3, %rax
cmpq %r13, %rax
jbe 0x87b7
imulq $0x28, %r13, %rsi
addq 0x288(%r14), %rsi
movq %rbx, %rdi
callq 0x9f3a
movq 0x8(%r12), %rsi
movq 0x20(%r12), %rdx
movq %rbx, %rdi
callq 0x11de8
movl %eax, %r15d
movq %rbx, %rdi
callq 0x5618
movq 0x270(%r14), %rax
movq (%rax,%r13,8), %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x11132
movl %eax, %ebp
movq 0x270(%r14), %rax
movq (%rax,%r13,8), %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x11088
movl %eax, %r15d
leaq 0x34d0e(%rip), %rdi     # 0x3d460
leaq 0x20ae7(%rip), %rsi     # 0x29240
callq 0x52c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x5200
movq %rax, %rdi
leaq 0x224b0(%rip), %rsi     # 0x2ac23
callq 0x52c0
movl %r15d, %ecx
leaq 0x34c0e(%rip), %rdx     # 0x3d390
movq (%rdx,%rcx,8), %rsi
movq %rax, %rdi
callq 0x52c0
movq %rax, %rdi
leaq 0x2248b(%rip), %rsi     # 0x2ac23
callq 0x52c0
movq %rax, %rdi
movl %ebp, %esi
callq 0x5560
movq %rax, %rdi
callq 0x5190
incl %r13d
jmp 0x86ce
leaq 0x28(%rsp), %rdi
callq 0x5618
movl 0x14(%rsp), %ebp
movq 0x20(%rsp), %r13
movl 0x10(%rsp), %ebx
leaq 0x3577b(%rip), %rax     # 0x3df50
movl $0x438, %ecx            # imm = 0x438
incq (%r14,%rcx)
cmpb $0x1, 0x14a(%r12)
jne 0x8800
cmpb $0x0, 0x225(%rax)
jne 0x8800
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %edx
callq 0x7f86
cmpl $0x0, 0x11c(%r12)
jne 0x8821
cmpl $0x0, 0x120(%r12)
jne 0x8821
movq 0x3a8(%r14), %rax
incq -0x8(%rax)
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r13d, %ecx
callq 0x1f248
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20946(%rip), %rdi     # 0x29191
leaq 0x2082d(%rip), %rsi     # 0x2907f
leaq 0x2095b(%rip), %rcx     # 0x291b4
movl $0xf0, %edx
callq 0x51e0
leaq 0x20980(%rip), %rdi     # 0x291ea
leaq 0x2080e(%rip), %rsi     # 0x2907f
leaq 0x2093c(%rip), %rcx     # 0x291b4
movl $0xf1, %edx
callq 0x51e0
movq %rax, %rbx
jmp 0x88cd
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x88c8
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x88c8
jmp 0x88c0
jmp 0x88c0
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x5618
jmp 0x88c3
jmp 0x88c0
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x88c8
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5618
movq %rbx, %rdi
callq 0x5570
nop