// File: /workspace/github_cpp_makefile_star2_O2/NishadSaraf[P]Championship-Simulator/src/cache.cc
// Function: CACHE::handle_prefetch()
void CACHE::handle_prefetch()
{
    // handle prefetch
    uint32_t prefetch_cpu = PQ.entry[PQ.head].cpu;
    if (prefetch_cpu == NUM_CPUS)
        return;

    for (uint32_t i=0; i<MAX_READ; i++) {

        // handle the oldest entry
        if ((PQ.entry[PQ.head].event_cycle <= current_core_cycle[prefetch_cpu]) && (PQ.occupancy > 0)) {
            int index = PQ.head;

            // access cache
            uint32_t set = get_set(PQ.entry[index].address);
            int way = check_hit(&PQ.entry[index]);
            
            if (way >= 0) { // prefetch hit

                // update replacement policy
                if (cache_type == IS_LLC) {
                    llc_update_replacement_state(prefetch_cpu, set, way, block[set][way].full_addr, PQ.entry[index].ip, 0, PQ.entry[index].type, 1);

                }
                else
                    update_replacement_state(prefetch_cpu, set, way, block[set][way].full_addr, PQ.entry[index].ip, 0, PQ.entry[index].type, 1);

                // COLLECT STATS
                sim_hit[prefetch_cpu][PQ.entry[index].type]++;
                sim_access[prefetch_cpu][PQ.entry[index].type]++;

                // check fill level
                if (PQ.entry[index].fill_level < fill_level) {

                    if (PQ.entry[index].instruction) 
                        upper_level_icache[prefetch_cpu]->return_data(&PQ.entry[index]);
                    else // data
                        upper_level_dcache[prefetch_cpu]->return_data(&PQ.entry[index]);
                }

                HIT[PQ.entry[index].type]++;
                ACCESS[PQ.entry[index].type]++;
                
                // remove this entry from PQ
                PQ.remove_queue(&PQ.entry[index]);
            }
            else { // prefetch miss

                DP ( if (warmup_complete[prefetch_cpu]) {
                cout << "[" << NAME << "] " << __func__ << " prefetch miss";
                cout << " instr_id: " << PQ.entry[index].instr_id << " address: " << hex << PQ.entry[index].address;
                cout << " full_addr: " << PQ.entry[index].full_addr << dec << " fill_level: " << PQ.entry[index].fill_level;
                cout << " cycle: " << PQ.entry[index].event_cycle << endl; });

                // check mshr
                uint8_t miss_handled = 1;
                int mshr_index = check_mshr(&PQ.entry[index]);

                if ((mshr_index == -1) && (MSHR.occupancy < MSHR_SIZE)) { // this is a new miss

                    DP ( if (warmup_complete[PQ.entry[index].cpu]) {
                    cout << "[" << NAME << "_PQ] " <<  __func__ << " want to add instr_id: " << PQ.entry[index].instr_id << " address: " << hex << PQ.entry[index].address;
                    cout << " full_addr: " << PQ.entry[index].full_addr << dec;
                    cout << " occupancy: " << lower_level->get_occupancy(3) << " SIZE: " << lower_level->get_size(3) << endl; });

                    // first check if the lower level PQ is full or not
                    // this is possible since multiple prefetchers can exist at each level of caches
                    if (lower_level) {
                        if (cache_type == IS_LLC) {
                            if (lower_level->get_occupancy(1) == lower_level->get_size(1))
                                miss_handled = 0;
                            else {
                                // add it to MSHRs if this prefetch miss will be filled to this cache level
                                if (PQ.entry[index].fill_level <= fill_level)
                                    add_mshr(&PQ.entry[index]);
                                
                                lower_level->add_rq(&PQ.entry[index]); // add it to the DRAM RQ
                            }
                        }
                        else {
                            if (lower_level->get_occupancy(3) == lower_level->get_size(3))
                                miss_handled = 0;
                            else {
                                // add it to MSHRs if this prefetch miss will be filled to this cache level
                                if (PQ.entry[index].fill_level <= fill_level)
                                    add_mshr(&PQ.entry[index]);

                                lower_level->add_pq(&PQ.entry[index]); // add it to the DRAM RQ
                            }
                        }
                    }
                }
                else {
                    if ((mshr_index == -1) && (MSHR.occupancy == MSHR_SIZE)) { // not enough MSHR resource

                        // TODO: should we allow prefetching with lower fill level at this case?
                        
                        // cannot handle miss request until one of MSHRs is available
                        miss_handled = 0;
                        STALL[PQ.entry[index].type]++;
                    }
                    else if (mshr_index != -1) { // already in-flight miss

                        // no need to update request except fill_level
                        // update fill_level
                        if (PQ.entry[index].fill_level < MSHR.entry[mshr_index].fill_level)
                            MSHR.entry[mshr_index].fill_level = PQ.entry[index].fill_level;

                        MSHR_MERGED[PQ.entry[index].type]++;

                        DP ( if (warmup_complete[prefetch_cpu]) {
                        cout << "[" << NAME << "] " << __func__ << " mshr merged";
                        cout << " instr_id: " << PQ.entry[index].instr_id << " prior_id: " << MSHR.entry[mshr_index].instr_id; 
                        cout << " address: " << hex << PQ.entry[index].address;
                        cout << " full_addr: " << PQ.entry[index].full_addr << dec << " fill_level: " << MSHR.entry[mshr_index].fill_level;
                        cout << " cycle: " << MSHR.entry[mshr_index].event_cycle << endl; });
                    }
                    else { // WE SHOULD NOT REACH HERE
                        cerr << "[" << NAME << "] MSHR errors" << endl;
                        assert(0);
                    }
                }

                if (miss_handled) {

                    DP ( if (warmup_complete[prefetch_cpu]) {
                    cout << "[" << NAME << "] " << __func__ << " prefetch miss handled";
                    cout << " instr_id: " << PQ.entry[index].instr_id << " address: " << hex << PQ.entry[index].address;
                    cout << " full_addr: " << PQ.entry[index].full_addr << dec << " fill_level: " << PQ.entry[index].fill_level;
                    cout << " cycle: " << PQ.entry[index].event_cycle << endl; });

                    MISS[PQ.entry[index].type]++;
                    ACCESS[PQ.entry[index].type]++;

                    // remove this entry from PQ
                    PQ.remove_queue(&PQ.entry[index]);
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0xad80(%rdi), %rax
movl 0xad14(%rdi), %ecx
imulq $0x380, %rcx, %rcx      # imm = 0x380
movl 0x330(%rax,%rcx), %r14d
cmpq $0x1, %r14
jne 0x4247
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %rbx
leaq 0xace8(%rdi), %rax
movq %rax, (%rsp)
leaq 0x10308(%rdi), %rcx
leaq 0x102e8(%rdi), %rax
leaq 0x8(%rdi,%r14,8), %rdx
movq %rdx, 0x10(%rsp)
leaq (%rdi,%r14,8), %rdx
addq $0x10, %rdx
movq %rdx, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x78c2c(%rip), %rbp     # 0x7ceb0
movq %r14, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movq %rcx, 0x20(%rsp)
addq %rax, %rdx
movq %rdx, 0x18(%rsp)
cmpl 0x739c(%rbx), %r12d
jae 0x4238
movq 0xad80(%rbx), %rax
movl 0xad14(%rbx), %ecx
imulq $0x380, %rcx, %rdx      # imm = 0x380
movq 0x378(%rax,%rdx), %rdx
cmpq (%rbp,%r14,8), %rdx
ja 0x446d
cmpl $0x0, 0xad1c(%rbx)
je 0x446d
movslq %ecx, %rcx
imulq $0x380, %rcx, %r15      # imm = 0x380
movq 0x340(%rax,%r15), %rsi
movq %rbx, %rdi
callq 0x2e58
movl %eax, %r13d
movq 0xad80(%rbx), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0x3772
testl %eax, %eax
js 0x4368
movl %eax, %ecx
imulq $0x50, %rcx, %rcx
cmpb $0x6, 0x73a4(%rbx)
movq 0x7390(%rbx), %rdx
movl %r13d, %esi
movq (%rdx,%rsi,8), %rdx
movq 0x20(%rdx,%rcx), %r8
movq 0xad80(%rbx), %rcx
movq 0x370(%rcx,%r15), %r9
movzbl 0x32f(%rcx,%r15), %r10d
jne 0x43d6
subq $0x8, %rsp
movq %rbx, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %eax, %ecx
pushq $0x1
pushq %r10
pushq $0x0
callq 0xfcc8
jmp 0x43f0
movq 0xad80(%rbx), %r13
leaq (%r15,%r13), %rsi
movq %rbx, %rdi
callq 0x3908
cmpl $-0x1, %eax
je 0x4475
movl 0x8(%r13,%r15), %ecx
movq 0xca20(%rbx), %rdx
cltq
imulq $0x380, %rax, %rax      # imm = 0x380
cmpl 0x8(%rdx,%rax), %ecx
jge 0x43a8
addq %rdx, %rax
addq $0x8, %rax
movl %ecx, (%rax)
movzbl 0x32f(%r13,%r15), %eax
incq 0x7308(%rbx,%rax,8)
movq 0xad80(%rbx), %rax
movzbl 0x32f(%rax,%r15), %ecx
incq 0x72e8(%rbx,%rcx,8)
jmp 0x4458
subq $0x8, %rsp
movq %rbx, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %eax, %ecx
pushq $0x1
pushq %r10
pushq $0x0
callq 0xfc4e
addq $0x20, %rsp
movq 0xad80(%rbx), %rsi
movzbl 0x32f(%rsi,%r15), %eax
shll $0x3, %eax
movq 0x20(%rsp), %rcx
incq (%rax,%rcx)
movq 0x18(%rsp), %rcx
incq (%rax,%rcx)
movl 0x8(%rsi,%r15), %eax
cmpl 0x7398(%rbx), %eax
jge 0x4440
addq %r15, %rsi
cmpb $0x0, (%rsi)
movq 0x10(%rsp), %rax
cmoveq 0x8(%rsp), %rax
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0xad80(%rbx), %rax
movzbl 0x32f(%rax,%r15), %ecx
incq 0x72c8(%rbx,%rcx,8)
leaq (%rax,%r15), %rsi
incq 0x72a8(%rbx,%rcx,8)
movq (%rsp), %rdi
callq 0x9cc0
incl %r12d
jmp 0x429b
movl 0x7388(%rbx), %eax
cmpl %eax, 0xc9bc(%rbx)
jae 0x44f1
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x43b9
cmpb $0x6, 0x73a4(%rbx)
movq (%rdi), %rax
movq 0x30(%rax), %rax
jne 0x4509
pushq $0x1
popq %rsi
callq *%rax
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
pushq $0x1
popq %rsi
callq *0x38(%rax)
cmpl %eax, %r13d
je 0x446d
movq 0xad80(%rbx), %rsi
movl 0x8(%rsi,%r15), %eax
cmpl 0x7398(%rbx), %eax
jg 0x44e0
addq %r15, %rsi
movq %rbx, %rdi
callq 0x3940
movq 0xad80(%rbx), %rsi
movq 0x18(%rbx), %rdi
addq %r15, %rsi
movq (%rdi), %rax
callq *(%rax)
jmp 0x43b9
jne 0x455f
movzbl 0x32f(%r13,%r15), %eax
incq 0x7328(%rbx,%rax,8)
jmp 0x446d
pushq $0x3
popq %rsi
callq *%rax
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq (%rdi), %rax
pushq $0x3
popq %rsi
callq *0x38(%rax)
cmpl %eax, %r13d
je 0x446d
movq 0xad80(%rbx), %rsi
movl 0x8(%rsi,%r15), %eax
cmpl 0x7398(%rbx), %eax
jg 0x454d
addq %r15, %rsi
movq %rbx, %rdi
callq 0x3940
movq 0xad80(%rbx), %rsi
movq 0x18(%rbx), %rdi
addq %r15, %rsi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x43b9
movq 0x11a8a(%rip), %rdi     # 0x15ff0
leaq 0xbb23(%rip), %rsi      # 0x10090
callq 0x2230
addq $0x7350, %rbx           # imm = 0x7350
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21d0
leaq 0xbb07(%rip), %rsi      # 0x10092
movq %rax, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x2110
leaq 0xbac5(%rip), %rdi      # 0x10067
leaq 0xbac0(%rip), %rsi      # 0x10069
leaq 0xbb56(%rip), %rcx      # 0x10106
movl $0x302, %edx            # imm = 0x302
callq 0x2160