1 /*========================== begin_copyright_notice ============================
2 
3 Copyright (C) 2021 Intel Corporation
4 
5 SPDX-License-Identifier: MIT
6 
7 ============================= end_copyright_notice ===========================*/
8 
9 #ifndef __SPLITALIGNEDSCALARS_H__
10 #define __SPLITALIGNEDSCALARS_H__
11 
12 #include "visa/GraphColor.h"
13 
14 namespace vISA
15 {
16     class SplitAlignedScalars
17     {
18     private:
19         const unsigned int MinOptDist = 200;
20         // Constant trip count assume for each loop to estimate dynamic inst
21         // count change due to splitting.
22         const unsigned int EstimatedLoopTripCount = 4;
23         // Threshold percent increase in estimated dynamic inst count allowed
24         const float BloatAllowed = 1.0f / 100.0f;
25 
26         unsigned int numDclsReplaced = 0;
27         unsigned int numMovsAdded = 0;
28         GlobalRA& gra;
29         GraphColor& coloring;
30         G4_Kernel& kernel;
31         bool changesMade = false;
32 
33         class Data
34         {
35         public:
36             unsigned int firstDef = 0;
37             unsigned int lastUse = 0;
38             bool allowed = true;
getDUMaxDist()39             unsigned int getDUMaxDist()
40             {
41                 return std::abs((int)lastUse - (int)firstDef);
42             };
43             std::vector<std::pair<G4_BB*, G4_INST*>> defs;
44             // store vector of <bb, inst, src#>
45             std::vector <std::tuple<G4_BB*, G4_INST*, unsigned int>> uses;
46         };
47 
48         std::unordered_map<G4_Declare*, Data> dclData;
49         std::unordered_map<G4_Declare*, G4_Declare*> oldNewDcls;
50 
51         bool static canReplaceDst(G4_INST* inst);
52         bool static canReplaceSrc(G4_INST* inst, unsigned int idx);
53 
54         bool heuristic(G4_Declare* dcl, Data& d);
55         bool isDclCandidate(G4_Declare* dcl);
56         std::vector<G4_Declare*> gatherCandidates();
57         void pruneCandidates(std::vector<G4_Declare*>& candidates);
58         unsigned int computeNumMovs(G4_Declare* dcl);
59 
60         template<class T>
61         G4_Declare* getDclForRgn(T* rgn, G4_Declare* newTopDcl);
62 
63         // store set of dcls marked as spill in current RA iteration
64         std::unordered_set<G4_Declare*> spilledDclSet;
65         // store spill cost for each dcl
66         std::unordered_map<G4_Declare*, float> dclSpillCost;
67         // store dcls that have callee save bias
68         std::unordered_set<G4_Declare*> calleeSaveBiased;
69 
70     public:
SplitAlignedScalars(GlobalRA & g,GraphColor & c)71         SplitAlignedScalars(GlobalRA& g, GraphColor& c) : gra(g), coloring(c), kernel(g.kernel)
72         {
73             for (auto spill : coloring.getSpilledLiveRanges())
74             {
75                 spilledDclSet.insert(spill->getDcl());
76             }
77             auto numVars = coloring.getNumVars();
78             auto lrs = coloring.getLiveRanges();
79             for (unsigned int i = 0; i != numVars; ++i)
80             {
81                 auto rootDcl = lrs[i]->getDcl();
82                 dclSpillCost[rootDcl] = lrs[i]->getSpillCost();
83                 if (lrs[i]->getCalleeSaveBias())
84                     calleeSaveBiased.insert(rootDcl);
85             }
86         }
87 
88         void run();
89 
getChangesMade()90         bool getChangesMade() { return changesMade; }
91 
92         void dump(std::ostream& of = std::cerr);
93     };
94 
95 }
96 #endif
97