// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movl %ecx, %eax
movq 0x8(%rdi,%rax,8), %rax
movq (%rax), %rax
movq %rsi, 0x20(%rsp)
leaq (%rsi,%rsi,2), %rcx
movq (%rax,%rcx,8), %rax
movq %rdx, 0x28(%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, 0x7(%rsp)
cmpb $0x1, %cl
jne 0x213f6
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, 0x8(%rsp)
incl (%rax,%rdx,4)
movq 0x278(%r15), %rax
cmpq 0x270(%r15), %rax
je 0x214e7
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %rcx
movq %rcx, 0x10(%rsp)
movq 0x30(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movl %eax, %r14d
xorb $0x1, %r14b
movl $0x1, %ebp
xorl %r12d, %r12d
movq 0x288(%r15), %rax
movq 0x30(%rsp), %rdi
movq 0x10(%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 0x14a60
movl %eax, %ecx
negl %ecx
cmovsl %eax, %ecx
leaq 0x24ab2(%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)
movq 0x278(%r15), %rax
subq 0x270(%r15), %rax
movl %ebp, %r12d
sarq $0x3, %rax
incl %ebp
cmpq %r12, %rax
ja 0x21462
cmpb $0x0, 0x7(%rsp)
je 0x215a9
movq %r13, 0x10(%rsp)
movq 0x278(%r15), %rax
cmpq 0x270(%r15), %rax
movq 0x28(%rsp), %rbp
je 0x21560
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %r12
movq 0x30(%rax), %r13
movl $0x1, %ebx
xorl %r14d, %r14d
movq 0x288(%r15), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *(%rax,%r14,8)
movq 0x270(%r15), %rcx
movq (%rcx,%r14,8), %rdi
movl %eax, %esi
callq 0x14a1c
movq 0x278(%r15), %rax
subq 0x270(%r15), %rax
movl %ebx, %r14d
sarq $0x3, %rax
incl %ebx
cmpq %r14, %rax
ja 0x21521
leaq 0x70(%rsp), %rax
movq 0x30(%rax), %rsi
movq %r15, %rdi
movq 0x20(%rsp), %rbx
movl %ebx, %edx
callq 0x26fe4
movq 0x2b8(%r15), %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rcx
addl 0x4(%rcx), %eax
cvtsi2sd %eax, %xmm0
movq 0x10(%rsp), %rax
movsd %xmm0, 0x130(%rax)
incq 0x3c8(%r15)
jmp 0x215d4
movq 0x2b8(%r15), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rax
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x130(%r13)
movq 0x28(%rsp), %rbp
movq 0x20(%rsp), %rbx
movzbl 0x1c(%rsp), %ecx
movq %r15, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x26892
nop