一、分支预测
1)局部分支预测器:
利用自己过去的跳转状态来进行预测。程序过去执行时分支的信息被存放在一个长度为 2^n 的表中,表中的每一项为 2 bit。该表通过指令 PC 的最低 n bits 来索引。每一项的 2 bits 都构成一个有限状态机,用来预测分支跳转条件。
2)关联预测器
利用branch global history 的寄存器,来存储最近几次分支执行结果,然后将寄存器与PC做异或运算得到索引,找到指定的状态机, 如果该状态的值超过一半,则预测跳转;如果该状态的值低于一半,则预测不跳转。
根据分支指令实际执行结果,来更新对应的饱和计数器,如果结果为跳转,则对应的状态值+1;如果结果为不跳转,则对应的状态值-1。更新全局历史寄存器,结果为跳转,将1移位到寄存器的最低位;结果为不跳转,将0移位到寄存器的最低位。
二、算法实现
1)局部预测
#include<stdio.h>
#define N 64
#define P 0//P为简单预测器初始值
/*(2,1)分支预测器*/
int main()
{
int hy_i[4][N], pt[4][N], ghr[2], max[4], j[4];//hy_i存储历史指令,pt存储对应的简单预测器的值,ghr记录前两位分支执行情况
int i, k, instr, addr, taken, ptaken, ic = 0, miss = 0;
FILE *fin;//读取文件
fin = fopen("history.txt", "r");
if(fin == NULL)
{
printf ("Failed to open the file !\n");
return 0;
}
ghr[0] = 0;//置零
ghr[1] = 0;
for(i = 0;i < 4;i++)
{
max[i] = 0;
j[i] = 0;
}
for(i = 0;i < N;i++)
for(k = 0;k < 4;k++)
pt[k][i] = 0;
while(!feof(fin))
{
fscanf(fin, "%x%x%d", &instr, &addr, &taken);
if(ghr[0] == 0 && ghr[1] == 0)//case1
{
for(i = 0;i < max[0];i++)
if(hy_i[0][i] == instr)//匹配到 PC
{
ptaken = pt[0][i];
if(ptaken != taken) //预测错误,翻转简单预测器
{
miss++;
pt[0][i] = (pt[0][i] + 1) % 2;
}
break;
}
if(i == max[0] && i < N) //未匹配到且深度未满,添加
{
hy_i[0][i] = instr;
if(P != taken)
{
miss++;
pt[0][i] = (pt[0][i] + 1) % 2;
}
max[0]++;
}
else if(i == N)//未匹配到且深度已满,更新
{
hy_i[0][j[0] % N] = instr;
if(P != taken)
{
miss++;
pt[0][j[0] % N] = (pt[0][j[0] % N] + 1) % 2;
}
j[0]++;
}
}
if(ghr[0] == 0 && ghr[1] == 1)//case2,后面三种同上
{
for(i = 0;i < max[1];i++)
if(hy_i[1][i] == instr)
{
ptaken = pt[1][i];
if(ptaken != taken)
{
miss++;
pt[1][i] = (pt[1][i] + 1) % 2;
}
break;
}
if(i == max[1] && i < N)
{
hy_i[1][i] = instr;
if(P != taken)
{
miss++;
pt[1][i] = (pt[1][i] + 1) % 2;
}
max[1]++;
}
else if(i == N)
{
hy_i[1][j[1] % N] = instr;
if(P != taken)
{
miss++;
pt[1][j[1] % N] = (pt[1][j[1] % N] + 1) % 2;
}
j[1]++;
}
}
if(ghr[0] == 1 && ghr[1] == 0)//case3
{
for(i = 0;i < max[2];i++)
if(hy_i[2][i] == instr)
{
ptaken = pt[2][i];
if(ptaken != taken)
{
miss++;
pt[2][i] = (pt[2][i] + 1) % 2;
}
break;
}
if(i == max[2] && i < N)
{
hy_i[2][i] = instr;
if(P != taken)
{
miss++;
pt[2][i] = (pt[2][i] + 1) % 2;
}
max[2]++;
}
else if(i == N)
{
hy_i[2][j[2] % N] = instr;
if(P != taken)
{
miss++;
pt[2][j[2] % N] = (pt[2][j[2] % N] + 1) % 2;
}
j[2]++;
}
}
if(ghr[0] == 1 && ghr[1] == 1)//case4
{
for(i = 0;i < max[3];i++)
if(hy_i[3][i] == instr)
{
ptaken = pt[3][i];
if(ptaken != taken)
{
miss++;
pt[3][i] = (pt[3][i] + 1) % 2;
}
break;
}
if(i == max[3] && i < N)
{
hy_i[3][i] = instr;
if(P != taken)
{
miss++;
pt[3][i] = (pt[3][i] + 1) % 2;
}
max[3]++;
}
else if(i == N)
{
hy_i[3][j[3] % N] = instr;
if(P != taken)
{
miss++;
pt[3][j[3] % N] = (pt[3][j[3] % N] + 1) % 2;
}
j[3]++;
}
}
ghr[0] = ghr[1];//更新 可能算法有错误,自己修改一下
ghr[1] = taken;
ic++;
printf("ic = %d, miss = %d, miss/ic = %f\n", ic, miss, float(miss) / float(ic));
}
fclose(fin);
return 0;
}
2)关联预测
///
Copyright 2022 by Li. //
///
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
// 饱和计数器:加1
static inline UINT32 SatIncrement(UINT32 x, UINT32 max)
{
if (x<max) return x + 1;
return x;
}
// 饱和计数器:减1
static inline UINT32 SatDecrement(UINT32 x)
{
if (x>0) return x - 1;
return x;
}
// The state is defined for Gshare, change for your design
// Gshare分支预测器的状态信息,你需要根据自己的设计进行调整
UINT32 ghr; // global history register 全局历史寄存器
UINT32 *pht; // pattern history table 模式历史表
UINT32 historyLength; // history length 历史长度
UINT32 numPhtEntries; // entries in pht PHT中的项数
#define PHT_CTR_MAX 3
#define PHT_CTR_INIT 2
#define HIST_LEN 17 // 全局历史寄存器长度,取17位
#define TAKEN 'T'
#define NOT_TAKEN 'N'
void PREDICTOR_init(void)
{
historyLength = HIST_LEN;
ghr = 0;
numPhtEntries = (1 << HIST_LEN); // 模式历史表,就有2^17项
pht = (UINT32 *)malloc(numPhtEntries * sizeof(UINT32));
// 将模式历史表,全部初始化为PHT_CTR_INIT
for (UINT32 ii = 0; ii< numPhtEntries; ii++) {
pht[ii] = PHT_CTR_INIT;
}
}
// Gshare分支预测器
// 将PC的低17位,与全局历史寄存器进行异或(加密),去索引PHT,得到对应的饱和状态
// 如果该状态的值超过一半,则预测跳转
// 如果该状态的值低于一半,则预测不跳转
char GetPrediction(UINT64 PC)
{
UINT32 phtIndex = (PC^ghr) % (numPhtEntries);
UINT32 phtCounter = pht[phtIndex];
if (phtCounter > (PHT_CTR_MAX / 2)) {
return TAKEN;
}
else {
return NOT_TAKEN;
}
}
// Gshare分支预测器
// 根据分支指令实际执行结果,来更新对应的饱和计数器
// 如果结果为跳转,则对应的饱和计数器+1
// 如果结果为不跳转,则对应的饱和计数器-1
// 更新全局历史寄存器:
// 结果为跳转,将1移位到GHR的最低位
// 结果为不跳转,将0移位到GHR的最低位
void UpdatePredictor(UINT64 PC, OpType opType, char resolveDir, char predDir, UINT64 branchTarget)
{
opType = opType;
predDir = predDir;
branchTarget = branchTarget;
UINT32 phtIndex = (PC^ghr) % (numPhtEntries);
UINT32 phtCounter = pht[phtIndex];
// printf("PC=%016llx resolveDir=%c predDir=%c branchTarget=%016llx\n", PC, resolveDir, predDir, branchTarget);
if (resolveDir == TAKEN) {
pht[phtIndex] = SatIncrement(phtCounter, PHT_CTR_MAX); // 如果结果为跳转,则对应的饱和计数器+1
}
else {
pht[phtIndex] = SatDecrement(phtCounter); // 如果结果为不跳转,则对应的饱和计数器-1
}
// update the GHR
ghr = (ghr << 1);
if (resolveDir == TAKEN) {
ghr = ghr | 0x1;
}
}
void PREDICTOR_free(void)
{
free(pht);
}