1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BERBERIS_BACKEND_X86_64_RENAME_COPY_USES_H_
18 #define BERBERIS_BACKEND_X86_64_RENAME_COPY_USES_H_
19 
20 #include <stdint.h>
21 
22 #include "berberis/backend/common/machine_ir.h"
23 #include "berberis/backend/x86_64/machine_ir.h"
24 #include "berberis/base/algorithm.h"
25 #include "berberis/base/arena_vector.h"
26 
27 namespace berberis::x86_64 {
28 
29 class RenameCopyUsesMap {
30  public:
RenameCopyUsesMap(MachineIR * machine_ir)31   explicit RenameCopyUsesMap(MachineIR* machine_ir)
32       : map_(machine_ir->NumVReg(), {kInvalidMachineReg, 0, 0}, machine_ir->arena()) {}
33 
34   void RenameUseIfMapped(MachineInsn* insn, int i);
35   void ProcessDef(MachineInsn* insn, int i);
36   void ProcessCopy(MachineInsn* copy);
Tick()37   void Tick() { time_++; }
38   void StartBasicBlock(MachineBasicBlock* bb);
39 
40  private:
41   struct RenameData {
42     MachineReg renamed;
43     uint64_t renaming_time;
44     uint64_t last_def_time;
45   };
46 
47   MachineReg Get(MachineReg reg);
RenameDataForReg(MachineReg reg)48   RenameData& RenameDataForReg(MachineReg reg) { return map_.at(reg.GetVRegIndex()); }
49 
50   ArenaVector<RenameData> map_;
51   // Since we are not SSA or SSI we keep track of time of definitions to see if
52   // mappings are still active.
53   uint64_t time_ = 0;
54   MachineBasicBlock* bb_;
55 };
56 
57 void RenameCopyUses(MachineIR* machine_ir);
58 
59 }  // namespace berberis::x86_64
60 
61 #endif  // BERBERIS_BACKEND_X86_64_RENAME_COPY_USES_H_
62