75 lines · c
1//==- RegAllocScore.h - evaluate regalloc policy quality ----------*-C++-*-==//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8/// Calculate a measure of the register allocation policy quality. This is used9/// to construct a reward for the training of the ML-driven allocation policy.10/// Currently, the score is the sum of the machine basic block frequency-weighed11/// number of loads, stores, copies, and remat instructions, each factored with12/// a relative weight.13//===----------------------------------------------------------------------===//14 15#ifndef LLVM_CODEGEN_REGALLOCSCORE_H_16#define LLVM_CODEGEN_REGALLOCSCORE_H_17 18#include "llvm/ADT/STLFunctionalExtras.h"19#include "llvm/Support/Compiler.h"20 21namespace llvm {22 23class MachineBasicBlock;24class MachineBlockFrequencyInfo;25class MachineFunction;26class MachineInstr;27 28/// Regalloc score.29class RegAllocScore final {30 double CopyCounts = 0.0;31 double LoadCounts = 0.0;32 double StoreCounts = 0.0;33 double CheapRematCounts = 0.0;34 double LoadStoreCounts = 0.0;35 double ExpensiveRematCounts = 0.0;36 37public:38 RegAllocScore() = default;39 RegAllocScore(const RegAllocScore &) = default;40 41 double copyCounts() const { return CopyCounts; }42 double loadCounts() const { return LoadCounts; }43 double storeCounts() const { return StoreCounts; }44 double loadStoreCounts() const { return LoadStoreCounts; }45 double expensiveRematCounts() const { return ExpensiveRematCounts; }46 double cheapRematCounts() const { return CheapRematCounts; }47 48 void onCopy(double Freq) { CopyCounts += Freq; }49 void onLoad(double Freq) { LoadCounts += Freq; }50 void onStore(double Freq) { StoreCounts += Freq; }51 void onLoadStore(double Freq) { LoadStoreCounts += Freq; }52 void onExpensiveRemat(double Freq) { ExpensiveRematCounts += Freq; }53 void onCheapRemat(double Freq) { CheapRematCounts += Freq; }54 55 RegAllocScore &operator+=(const RegAllocScore &Other);56 LLVM_ABI_FOR_TEST bool operator==(const RegAllocScore &Other) const;57 bool operator!=(const RegAllocScore &Other) const;58 LLVM_ABI_FOR_TEST double getScore() const;59};60 61/// Calculate a score. When comparing 2 scores for the same function but62/// different policies, the better policy would have a smaller score.63/// The implementation is the overload below (which is also easily unittestable)64RegAllocScore calculateRegAllocScore(const MachineFunction &MF,65 const MachineBlockFrequencyInfo &MBFI);66 67/// Implementation of the above, which is also more easily unittestable.68LLVM_ABI_FOR_TEST RegAllocScore calculateRegAllocScore(69 const MachineFunction &MF,70 llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,71 llvm::function_ref<bool(const MachineInstr &)> IsTriviallyRematerializable);72} // end namespace llvm73 74#endif // LLVM_CODEGEN_REGALLOCSCORE_H_75