Non-Confidential | ![]() | 100964_1180_00_en | ||
| ||||
Home > Plug-ins for Fast Models > BranchPrediction > Creating a new branch predictor |
The example predictors that Arm provides are intended to show different techniques for branch prediction to help you create new ones. The source code for them is located in $PVLIB_HOME/plugins/source/BranchPrediction/.
A user-defined branch predictor must:
Derive from the abstract interface class
PredictorInterface
.
Implement the following functions:
Direction getPredictedDirection(VAddr64_t pc_) SG_OVERRIDE;
Returns the predicted branch direction, that is, taken or not taken.
void updateStructures(VAddr64_t pc_, Direction actual_direction_) SG_OVERRIDE;
Updates any tables or structures that the user-defined predictor implements, based on the actual branch direction.
Implement the following static functions:
static const char* getPredictorName()
Returns the name of the predictor. This is displayed in the log file.
static PredictorInterface* createInstance()
Creates an instance of the predictor.
Register the predictor in the registerPredictors()
function, which is defined in PredictorFactory.h. For example:
void registerPredictors(){ // All predictor types must be registered here map_predictor_instances[SamplePredictor::getPredictorName()] = &SamplePredictor::createInstance; … }
// SamplePredictor.h #include <ct/BranchPredictionPluginInterfaces.h> #include "PredictorInterface.h" namespace BranchPrediction { class SamplePredictor : public PredictorInterface { public: Direction getPredictedDirection(VAddr64_t pc_) SG_OVERRIDE; void updateStructures(VAddr64_t pc_, Direction actual_direction_) SG_OVERRIDE; public: SamplePredictor(Direction dir_ = Direction::TAKEN); ~ SamplePredictor(); static const char* getPredictorName() { return "SamplePredictor"; } static PredictorInterface* createInstance() { return new SamplePredictor(); } private: Direction default_direction; }; }; // SamplePredictor.cpp #include "SamplePredictor.h" namespace BranchPrediction { SamplePredictor::SamplePredictor() { // Initialize the predictor } SamplePredictor::~SamplePredictor() { // Do nothing } Direction SamplePredictor::getPredictedDirection(VAddr64_t pc_) { // Return the predicted direction // This is called when the CPU makes a branch prediction return Direction::TAKEN; } void SamplePredictor::updateStructures(VAddr64_t pc_, Direction actual_direction_) { // Update any structure or tables that this predictor uses // This is called when the branch has committed and the direction // taken is known. } };