1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAPLEBE_CG_INCLUDE_CGPRE_H 17 #define MAPLEBE_CG_INCLUDE_CGPRE_H 18 #include "cg_occur.h" 19 #include "cg_dominance.h" 20 #include "cgfunc.h" 21 22 namespace maplebe { 23 enum PreKind { kExprPre, kStmtPre, kLoadPre, kAddrPre }; 24 25 class CGPre { 26 public: CGPre(DomAnalysis & currDom,MemPool & memPool,MemPool & mp2,PreKind kind,uint32 limit)27 CGPre(DomAnalysis &currDom, MemPool &memPool, MemPool &mp2, PreKind kind, uint32 limit) 28 : dom(&currDom), 29 ssaPreMemPool(&memPool), 30 ssaPreAllocator(&memPool), 31 perCandMemPool(&mp2), 32 perCandAllocator(&mp2), 33 workList(ssaPreAllocator.Adapter()), 34 preKind(kind), 35 allOccs(ssaPreAllocator.Adapter()), 36 phiOccs(ssaPreAllocator.Adapter()), 37 exitOccs(ssaPreAllocator.Adapter()), 38 preLimit(limit), 39 dfPhiDfns(std::less<uint32>(), ssaPreAllocator.Adapter()), 40 varPhiDfns(std::less<uint32>(), ssaPreAllocator.Adapter()), 41 temp2LocalRefVarMap(ssaPreAllocator.Adapter()) 42 { 43 preWorkCandHashTable.GetWorkcandHashTable().fill(nullptr); 44 } 45 46 virtual ~CGPre() = default; 47 GetRealOccList()48 const MapleVector<CgOccur *> &GetRealOccList() const 49 { 50 return workCand->GetRealOccs(); 51 } 52 53 virtual BB *GetBB(uint32 id) const = 0; 54 virtual PUIdx GetPUIdx() const = 0; SetCurFunction(PUIdx)55 virtual void SetCurFunction(PUIdx) const {} 56 GetIterDomFrontier(const BB * bb,MapleSet<uint32> * dfset)57 void GetIterDomFrontier(const BB *bb, MapleSet<uint32> *dfset) const 58 { 59 for (uint32 bbid : dom->GetIdomFrontier(bb->GetId())) { 60 (void)dfset->insert(dom->GetDtDfnItem(bbid)); 61 } 62 } 63 GetWorkCand()64 PreWorkCand *GetWorkCand() const 65 { 66 return workCand; 67 } 68 // compute downsafety for each PHI 69 static void ResetDS(CgPhiOcc *phiOcc); 70 void ComputeDS(); 71 72 protected: 73 virtual void ComputeVarAndDfPhis() = 0; 74 virtual void CreateSortedOccs(); 75 CgOccur *CreateRealOcc(Insn &insn, Operand &opnd, OccType occType); 76 virtual void BuildWorkList() = 0; 77 /* for stmt pre only */ CreateExitOcc(BB & bb)78 void CreateExitOcc(BB &bb) 79 { 80 CgOccur *exitOcc = ssaPreMemPool->New<CgOccur>(kOccExit, 0, bb, nullptr); 81 exitOccs.push_back(exitOcc); 82 } 83 84 DomAnalysis *dom; 85 MemPool *ssaPreMemPool; 86 MapleAllocator ssaPreAllocator; 87 MemPool *perCandMemPool; 88 MapleAllocator perCandAllocator; 89 MapleList<PreWorkCand *> workList; 90 PreWorkCand *workCand = nullptr; // the current PreWorkCand 91 PreKind preKind; 92 93 // PRE work candidates; incremented by 2 for each tree; 94 // purpose is to avoid processing a node the third time 95 // inside a tree (which is a DAG) 96 // the following 3 lists are all maintained in order of dt_preorder 97 MapleVector<CgOccur *> allOccs; // cleared at start of each workcand 98 MapleVector<CgPhiOcc *> phiOccs; // cleared at start of each workcand 99 MapleVector<CgOccur *> exitOccs; // this is shared by all workcands 100 uint32 preLimit; // set by command-line option to limit the number of candidates optimized (for debugging purpose) 101 // step 1 phi insertion data structures 102 // following are set of BBs in terms of their dfn's; index into 103 // dominance->pdt_preorder to get their bbid's 104 MapleSet<uint32> dfPhiDfns; // phis inserted due to dominance frontiers 105 MapleSet<uint32> varPhiDfns; // phis inserted due to the var operands 106 // step 2 renaming data structures 107 uint32 classCount = 0; // count class created during renaming 108 // is index into workCand->realOccs 109 // step 6 codemotion data structures 110 MapleMap<Operand *, Operand *> temp2LocalRefVarMap; 111 int32 reBuiltOccIndex = -1; // stores the size of worklist every time when try to add new worklist, update before 112 // each code motion 113 uint32 strIdxCount = 114 0; // ssapre will create a lot of temp variables if using var to store redundances, start from 0 115 PreWorkCandHashTable preWorkCandHashTable; 116 }; 117 } // namespace maplebe 118 #endif // MAPLEBE_CG_INCLUDE_CGPRE_H 119