// File: /workspace/github_cpp_makefile_star2_O3/gvaumour[P]cache-simulator/src/PHC.cc
// Function: PHCPredictor::insertionPolicy(unsigned long, unsigned long, bool, Access)
void
PHCPredictor::insertionPolicy(uint64_t set, uint64_t index, bool inNVM, Access element)
{
	CacheEntry* entry = get_entry(set , index , inNVM);
	entry->policyInfo = m_cpt++;

	RD_TYPE rd = RD_NOT_ACCURATE;
	
	stats_allocate++;
	if( element.isSRAMerror)
	{
		rd = RD_MEDIUM;	
		stats_cptSRAMerror++;
	}
		
	stats_access_class[rd][element.isWrite()]++;
	
	/** Training learning cache lines */ 
	/*if(entry->isLearning)
	{
//		cout << "Insertion Learning Cache line 0x" << std::hex << element.block_addr << endl;
	*/
/*
	string alloc = inNVM ? "NVM" : "SRAM";
	cout << "Insertion Block addr = 0x" << std::hex << element.block_addr << std::dec << " in " << alloc << "; Prediction per feature:" << endl;
*/
	for(unsigned i = 0 ; i < m_features.size() ; i++)
	{
		int hash = m_features_hash[i](element.block_addr , element.m_pc);
		int pred = m_features[i]->getAllocationPrediction(hash, true);		
		allocDecision des = ALLOCATE_IN_SRAM;
		if( abs(pred) < simu_parameters.perceptron_allocation_threshold)
			des = element.isWrite() ? ALLOCATE_IN_SRAM : ALLOCATE_IN_NVM;
		if(pred > 0 )
			des = ALLOCATE_IN_NVM;
			
//		cout << "\t- " << m_criterias_names[i] << " : " << str_allocDecision[des]  << ", " << hash  << ", " << pred << endl; 
		entry->PHC_allocation_pred[i].second = des; 
		entry->PHC_allocation_pred[i].first = hash; 
		
	}
	if( element.isSRAMerror)
	{
		for(unsigned i = 0 ; i < m_features.size() ; i++)
		{
			int hash = m_features_hash[i](element.block_addr , element.m_pc);
			m_features[i]->increaseAlloc(hash);			
		}
		entry->cost_value = missingTagCostValue(element.block_addr , set) +  m_costAccess[element.isWrite()][RD_MEDIUM];
		stats_cptLearningSRAMerror++;		
	}
	else
		entry->cost_value = m_costAccess[element.isWrite()][RD_SHORT];

			
	Predictor::insertionPolicy(set , index , inNVM , element);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
movl %ecx, 0xc(%rsp)
movl %ecx, %eax
movq 0x8(%rdi,%rax,8), %rax
movq (%rax), %rax
movq %rsi, 0x18(%rsp)
leaq (%rsi,%rsi,2), %rcx
movq (%rax,%rcx,8), %rax
movq %rdx, 0x20(%rsp)
movq (%rax,%rdx,8), %r13
movq 0x2d8(%rdi), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x2d8(%rdi)
movl %eax, 0x38(%r13)
incq 0x3b8(%rdi)
leaq 0x70(%rsp), %rdx
movb 0x28(%rdx), %cl
movl $0x2, %eax
movb %cl, 0xb(%rsp)
cmpb $0x1, %cl
jne 0x2126a
incq 0x3d0(%r15)
movl $0x1, %eax
movq 0x430(%r15), %rcx
leaq (%rax,%rax,2), %rax
movl 0x1c(%rdx), %edx
cmpl $0x3, %edx
sete %sil
cmpl $0x6, %edx
sete %dl
orb %sil, %dl
movzbl %dl, %edx
movq (%rcx,%rax,8), %rax
movq %rdx, 0x10(%rsp)
incl (%rax,%rdx,4)
movq 0x278(%r15), %rax
movq %rax, %rcx
cmpq 0x270(%r15), %rax
je 0x21364
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x30(%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %r14d
xorb $0x1, %r14b
movl $0x1, %ebp
xorl %r12d, %r12d
movq 0x288(%r15), %rax
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq *(%rax,%r12,8)
movl %eax, %ebx
movq 0x270(%r15), %rax
movq (%rax,%r12,8), %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x148ea
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leaq 0x24c3b(%rip), %rdx     # 0x45f50
cmpl 0x1c0(%rdx), %ecx
setl %cl
andb %r14b, %cl
testl %eax, %eax
setg %al
orb %cl, %al
movzbl %al, %eax
andl $0x1, %eax
movq 0xd0(%r13), %rcx
movl %eax, 0x4(%rcx,%r12,8)
movl %ebx, (%rcx,%r12,8)
movl %ebp, %r12d
movq 0x270(%r15), %rax
movq 0x278(%r15), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
incl %ebp
cmpq %r12, %rdx
ja 0x212d9
cmpb $0x0, 0xb(%rsp)
je 0x213cd
movq %r13, %rbp
cmpq %rax, %rcx
je 0x213fa
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %r13
movq 0x30(%rax), %r12
movl $0x1, %ebx
xorl %r14d, %r14d
movq 0x288(%r15), %rax
movq %r12, %rdi
movq %r13, %rsi
callq *(%rax,%r14,8)
movq 0x270(%r15), %rcx
movq (%rcx,%r14,8), %rdi
movl %eax, %esi
callq 0x148a8
movq 0x278(%r15), %rax
subq 0x270(%r15), %rax
movl %ebx, %r14d
sarq $0x3, %rax
incl %ebx
cmpq %r14, %rax
ja 0x2138c
jmp 0x21403
movq 0x2b8(%r15), %rax
movq 0x10(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x130(%r13)
movq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rbx
jmp 0x21444
leaq 0x70(%rsp), %rax
movq 0x30(%rax), %r12
movq %r15, %rdi
movq %r12, %rsi
movq 0x18(%rsp), %rbx
movl %ebx, %edx
callq 0x26d8a
movq 0x2b8(%r15), %rcx
movq 0x10(%rsp), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rcx
addl 0x4(%rcx), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x130(%rbp)
incq 0x3c8(%r15)
movq 0x20(%rsp), %rdx
movl 0xc(%rsp), %eax
movzbl %al, %ecx
movq %r15, %rdi
movq %rbx, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x26664