1 //===- bolt/Passes/RegReAssign.h --------------------------------*- 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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef BOLT_PASSES_REGREASSIGN_H
10 #define BOLT_PASSES_REGREASSIGN_H
11 
12 #include "bolt/Passes/BinaryPasses.h"
13 #include "bolt/Passes/RegAnalysis.h"
14 
15 namespace llvm {
16 namespace bolt {
17 
18 class RegReAssign : public BinaryFunctionPass {
19   std::vector<int64_t> RegScore;
20   std::vector<size_t> RankedRegs;
21   BitVector ClassicRegs;
22   BitVector CalleeSaved;
23   BitVector ClassicCSR;
24   BitVector ExtendedCSR;
25   BitVector GPRegs;
26 
27   /// Hooks to other passes
28   std::unique_ptr<RegAnalysis> RA;
29   std::unique_ptr<BinaryFunctionCallGraph> CG;
30 
31   /// Stats
32   DenseSet<const BinaryFunction *> FuncsChanged;
33   int64_t StaticBytesSaved{0};
34   int64_t DynBytesSaved{0};
35 
36   void swap(BinaryFunction &Function, MCPhysReg A, MCPhysReg B);
37   void rankRegisters(BinaryFunction &Function);
38   void aggressivePassOverFunction(BinaryFunction &Function);
39   bool conservativePassOverFunction(BinaryFunction &Function);
40   void setupAggressivePass(BinaryContext &BC,
41                            std::map<uint64_t, BinaryFunction> &BFs);
42   void setupConservativePass(BinaryContext &BC,
43                              std::map<uint64_t, BinaryFunction> &BFs);
44 
45 public:
46   /// BinaryPass public interface
47 
RegReAssign(const cl::opt<bool> & PrintPass)48   explicit RegReAssign(const cl::opt<bool> &PrintPass)
49       : BinaryFunctionPass(PrintPass) {}
50 
getName()51   const char *getName() const override { return "regreassign"; }
52 
shouldPrint(const BinaryFunction & BF)53   bool shouldPrint(const BinaryFunction &BF) const override {
54     return BinaryFunctionPass::shouldPrint(BF) && FuncsChanged.count(&BF) > 0;
55   }
56 
57   void runOnFunctions(BinaryContext &BC) override;
58 };
59 } // namespace bolt
60 } // namespace llvm
61 
62 #endif
63