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_VREGS_H_
18 #define BERBERIS_BACKEND_X86_64_RENAME_VREGS_H_
19 
20 #include "berberis/backend/x86_64/machine_ir.h"
21 #include "berberis/base/arena_vector.h"
22 #include "berberis/base/logging.h"
23 
24 namespace berberis::x86_64 {
25 
26 // Exported for testing only.
27 class VRegMap {
28  public:
VRegMap(MachineIR * machine_ir)29   explicit VRegMap(MachineIR* machine_ir)
30       : machine_ir_(machine_ir),
31         map_(
32             machine_ir->NumBasicBlocks(),
33             ArenaVector<MachineReg>(machine_ir->NumVReg(), kInvalidMachineReg, machine_ir->arena()),
34             machine_ir->arena()),
35         max_size_(machine_ir->NumVReg(), 0, machine_ir->arena()) {}
36 
37   // Rename vregs so that they have different numbers in different basic blocks.
38   // Remember the mapping, so that it can be retrieved by Get().
39   void AssignNewVRegs();
40 
41   MachineReg Get(MachineReg reg, const MachineBasicBlock* bb);
GetMaxSize(MachineReg reg)42   [[nodiscard]] int GetMaxSize(MachineReg reg) const { return max_size_.at(reg.GetVRegIndex()); }
43 
44  private:
45   MachineIR* machine_ir_;
46   ArenaVector<ArenaVector<MachineReg>> map_;
47   ArenaVector<int> max_size_;
48 };
49 
50 void RenameVRegs(MachineIR* machine_ir);
51 
52 }  // namespace berberis::x86_64
53 
54 #endif  // BERBERIS_BACKEND_X86_64_RENAME_VREGS_H_
55